New batchZero to DevOps starts 29 Nov · limited seatsReserve your seat
Cheat sheetsGitHub ActionsGitHub Actions
FreeSheet 01 of 6

Workflow Basics & Syntax

A comprehensive developer cheatsheet covering GitHub Actions fundamentals and workflow syntax, featuring essential YAML configurations, event triggers, runner setups, matrix strategies, and job dependency management.

What is GitHub Actions?

GitHub Actions is a CI/CD platform built into GitHub that allows you to automate build, test, and deployment workflows directly from your repository.

Key Features

FeatureDescription
Native IntegrationBuilt into GitHub, no extra setup
Free for Public ReposUnlimited minutes for public repos
Matrix BuildsTest across multiple environments
Self-hosted RunnersRun on your own infrastructure
Marketplace10,000+ pre-built actions
Event-drivenTrigger on push, PR, schedule, etc.

GitHub Actions vs Jenkins

FeatureGitHub ActionsJenkins
SetupZero (built-in)Manual
MaintenanceNoneSelf-managed
CostFree for publicInfrastructure cost
MarketplaceGitHub MarketplaceJenkins Plugins
Learning CurveGentleSteep
ScalabilityAutomaticManual

Core Concepts

Architecture Overview

text
┌─────────────────────────────────────────────────────────────────┐
│                         GitHub Repository                       │
│  ┌─────────────────────────────────────────────────────────┐   │
│  │  .github/workflows/                                    │   │
│  │  ├── ci.yml                                             │   │
│  │  ├── cd.yml                                             │   │
│  │  └── security-scan.yml                                  │   │
│  └─────────────────────────────────────────────────────────┘   │
│                              │                                   │
│                              ▼                                   │
│  ┌─────────────────────────────────────────────────────────┐   │
│  │                     Event Trigger                        │   │
│  │  push · pull_request · schedule · workflow_dispatch    │   │
│  └─────────────────────────────────────────────────────────┘   │
│                              │                                   │
│                              ▼                                   │
│  ┌─────────────────────────────────────────────────────────┐   │
│  │                        Job 1                             │   │
│  │  ┌──────────────────────────────────────────────────┐   │   │
│  │  │  Step 1: Checkout code                           │   │   │
│  │  │  Step 2: Install dependencies                    │   │   │
│  │  │  Step 3: Run tests                               │   │   │
│  │  │  Step 4: Build artifacts                         │   │   │
│  │  └──────────────────────────────────────────────────┘   │   │
│  └─────────────────────────────────────────────────────────┘   │
│                              │                                   │
│                              ▼                                   │
│  ┌─────────────────────────────────────────────────────────┐   │
│  │                        Job 2                             │   │
│  │  ┌──────────────────────────────────────────────────┐   │   │
│  │  │  Step 1: Download artifacts                      │   │   │
│  │  │  Step 2: Deploy to staging                      │   │   │
│  │  └──────────────────────────────────────────────────┘   │   │
│  └─────────────────────────────────────────────────────────┘   │
└─────────────────────────────────────────────────────────────────┘

Key Concepts Table

ConceptDescriptionExample
WorkflowYAML file defining the automation.github/workflows/ci.yml
EventTriggers the workflowpush, pull_request
JobSet of steps running on a runnerbuild, test, deploy
StepIndividual task in a jobactions/checkout@v4
ActionReusable unit of codeactions/setup-node@v4
RunnerServer executing the jobubuntu-latest, windows-latest
MatrixRun jobs with multiple configurationsnode-version: [18, 20, 22]

Workflow File Structure

File Location

text
.github/
└── workflows/
    ├── ci.yml
    ├── cd.yml
    ├── security-scan.yml
    └── manual-deploy.yml

Anatomy of a Workflow File

yaml
# Workflow name (displayed in GitHub UI)
name: CI Pipeline

# Event triggers
on:
  push:
    branches: [ main, develop ]
  pull_request:
    branches: [ main ]
  schedule:
    - cron: '0 2 * * *'  # Daily at 2 AM
  workflow_dispatch:      # Manual trigger

# Environment variables (global)
env:
  NODE_VERSION: '18'
  REGISTRY: docker.io

# Jobs
jobs:
  build:
    name: Build Application
    runs-on: ubuntu-latest
    
    # Environment variables (job-specific)
    env:
      ENV: production
    
    # Timeout
    timeout-minutes: 30
    
    # Conditional execution
    if: github.ref == 'refs/heads/main'
    
    # Strategy (matrix builds)
    strategy:
      matrix:
        node-version: [18, 20, 22]
        os: [ubuntu-latest, windows-latest]
    
    # Steps
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
      
      - name: Install dependencies
        run: npm install
      
      - name: Run tests
        run: npm test
      
      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: build-artifacts
          path: dist/

  deploy:
    name: Deploy Application
    runs-on: ubuntu-latest
    needs: build  # Wait for build job to complete
    if: github.ref == 'refs/heads/main'
    
    steps:
      - name: Download artifacts
        uses: actions/download-artifact@v4
        with:
          name: build-artifacts
      
      - name: Deploy to staging
        run: ./deploy.sh

Basic Workflow Example

Simple CI Workflow

Create .github/workflows/ci.yml:

yaml
name: CI

on:
  push:
    branches: [ "main", "develop" ]
  pull_request:
    branches: [ "main" ]

jobs:
  build:
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Use Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '18.x'
      
      - name: Install dependencies
        run: npm ci
      
      - name: Run tests
        run: npm test
      
      - name: Build
        run: npm run build
      
      - name: Upload build artifacts
        uses: actions/upload-artifact@v4
        with:
          name: build
          path: dist/

Simple Python Workflow

yaml
name: Python CI

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

