Skip to content

GitHub Actions Cheat Sheet

Complete reference for GitHub Actions workflow syntax, job orchestration, reusable workflows, expressions, secrets management, matrix builds, caching strategies, artifacts, and deployment pipelines.


Workflow Basics

Workflow files are stored inside:

.github/workflows/
name: CI Pipeline

on:
  push:
    branches: [main]
  pull_request:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: echo "Hello CI"

Events

on:
  push:
    branches:
      - main
      - release/*
  pull_request:
  workflow_dispatch:
  schedule:
    - cron: "0 3 * * *"

Jobs & Dependencies

jobs:
  build:
    runs-on: ubuntu-latest

  test:
    runs-on: ubuntu-latest
    needs: build

Matrix Strategy

strategy:
  fail-fast: false
  matrix:
    os: [ubuntu-latest, windows-latest]
    node: [16, 18]

runs-on: ${{ matrix.os }}

Context & Expressions

if: ${{ github.ref == 'refs/heads/main' }}

Common contexts: - github - env - secrets - matrix - runner


Environment Variables

env:
  NODE_ENV: production

Secrets

run: echo "${{ secrets.API_KEY }}"

Cache

- uses: actions/cache@v4
  with:
    path: ~/.npm
    key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}

Artifacts

- uses: actions/upload-artifact@v4
  with:
    name: build-output
    path: dist/

Reusable Workflows

on:
  workflow_call:
    inputs:
      environment:
        required: true
        type: string

Concurrency

concurrency:
  group: production-deploy
  cancel-in-progress: true

Permissions

permissions:
  contents: read
  packages: write

Services Example

services:
  postgres:
    image: postgres:15
    ports:
      - 5432:5432
    env:
      POSTGRES_PASSWORD: example

Deployment Example

name: Deploy

on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: production

    steps:
      - uses: actions/checkout@v4
      - run: docker build -t app .
      - run: docker push myregistry/app