GitHub Actions basics

Topic 1 of 3 in Automation and releases8 min readBeginnerFree course

Overview

GitHub Actions runs automation in response to things that happen in your repository: a push, a pull request, a new tag, a schedule, or a button click. Teams use it to run tests on every PR, check code style, build and deploy applications, publish packages, and handle chores like labelling issues. It’s built into every repository and free for public repositories.

How a GitHub Actions workflow is built. An event — push, pull_request, schedule or manual — triggers the workflow in .github/workflows/ci.yml. It has two jobs: test, on ubuntu-latest, with steps uses: actions/checkout@v7, uses: actions/setup-node@v7, run: npm ci and run: npm test; and deploy, which needs test, with steps uses: actions/checkout@v7 and run: ./deploy.sh. Each job runs on a fresh virtual machine (runner). Jobs run in parallel unless one needs another. Workflow → jobs → steps: a step either uses a published action or runs a shell command.

The building blocks

Term Meaning
Workflow a YAML file in .github/workflows/ describing an automated process
Event what triggers the workflow (push, pull_request, schedule, workflow_dispatch…)
Job a set of steps that runs on one runner; jobs run in parallel by default
Runner the machine a job runs on — GitHub-hosted (Ubuntu, Windows, macOS) or your own
Step one command (run:) or one action (uses:)
Action a reusable, published step, like actions/checkout

A first workflow

YAML
# .github/workflows/hello.yml
name: Hello

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

permissions:
  contents: read

jobs:
  greet:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - name: Say hello
        run: echo "Hello ${{ github.actor }}, this run is for ${{ github.ref_name }}"

Commit it, push, and open the repository’s Actions tab: you’ll see the run, each job and the log of every step. From the terminal: gh run list, gh run watch, gh run view --log.

YAML is indentation-sensitive — use spaces, not tabs, and keep nesting consistent. Your editor’s GitHub Actions extension (or the actionlint tool) catches most mistakes before you push.

Events

YAML
on:
  push:
    branches: [main]          # only pushes to main
    paths: ["src/**"]         # only when these files change
  pull_request:               # PRs targeting any branch (or filter with branches:)
  schedule:
    - cron: "0 6 * * 1"       # every Monday 06:00 UTC
  workflow_dispatch:          # a "Run workflow" button (and gh workflow run)
  release:
    types: [published]

There are dozens of events — issues opened, PR labelled, comments, and more — so workflows can automate almost anything in a repository.

Jobs and steps

Each job starts on a fresh runner: nothing from a previous job or run is there unless you check it out, cache it or pass it as an artifact. That’s why most jobs start with actions/checkout.

YAML
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7          # get the repository's code
      - uses: actions/setup-node@v7        # install a tool
        with:
          node-version: 24
      - run: npm ci                        # shell commands
      - run: |
          npm run build
          ls dist/

  deploy:
    needs: build                           # run after build succeeds
    if: github.ref == 'refs/heads/main'    # only on main
    runs-on: ubuntu-latest
    steps:
      - run: echo "deploying…"
  • uses: runs an action. with: passes its inputs.
  • run: runs shell commands — | for several lines. A step fails if a command exits with a non-zero code, and a failed step fails the job.
  • needs: makes a job wait for others; if: adds conditions.

Actions and versions

Actions are referenced as owner/repository@version. The actions/ organisation is maintained by GitHub; thousands more are in the GitHub Marketplace.

Because an action runs code inside your workflow, treat it like any dependency:

  • Prefer official and well-maintained actions.
  • Pin a version. @v7 follows the latest v7.x release. For the strongest guarantee against a compromised action, pin third-party actions to a full commit SHA: uses: some/action@8f4b7f84… # v2.1.0. Dependabot can keep pinned versions up to date.

Contexts, variables and secrets

${{ … }} expressions insert values:

  • github.* — the event: github.actor, github.ref_name, github.sha, github.event_name…
  • env.* and vars.* — environment variables and repository variables (Settings → Secrets and variables → Actions → Variables).
  • secrets.* — secrets: encrypted values like API keys.
YAML
- name: Deploy
  run: ./deploy.sh
  env:
    API_URL: ${{ vars.API_URL }}
    DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}

Secrets are masked (***) if they appear in logs, and they’re not given to workflows triggered by pull requests from forks, so outside contributors can’t steal them.

Terminal
gh secret set DEPLOY_TOKEN        # prompts for the value

The GITHUB_TOKEN

Every run gets an automatic, short-lived token, secrets.GITHUB_TOKEN (also github.token), that can call the GitHub API for this repository. Control what it can do with permissions::

YAML
permissions:
  contents: read          # read the code
  pull-requests: write    # e.g. to comment on PRs

Grant the least needed. When you set permissions, anything not listed gets no access. Prefer GITHUB_TOKEN over personal access tokens in workflows — it expires when the job ends.

Writing to the log

Workflow commands make logs and PRs more helpful:

Terminal
echo "::error file=src/cart.js,line=14::Total ignores quantity"   # annotation on the PR
echo "::warning::Deprecated config option"
echo "### Test summary ✓" >> "$GITHUB_STEP_SUMMARY"                  # Markdown summary on the run page

Costs and limits

GitHub-hosted runners are free for public repositories. Private repositories get a monthly allowance of minutes that depends on the plan; Windows and macOS runners use it faster than Linux. Keep workflows efficient — run only what’s needed (paths: filters), cache dependencies, and cancel outdated runs (next lesson).

Start small: one workflow, one job, one test command. You can grow it into a full pipeline once you’ve seen it run green.

Exercise

Your first workflow

Create .github/workflows/hello.yml. It should run on every push to main, on every pull request, and manually from the Actions tab. It has one job, greet, on the latest Ubuntu runner, with read-only repository permissions. The steps: check out the code, print a greeting that includes the name of the person who triggered the run and the branch, list the repository’s files, and run a multi-line script that fails the job if README.md is missing. Push it and watch it run.

YAML
# .github/workflows/hello.yml
name: Hello

on:
  # 1. Push to main, pull requests, manual runs

# 2. Least-privilege token

jobs:
  greet:
    # 3. Runner

    steps:
      # 4. Check out the code

      # 5. Greeting with actor and branch

      # 6. List files

      # 7. Fail if README.md is missing

Summary

  • Workflows are YAML files in .github/workflows/; events such as push, pull_request, schedule or workflow_dispatch trigger them.
  • A workflow has jobs; each job runs on a fresh runner and is a list of steps.
  • A step either uses: a reusable action (like actions/checkout@v7) or run:s shell commands.
  • Contexts like ${{ github.actor }} and ${{ secrets.NAME }} inject data; secrets are masked in logs.
  • Give the automatic GITHUB_TOKEN least-privilege permissions, and pin third-party actions to a version.