jobs:
  test:
    runs-on: ubuntu-latest
    
    strategy:
      matrix:
        python-version: ["3.9", "3.10", "3.11"]
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}
      
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
      
      - name: Lint
        run: |
          pip install flake8
          flake8 . --count --max-complexity=10
      
      - name: Test
        run: |
          pip install pytest
          pytest

Simple Docker Workflow

yaml
name: Docker Build

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      
      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      
      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: myusername/myapp:latest

Common Triggers (Events)

Basic Triggers

yaml
# Push to specific branches
on:
  push:
    branches: [ main, develop ]

# Pull request to specific branches
on:
  pull_request:
    branches: [ main ]

# Both push and pull_request
on: [push, pull_request]

Advanced Triggers

yaml
# Path-based filtering
on:
  push:
    branches: [ main ]
    paths:
      - 'src/**'
      - 'Dockerfile'
      - '!docs/**'  # Exclude docs

# Tag-based triggers
on:
  push:
    tags:
      - 'v*'  # Tags starting with 'v'

# Scheduled triggers (cron)
on:
  schedule:
    - cron: '0 2 * * *'      # Daily at 2 AM
    - cron: '0 8 * * 1'      # Every Monday at 8 AM

# Manual trigger
on:
  workflow_dispatch:
    inputs:
      environment:
        description: 'Deploy environment'
        required: true
        default: 'staging'
        type: choice
        options:
          - staging
          - production
      version:
        description: 'Version to deploy'
        required: true
        type: string

# Workflow run trigger
on:
  workflow_run:
    workflows: ["CI Pipeline"]
    types: [completed]

# Repository dispatch
on:
  repository_dispatch:
    types: [deploy]

Jobs & Runners

Runners

RunnerOSUse Case
ubuntu-latestLinuxMost builds
ubuntu-22.04Linux (specific)Compatibility
windows-latestWindowsWindows builds
macos-latestmacOSiOS/macOS builds
self-hostedCustomPrivate runners

Runner Examples

yaml
# Single runner
jobs:
  build:
    runs-on: ubuntu-latest

# Multiple runners
jobs:
  build:
    runs-on: [ubuntu-latest, self-hosted]

# Matrix builds
jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [18, 20, 22]
        os: [ubuntu-latest, windows-latest]

Job Dependencies

yaml
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - run: echo "Building..."
  
  test:
    runs-on: ubuntu-latest
    needs: build  # Wait for build job
    steps:
      - run: echo "Testing..."
  
  deploy:
    runs-on: ubuntu-latest
    needs: [build, test]  # Wait for both
    steps:
      - run: echo "Deploying..."

Steps & Actions

Step Syntax

yaml
steps:
  # Using an action
  - name: Checkout code
    uses: actions/checkout@v4
  
  # Using a shell command
  - name: Run script
    run: echo "Hello World"
  
  # Using environment variables
  - name: Use env var
    run: echo ${{ env.MY_VAR }}
  
  # Conditional step
  - name: Conditional step
    if: github.ref == 'refs/heads/main'
    run: echo "Running on main branch"
  
  # Continue on error
  - name: Continue even if fails
    run: npm test
    continue-on-error: true
  
  # Timeout per step
  - name: Long running step
    timeout-minutes: 10
    run: ./long-running-script.sh

Common Actions

ActionPurposeExample
actions/checkout@v4Checkout codeuses: actions/checkout@v4
actions/setup-node@v4Setup Node.jsuses: actions/setup-node@v4
actions/setup-python@v4Setup Pythonuses: actions/setup-python@v4
actions/upload-artifact@v4Upload artifactsuses: actions/upload-artifact@v4
actions/download-artifact@v4Download artifactsuses: actions/download-artifact@v4
actions/cache@v3Cache dependenciesuses: actions/cache@v3
docker/login-action@v3Docker loginuses: docker/login-action@v3
docker/build-push-action@v5Docker build/pushuses: docker/build-push-action@v5
aws-actions/configure-aws-credentials@v4AWS credentialsuses: aws-actions/configure-aws-credentials@v4
azure/login@v1Azure loginuses: azure/login@v1

Quick Reference — Most Used Commands

TaskYAML Snippet
Basic workflowname: CI + on: [push] + jobs: { build: { runs-on: ubuntu-latest, steps: ... } }
Checkout code- uses: actions/checkout@v4
Setup Node.js- uses: actions/setup-node@v4 + with: { node-version: '18.x' }
Run npm install- run: npm install
Run tests- run: npm test
Upload artifact- uses: actions/upload-artifact@v4 + with: { name: build, path: dist/ }
Download artifact- uses: actions/download-artifact@v4 + with: { name: build }
Cache dependencies- uses: actions/cache@v3 + with: { path: ~/.npm, key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}}
Conditional stepif: github.ref == 'refs/heads/main'
Matrix buildstrategy: { matrix: { node-version: [18, 20, 22] } }
Manual triggerworkflow_dispatch:

Pro Tips

  1. Use actions/checkout@v4 — always the first step.

  2. Pin action versions — use @v4 instead of @latest for stability.

  3. Use matrix builds — test across multiple environments.

  4. Cache dependencies — speed up builds significantly.

  5. Use environment variables — for secrets and configuration.

  6. Keep workflows in .github/workflows/ — with .yml extension.

  7. Use workflow_dispatch — for manual triggers.

  8. Use paths and paths-ignore — to avoid unnecessary runs.

  9. Check GitHub Actions status — https://www.githubstatus.com/.

  10. Use needs — to control job execution order.


Next in GitHub ActionsEnvironment Variables & Secrets
GitHub Actions cheat sheet: Workflow Basics & Syntax | InfraThrone DevOps Park