Skip to content

GitHub Actions Intro

Automating workflows with GitHub Actions — CI/CD pipelines triggered by Git events.

What you'll learn

  • What GitHub Actions is and how it relates to Git
  • The structure of a workflow YAML file
  • Common triggers: push, pull request, schedule
  • Running tests, builds, and deployments automatically

Concept

What Is GitHub Actions?

GitHub Actions is GitHub's built-in CI/CD (Continuous Integration / Continuous Deployment) platform. It automates workflows that run in response to Git events — pushes, pull requests, tags, schedules, and more.

When you push code, GitHub Actions can automatically:

  • Run your test suite
  • Build your project
  • Deploy to staging or production
  • Send notifications
  • Run security scans
  • Generate documentation

This is the natural extension of Git: Git tracks your changes, and Actions responds to them.

How It Works

  1. You create a workflow YAML file in .github/workflows/
  2. A trigger (like a push) starts the workflow
  3. The workflow runs one or more jobs
  4. Each job runs on a runner (a virtual machine)
  5. Jobs contain steps — individual commands or pre-built actions
push to main → trigger → job (test) → job (build) → job (deploy)

Workflow Structure

# .github/workflows/ci.yml
name: CI

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

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
      - run: npm install
      - run: npm test

Key Fields

  • name — workflow name (shown in the Actions tab)
  • on — the trigger(s) that start the workflow
  • jobs — the work to do
  • runs-on — the OS/environment (ubuntu-latest, windows-latest, macos-latest)
  • steps — individual tasks within a job
  • uses — references a pre-built action (like actions/checkout)
  • run — runs a shell command

Common Triggers

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]
  schedule:
    - cron: '0 2 * * *'  # every day at 2 AM
  workflow_dispatch:       # manual trigger
  release:
    types: [published]

Using Pre-Built Actions

The GitHub Marketplace has thousands of reusable actions:

steps:
  - uses: actions/checkout@v4           # check out your code
  - uses: actions/setup-node@v4         # set up Node.js
    with:
      node-version: '20'
  - uses: actions/cache@v4              # cache dependencies
    with:
      path: ~/.npm
      key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
  - uses: peaceiris/actions-gh-pages@v3 # deploy to GitHub Pages

A Complete CI Workflow

name: CI/CD

on:
  push:
    branches: [main]
  pull_request:

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [18, 20]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm ci
      - run: npm run lint
      - run: npm test
      - run: npm run build

  deploy:
    needs: test
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm ci && npm run build
      - run: npx wrangler deploy

This workflow:

  1. Runs tests on every push and PR (matrix tests on Node 18 and 20)
  2. Deploys only on pushes to main, after tests pass (needs: test)

Viewing Workflow Results

  • Go to your repo's Actions tab on GitHub
  • See workflow runs, logs, and status checks
  • Use the CLI: gh run list, gh run view
gh run list              # list recent runs
gh run view <run-id>     # view details of a run
gh run watch             # watch a running workflow
gh workflow list         # list workflows

Why This Matters for Git

GitHub Actions is the bridge between Git and automation. Every git push can trigger a pipeline that validates your code. This means:

  • Bugs are caught before they reach main
  • Deployments are automatic and consistent
  • Your team has a shared definition of "done" (the pipeline passes)

Use the CI/CD YAML Generator tool to create workflow files tailored to your project.

Example

              # --- Create a CI workflow ---
mkdir -p .github/workflows
cat > .github/workflows/ci.yml << 'EOF'
name: CI

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

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
      - run: npm ci
      - run: npm run lint
      - run: npm test -- --coverage
      - run: npm run build

  deploy:
    needs: test
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm ci && npm run build
      - name: Deploy
        run: echo "Deploy step here"
EOF

# Commit and push to trigger the workflow
git add .github/workflows/ci.yml
git commit -m "ci: add test and deploy workflow"
git push origin main

# --- View workflow runs ---
gh run list
gh run view --web

# --- Watch a running workflow ---
gh run watch

# --- Manually trigger a workflow ---
gh workflow run "CI"

# --- List and manage workflows ---
gh workflow list
gh workflow disable "CI"
gh workflow enable "CI"

# --- A release workflow triggered by tags ---
cat > .github/workflows/release.yml << 'EOF'
name: Release
on:
  push:
    tags: ['v*']
jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: softprops/action-gh-release@v1
        with:
          generate_release_notes: true
EOF

git tag -a v1.0.0 -m "Release 1.0.0"
git push origin v1.0.0  # triggers the release workflow
            

Creates a CI workflow that runs tests on push and PR, deploys on main, then shows viewing and managing runs with the GitHub CLI. Also creates a release workflow triggered by version tags. The workflow YAML is the core of GitHub Actions automation.

Try it

Common mistakes

The mistake

Committing secrets directly in the workflow YAML file

The fix

Never put secrets (API keys, passwords, tokens) in YAML files. Use GitHub Secrets: go to Settings > Secrets and variables > Actions, add your secret, then reference it as ${{ secrets.MY_SECRET }} in the workflow. Secrets are encrypted and masked in logs.

The mistake

Not caching dependencies, making CI runs slow and expensive

The fix

Use actions/cache to cache node_modules, pip packages, or other dependencies. Caching keyed on the lock file hash means the cache is reused when dependencies have not changed. This can cut CI time from minutes to seconds. See the actions/cache documentation for setup patterns.

Related Cheatsheets

Practice GitHub Actions Intro

1 exercise

Practice GitHub Actions Intro

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors