GitHub Actions: Terminology – DevOpsSchool.com

Here’s a table that summarizes key GitHub Actions terminology, providing a clear and concise explanation of each term:

Term Definition
Workflow A configurable automated process made up of one or more jobs. Defined in YAML files in .github/workflows/.
Job A set of steps executed sequentially. Multiple jobs can run in parallel or sequentially.
Step A single task that runs as part of a job. It can be a shell command or an action.
Action A reusable command or script that can be shared and used in workflows. Defined in public repositories or locally.
Runner A server that runs jobs. It can be GitHub-hosted (runs on GitHub servers) or self-hosted (runs on your own infrastructure).
Event Triggers that start a workflow, such as push, pull_request, schedule, or workflow_dispatch.
Trigger The event or condition that initiates a workflow run.
Job Matrix A way to run a job in multiple configurations (e.g., different OS, environments, or parameters) in parallel.
Environment A collection of settings and resources (like secrets) that control how a workflow interacts with environments such as production, development, or staging.
Artifact Files generated by jobs, such as build outputs or test results, that can be shared across jobs.
Secret Encrypted variables used in workflows to store sensitive data like API keys, tokens, or passwords.
Context Information available to workflows during runtime, such as GitHub metadata, environment variables, or the repository.
Service Container Containers that can run alongside a job, typically used for databases or other services needed for the job.
Workflow Run An instance of a workflow being executed as a result of a trigger or manual dispatch.
Expression Syntax Special syntax (${{ }}) used in workflows to evaluate conditions, access context variables, or make decisions.
Cache Mechanism to store dependencies or build outputs that can be reused in future workflow runs to improve performance.
Permissions The level of access given to the GitHub Actions runner for a job, such as read or write permissions for repositories or secrets.
Status Badge A badge that shows the current status of a workflow (e.g., success, failure) that can be embedded in a README.
Timeout The maximum duration a job or workflow is allowed to run before it is automatically stopped.
Checkout An action (actions/checkout) that fetches the repository’s code in a workflow so that subsequent steps can work with it.
Cache Action An action (actions/cache) used to cache dependencies or output between workflow runs.
Matrix Strategy A feature allowing you to run the same job multiple times with different inputs (e.g., various operating systems, programming languages).
Approval Workflow A manual action that requires approval from a human (typically used in deployment workflows).
Filter Mechanism used to conditionally run workflows or jobs based on file changes, branches, or other criteria (e.g., paths, branches, tags).

Example


name: CI Pipeline

# Define the triggers (events) for this workflow
on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main
  workflow_dispatch: # Allows manually triggering the workflow

# Define the jobs in the workflow
jobs:
  # A job for building the project using a matrix strategy
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [12.x, 14.x, 16.x] # Run the job for different versions of Node.js
        os: [ubuntu-latest, windows-latest] # Run on different operating systems
    steps:
      - name: Checkout code
        uses: actions/checkout@v3 # Action to checkout the code

      - name: Set up Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}

      - name: Cache node modules
        uses: actions/cache@v3 # Cache dependencies
        with:
          path: ~/.npm
          key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-

      - name: Install Dependencies
        run: npm install

      - name: Run Unit Tests
        run: npm test

      - name: Upload Test Results as Artifacts
        if: always() # Upload artifacts even if the tests fail
        uses: actions/upload-artifact@v3
        with:
          name: test-results
          path: test-results.xml

  # A job for deploying the application, running only when building on main
  deploy:
    runs-on: ubuntu-latest
    needs: build # This job will only run if the build job succeeds
    environment: production # Specifies the environment (can be 'production', 'staging', etc.)
    if: github.ref == 'refs/heads/main' # Deploy only on pushes to main
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Node.js 16.x
        uses: actions/setup-node@v3
        with:
          node-version: 16.x

      - name: Install Dependencies
        run: npm install

      - name: Deploy to Production Server
        env:
          SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} # Using a secret for authentication
        run: |
          ssh -i $SSH_PRIVATE_KEY [email protected] "cd /var/www/app && git pull && npm install && pm2 restart app"
        
      - name: Notify on Success
        if: success()
        run: echo "Deployment successful!"

      - name: Notify on Failure
        if: failure()
        run: echo "Deployment failed!"

Explanation of Key Terminology in the Workflow

Term Explanation
Workflow The entire .yml file defines a workflow. This example has two jobs: build and deploy.
Job A collection of steps, running in a virtual machine environment. The jobs are build and deploy.
Step Individual tasks within a job. For example, checking out the code, installing dependencies, and running tests.
Action Reusable commands from the GitHub Marketplace. In this example, actions/checkout@v3, actions/setup-node@v3, etc.
Matrix Allows running the build job in parallel for different versions of Node.js and operating systems.
Runner The server that runs the jobs (ubuntu-latest or windows-latest).
Event/Trigger The workflow is triggered by events like push, pull_request, and workflow_dispatch.
Environment Specifies the environment (in this case, production) for deployment.
Artifact The test results are uploaded as artifacts using actions/upload-artifact@v3.
Cache Dependencies are cached to speed up future builds (actions/cache@v3 caches the ~/.npm directory).
Secret A secure environment variable (SSH_PRIVATE_KEY) is used to authenticate the deployment server.
if Conditional logic to control when certain steps or jobs are executed (e.g., if: success() or if: github.ref == 'refs/heads/main').
Context The expression syntax ${{ }} allows accessing GitHub contexts such as matrix, secrets, and github.ref.
needs Specifies that the deploy job depends on the build job, meaning it will only run if build is successful.
Artifact Test results are stored using artifacts, which can be used in later jobs or downloaded after the workflow finishes.

Main Features Demonstrated

  1. Triggers: The workflow triggers on push and pull_request to the main branch, and can also be manually triggered via workflow_dispatch.
  2. Matrix: The build job uses a matrix to run multiple combinations of Node.js versions (12.x, 14.x, 16.x) and operating systems (ubuntu-latest, windows-latest).
  3. Secrets: The deploy job uses a secret (SSH_PRIVATE_KEY) to securely authenticate the deployment process.
  4. Conditional Execution:
    • The deploy job only runs on the main branch.
    • The steps Notify on Success and Notify on Failure use if: success() and if: failure() to conditionally run based on the outcome of previous steps.
  5. Artifacts: Test results are uploaded as an artifact so that they can be accessed after the workflow completes.
  6. Caching: Caching is used for npm dependencies to improve performance on subsequent runs.

Conclusion:

This workflow showcases a wide variety of GitHub Actions terminologies, demonstrating how to set up a matrix build, conditionally run jobs, use secrets, cache dependencies, upload artifacts, and trigger workflows with various events.

Rajesh Kumar
Latest posts by Rajesh Kumar (see all)


Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *