Know your github actions
- Mark Kendall
- Mar 5
- 3 min read
Absolutely! Let's build a concise and practical cheat sheet for GitHub Actions, focusing on the most commonly used terms and concepts.
GitHub Actions Cheat Sheet
Core Concepts:
Workflow:
A configurable automated process that will run one or more jobs. Defined in a YAML file (.github/workflows/*.yml).
Job:
A set of steps that execute on the same runner. Runs in parallel by default.
Step:
An individual task that can run commands or actions.
Action:
A reusable unit of code. Can be a community-created action or a custom action.
Runner:
A server that runs your workflows. Can be GitHub-hosted or self-hosted.
Event:
A trigger that starts a workflow (e.g., push, pull_request, schedule).
Artifacts:
Files generated during a workflow that can be stored and downloaded.
Secrets:
Encrypted environment variables used to store sensitive information.
Workflow File Structure (YAML):
YAML
name: My Workflow Name on: # Triggers push: branches: - main pull_request: jobs: build: runs-on: ubuntu-latest # Runner type steps: - name: Checkout code uses: actions/checkout@v3 # Action usage - name: Set up Node.js uses: actions/setup-node@v3 with: node-version: '18' - name: Install dependencies run: npm install # Shell command - name: Build run: npm run build - name: Run tests run: npm test - name: Deploy if: github.ref == 'refs/heads/main' # Conditional execution run: | echo "Deploying to production..." # Deployment commands - name: Upload Artifacts uses: actions/upload-artifact@v3 with: name: build-artifacts path: build/
Key Terms and Phrases:
name:: Workflow or job name.
on:: Specifies events that trigger the workflow.
jobs:: Defines the jobs to be executed.
runs-on:: Specifies the runner environment.
steps:: Defines the sequence of steps within a job.
uses:: Specifies an action to use.
run:: Executes shell commands.
with:: Provides input parameters to an action.
env:: Sets environment variables.
secrets:: Accesses stored secrets.
if:: Conditional execution of a step.
needs:: Defines dependencies between jobs.
strategy:: Defines a matrix of job configurations.
permissions:: Grants permissions to the GITHUB_TOKEN.
timeout-minutes:: Specifies the maximum execution time for a job.
continue-on-error:: Allows a job to continue even if a step fails.
GITHUB_TOKEN:: A token automatically provided by GitHub Actions.
github.ref: The branch or tag that triggered the workflow.
github.event_name: The name of the event that triggered the workflow.
github.workspace: The default working directory.
Categories of Actions (Commonly Used):
Version Control:
actions/checkout@v3: Checks out your repository.
actions/upload-artifact@v3: Uploads artifacts.
actions/download-artifact@v3: Downloads artifacts.
Language/Environment Setup:
actions/setup-node@v3: Sets up Node.js.
actions/setup-python@v4: Sets up Python.
actions/setup-java@v3: Sets up Java.
actions/setup-dotnet@v3: Sets up .NET.
actions/setup-go@v4: Sets up Go.
Build/Test/Deployment:
(Custom scripts using run:) Build, test, and deployment commands.
Third party deployment actions for cloud providers (AWS, Azure, GCP, etc.)
Notifications/Integrations:
Actions for sending Slack, Discord, or email notifications.
Actions for integrating with external tools (e.g., SonarCloud, Codecov).
Security:
Actions for static analysis security testing (SAST)
Actions for secret scanning.
Docker:
docker/build-push-action@v5: Builds and pushes Docker images.
docker/login-action@v3: Logs in to Docker registries.
Caching:
actions/cache@v3: Caches dependencies.
Tips for Effective Usage:
Use the GitHub Actions Marketplace to find reusable actions.
Leverage if: conditions to control step execution.
Use secrets: to store sensitive information securely.
Use strategy: matrix: for parallel testing across multiple configurations.
Test workflows locally using act.
Break down large workflows into smaller, more manageable jobs.
Use comments in your YAML files to document your workflow.
Example of using secrets:
YAML
steps: - name: Deploy to production run: | deploy.sh --api-key ${{ secrets.PRODUCTION_API_KEY }}
Example of using matrix:
YAML
jobs: test: runs-on: ubuntu-latest strategy: matrix: node-version: ['16', '18', '20'] steps: - name: Checkout code uses: actions/checkout@v3 - name: Set up Node.js ${{ matrix.node-version }} uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} - name: Install dependencies run: npm install - name: Run tests run: npm test
This cheat sheet should provide a solid foundation for working with GitHub Actions. Remember to refer to the official GitHub Actions documentation for the most up-to-date information.
Comments