4 Hours

2. GitHub Actions: Deep Dive into Workflows

GitHub Actions

GitHub Actions: Complete Workflow Mastery

GitHub Actions is a powerful CI/CD platform built into GitHub. It enables you to automate testing, building, and deployment directly from your repository. This module provides a deep dive into GitHub Actions workflows, covering everything from basic syntax to advanced features.

The key insight is that GitHub Actions workflows are event-driven automation. Workflows are triggered by GitHub events (push, pull_request, schedule, workflow_dispatch). Each workflow defines jobs that run on virtual machines (runners). Jobs run steps that execute commands or use actions. This model enables complete automation of your development pipeline. Understanding the workflow structure and syntax is essential for effective CI/CD implementation.

This module covers workflows, jobs, steps, actions, runners, triggers, and best practices. You'll learn to write production-grade workflows with confidence.

Workflow Structure & Syntax

A GitHub Actions workflow is a YAML file stored in .github/workflows/. It defines the automation pipeline. The basic structure includes: name (optional), on (trigger), jobs (list of jobs), each job has runs-on (runner) and steps (list of actions/commands). Steps can be uses (actions) or run (shell commands). Understanding this structure is essential for writing effective workflows.

The syntax is YAML, which is whitespace-sensitive. Indentation must be consistent. Strings can be quoted or unquoted. Environment variables can be used with ${{ env.VAR }}. Secrets are accessed with ${{ secrets.SECRET_NAME }}. Understanding YAML syntax and GitHub Actions expressions is essential for building complex workflows.

Text
name: CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
  workflow_dispatch:

jobs:
  test:
    runs-on: ubuntu-latest
    env:
      NODE_ENV: test
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '18'
      - name: Install dependencies
        run: npm install
      - name: Run tests
        run: npm test
      - name: Upload test results
        uses: actions/upload-artifact@v4
        with:
          name: test-results
          path: test-results/

Actions & Marketplace

GitHub Actions provides a marketplace of pre-built actions that you can use in your workflows. Actions are reusable units of code that perform specific tasks. Common actions include: actions/checkout (checkout code), actions/setup-node (setup Node.js), actions/setup-python (setup Python), actions/cache (cache dependencies), actions/upload-artifact (save files), and many more. Using pre-built actions saves time and reduces errors.

You can also create your own custom actions. Custom actions can be written in JavaScript or as Docker containers. They're useful for encapsulating complex logic that you reuse across multiple workflows. Custom actions can be shared privately within your organization or publicly on the marketplace. Understanding when to use pre-built actions and when to build custom ones is essential for maintainable CI/CD.

Advanced Workflow Patterns

Advanced workflow patterns include: conditional execution, matrix strategies, job dependencies, and reusable workflows. Conditional execution uses the if keyword to conditionally run steps or jobs. Matrix strategies run a job with multiple configurations. Job dependencies use the needs keyword to order jobs. Reusable workflows allow you to share workflows across repositories. These patterns enable sophisticated automation scenarios.

  • Conditional Execution: Use if to run steps only when conditions are met.
  • Matrix Strategies: Run jobs with multiple configurations in parallel.
  • Job Dependencies: Use needs to control job execution order.
  • Reusable Workflows: Share workflows across repositories.
  • Environment Protection: Require approvals before deploying to production.

Step-by-Step: Building a Production Workflow

Follow these steps to build a production-grade GitHub Actions workflow.

Step 1: Define the workflow structure
name: Production CI/CD<br><br>on:<br> push:<br> branches: [ main ]<br> pull_request:<br> branches: [ main ]<br> workflow_dispatch:

Step 2: Add the test job
jobs:<br> test:<br> runs-on: ubuntu-latest<br> strategy:<br> matrix:<br> node-version: [<strong><code>18, 20, 22]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- uses: actions/cache@v3
with:
path: node_modules
key: npm-${{ runner.os }}-${{ hashFiles(package-lock.json) }}
- run: npm install
- run: npm test

Step 3: Add the build job
build:<br> needs: test<br> runs-on: ubuntu-latest<br> if: github.ref == <strong><code>refs/heads/main
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
- run: npm install
- run: npm run build
- uses: actions/upload-artifact@v4
with:
name: build-artifacts
path: dist/

Step 4: Add the deploy job
deploy:<br> needs: build<br> runs-on: ubuntu-latest<br> if: github.ref == <strong><code>refs/heads/main
environment: production
steps:
- uses: actions/download-artifact@v4
with:
name: build-artifacts
path: dist/
- name: Deploy to production
run: echo Deploying to production...

Step 5: Add environment protection
Go to repository Settings → Environments → Add environment → production. Require approval from reviewers before deployment.

Try It Yourself: Add a slack notification step that sends a message when the workflow completes. Add a step that creates a GitHub release when the build succeeds.

Knowledge Check

Ready to test your understanding of 2. GitHub Actions: Deep Dive into Workflows?

Back