GitHub Actions Introduction
.github/workflows/, YAML structure, jobs, steps, runners — the anatomy of a workflow
Skills taught: cicd.workflow-syntax, cicd.jobs, cicd.steps, cicd.runners, cicd.contexts
What is GitHub Actions?
GitHub Actions is GitHub's built-in CI/CD platform — free for public repos, generous free tier for private ones. Workflows are defined in YAML files inside .github/workflows/. When events happen (push, PR, schedule), GitHub spins up a fresh virtual machine (a runner), clones your repo, and runs your workflow. No server to configure, no plugins to install.
Anatomy of a workflow file
# Every workflow has a name (shown in GitHub UI)
name: CI
# ── on: TRIGGER ──────────────────────────────────────────
# When should this workflow run?
on:
push:
branches: [main, dev] # on push to these branches
pull_request:
branches: [main] # on PR targeting main
workflow_dispatch: # manual trigger from GitHub UI
# ── jobs ─────────────────────────────────────────────────
# Jobs run in PARALLEL by default (unless needs: is specified)
jobs:
# Job 1: run tests
test:
# The OS to run on — ubuntu-latest is the most common
runs-on: ubuntu-latest
# ── steps: the actual commands ──────────────────────
steps:
# Every workflow starts by checking out your code
- name: Checkout code
uses: actions/checkout@v4
# Set up Node.js
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm' # cache node_modules between runs
# Install dependencies
- name: Install dependencies
run: npm ci
# Run tests
- name: Run tests
run: npm test
# Job 2: lint (runs in parallel with test)
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with: { node-version: '20', cache: 'npm' }
- run: npm ci
- run: npm run lint
# Job 3: only runs if test AND lint pass
build:
needs: [test, lint] # wait for both jobs to succeed
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with: { node-version: '20', cache: 'npm' }
- run: npm ci
- run: npm run build
Key concepts
- Workflow — the YAML file. One repo can have multiple workflows (ci.yml, deploy.yml, release.yml).
- Job — a group of steps that run on one runner. Jobs run in parallel by default.
- Step — a single task: either a shell command (
run:) or a pre-built action (uses:). - Action — a reusable step from the GitHub Marketplace.
actions/checkout@v4 clones your repo. actions/setup-node@v4 installs Node.js. - Runner — the VM that executes the job.
ubuntu-latest, windows-latest, macos-latest are GitHub-hosted. You can also self-host. - Context — runtime data available via
${{ }}: ${{ github.sha }}, ${{ github.actor }}, ${{ secrets.MY_SECRET }}.
steps:
- name: Print useful context values
run: |
echo "Branch: ${{ github.ref_name }}"
echo "Commit SHA: ${{ github.sha }}"
echo "Actor (who triggered): ${{ github.actor }}"
echo "Event: ${{ github.event_name }}"
echo "Repo: ${{ github.repository }}"
# Conditional step — only on main branch
- name: Deploy (only on main)
if: github.ref == 'refs/heads/main'
run: ./deploy.sh
# Environment-specific step
- name: Set image tag
run: echo "IMAGE_TAG=${{ github.sha }}" >> $GITHUB_ENV
- name: Use the tag set above
run: echo "Building image:$IMAGE_TAG"
Tip: Always pin action versions. Use actions/checkout@v4 not actions/checkout@latest. Pinning to a major version (v4) protects you from breaking changes while still getting patches. For maximum security, pin to a full SHA: actions/checkout@9bb56186c3edbf8a20b5...@v4
Frequently Asked Questions
How much does GitHub Actions cost?
Public repositories: completely free, unlimited minutes. Private repositories: 2,000 minutes/month free on GitHub Free, 3,000 on Pro, 50,000 on Teams. ubuntu-latest uses 1 minute of quota per minute of compute. macOS uses 10× (10 minutes quota per minute). Most projects stay within the free tier.