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
| Feature | Description |
|---|---|
| Native Integration | Built into GitHub, no extra setup |
| Free for Public Repos | Unlimited minutes for public repos |
| Matrix Builds | Test across multiple environments |
| Self-hosted Runners | Run on your own infrastructure |
| Marketplace | 10,000+ pre-built actions |
| Event-driven | Trigger on push, PR, schedule, etc. |
GitHub Actions vs Jenkins
| Feature | GitHub Actions | Jenkins |
|---|---|---|
| Setup | Zero (built-in) | Manual |
| Maintenance | None | Self-managed |
| Cost | Free for public | Infrastructure cost |
| Marketplace | GitHub Marketplace | Jenkins Plugins |
| Learning Curve | Gentle | Steep |
| Scalability | Automatic | Manual |
Core Concepts
Architecture Overview
┌─────────────────────────────────────────────────────────────────┐
│ 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
| Concept | Description | Example |
|---|---|---|
| Workflow | YAML file defining the automation | .github/workflows/ci.yml |
| Event | Triggers the workflow | push, pull_request |
| Job | Set of steps running on a runner | build, test, deploy |
| Step | Individual task in a job | actions/checkout@v4 |
| Action | Reusable unit of code | actions/setup-node@v4 |
| Runner | Server executing the job | ubuntu-latest, windows-latest |
| Matrix | Run jobs with multiple configurations | node-version: [18, 20, 22] |
Workflow File Structure
File Location
.github/
└── workflows/
├── ci.yml
├── cd.yml
├── security-scan.yml
└── manual-deploy.ymlAnatomy of a Workflow File
# 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.shBasic Workflow Example
Simple CI Workflow
Create .github/workflows/ci.yml:
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
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
pytestSimple Docker Workflow
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:latestCommon Triggers (Events)
Basic Triggers
# 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
# 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
| Runner | OS | Use Case |
|---|---|---|
ubuntu-latest | Linux | Most builds |
ubuntu-22.04 | Linux (specific) | Compatibility |
windows-latest | Windows | Windows builds |
macos-latest | macOS | iOS/macOS builds |
self-hosted | Custom | Private runners |
Runner Examples
# 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
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
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.shCommon Actions
| Action | Purpose | Example |
|---|---|---|
actions/checkout@v4 | Checkout code | uses: actions/checkout@v4 |
actions/setup-node@v4 | Setup Node.js | uses: actions/setup-node@v4 |
actions/setup-python@v4 | Setup Python | uses: actions/setup-python@v4 |
actions/upload-artifact@v4 | Upload artifacts | uses: actions/upload-artifact@v4 |
actions/download-artifact@v4 | Download artifacts | uses: actions/download-artifact@v4 |
actions/cache@v3 | Cache dependencies | uses: actions/cache@v3 |
docker/login-action@v3 | Docker login | uses: docker/login-action@v3 |
docker/build-push-action@v5 | Docker build/push | uses: docker/build-push-action@v5 |
aws-actions/configure-aws-credentials@v4 | AWS credentials | uses: aws-actions/configure-aws-credentials@v4 |
azure/login@v1 | Azure login | uses: azure/login@v1 |
Quick Reference — Most Used Commands
| Task | YAML Snippet |
|---|---|
| Basic workflow | name: 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 step | if: github.ref == 'refs/heads/main' |
| Matrix build | strategy: { matrix: { node-version: [18, 20, 22] } } |
| Manual trigger | workflow_dispatch: |
Pro Tips
-
Use
actions/checkout@v4— always the first step. -
Pin action versions — use
@v4instead of@latestfor stability. -
Use matrix builds — test across multiple environments.
-
Cache dependencies — speed up builds significantly.
-
Use environment variables — for secrets and configuration.
-
Keep workflows in
.github/workflows/— with.ymlextension. -
Use
workflow_dispatch— for manual triggers. -
Use
pathsandpaths-ignore— to avoid unnecessary runs. -
Check GitHub Actions status —
https://www.githubstatus.com/. -
Use
needs— to control job execution order.