Chapter 12 · Lesson 2 of 10
CI/CD and automation

GitHub Actions workflow anatomy, explained line by line

A GitHub Actions workflow is a YAML file in .github/workflows that names the events that start it, the jobs to run, and the steps in each job.

By the end you will

Read and write a minimal workflow file, and find the logs of a run in the Actions tab.

Time
15 min
Level
Beginner
Check
4 questions
You need
  • A GitHub repository you can push to
  • Basic YAML: indentation and lists

A workflow is one YAML file under .github/workflows/. It says when to run, on which machine, and which commands to execute. Once you can read the six or seven keys that appear in almost every file, you can read nearly any workflow. This lesson builds one up and then shows where to look when it fails. The pipeline idea behind it is in CI/CD pipeline stages explained.

How it works#

When an event happens in your repository, GitHub looks at every workflow file for a matching on entry. Each match creates a run. A run contains one or more jobs. Each job gets its own fresh virtual machine (a runner), executes its steps in order, and is thrown away. Jobs share nothing unless you pass data through artifacts or job outputs.

A minimal file:

yaml
name: ci

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

permissions:
  contents: read

jobs:
  hello:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Show the commit
        run: git log -1 --oneline

Later lessons extend this shape: caching dependencies, matrix builds and deploying over SSH. Save it as .github/workflows/ci.yml, commit and push. It runs on pushes to main, on pull requests, and on demand.

Step 1: Choose the triggers with on#

The on key lists events. The ones you will use most:

  • push: runs when commits are pushed. Narrow it with branches: [main] or tags: ['v*'].
  • pull_request: runs when a pull request is opened or updated. It checks the merge result of the PR.
  • workflow_dispatch: adds a manual Run workflow button, and can take inputs.
  • schedule: runs on a cron expression in UTC, for example nightly checks.
yaml
on:
  schedule:
    - cron: "0 3 * * *"

Scheduled workflows run on the default branch only. A workflow with no on match never runs, which is the usual reason a new file seems to do nothing.

Step 2: Define jobs and runners#

Under jobs: each key is a job id. runs-on picks the runner image. ubuntu-latest is a GitHub-hosted Ubuntu machine and is the default choice for Linux builds.

yaml
jobs:
  test:
    runs-on: ubuntu-latest
  deploy:
    runs-on: ubuntu-latest
    needs: test

Jobs run in parallel unless you add needs. Here deploy waits for test and is skipped if test fails. That single key is how you enforce “no deploy without green tests”.

Step 3: Write steps with uses and run#

A job is a list of steps that run in order on the same machine.

yaml
steps:
  - uses: actions/checkout@v4
  - uses: actions/setup-node@v4
    with:
      node-version: 20
  - run: npm ci
  - run: npm test

uses runs a packaged action. Always pin a version, as in @v4. with passes inputs to the action. run executes shell commands. A step that exits non-zero fails the job and the remaining steps are skipped. Use name: to give a step a readable label in the log.

The runner starts empty, so almost every job begins with actions/checkout@v4. Without it there is no code to test.

Step 4: Add conditions and environment variables#

if runs a step or job only when an expression is true:

yaml
- name: Only on main
  if: github.ref == 'refs/heads/main'
  run: echo "on main"

env sets environment variables for a workflow, a job or a single step:

yaml
jobs:
  test:
    runs-on: ubuntu-latest
    env:
      NODE_ENV: test
    steps:
      - run: echo "$NODE_ENV"

Values for env are plain text in the file, so never put a secret there. Secrets come from the secrets context and are covered in Secrets and variables in CI pipelines.

Step 5: Limit the token with permissions#

Every run gets a temporary GITHUB_TOKEN. The permissions key controls what it may do. Set it at the top of the file, or per job, to the least you need:

yaml
permissions:
  contents: read

Jobs that push images add packages: write, and jobs that comment on pull requests add pull-requests: write. Anything you do not list is denied once you set permissions. Starting narrow is cheap and limits the damage if a third-party action misbehaves.

Step 6: Read the run in the Actions tab#

Open the repository on GitHub and click Actions. The left column lists workflows, the main list shows runs. Click a run to see its jobs as a graph, then a job to see each step. Click a step to expand its log. A red mark shows the step that failed, and the lines just above the failure usually name the problem. Use Re-run failed jobs after a fix to your settings, and Re-run all jobs when you suspect a flaky step. For debugging, add run: env | sort carefully, since it can print values you meant to keep private. See the secrets lesson before doing that.

Check it worked#

Push the minimal file above. In the Actions tab you should see a run named after your commit message, with one job called hello and a green check. Expand the Show the commit step and confirm the log prints your latest commit. Then click Run workflow on the workflow page to test the manual trigger.

Edge cases and gotchas#

  • Indentation is syntax. YAML uses spaces, never tabs. A wrong indent under steps gives a parse error or a silently different meaning.
  • Quote cron and version strings. Unquoted 20.10 can be read as the number 20.1. Quote values that must stay text.
  • Workflows from forks. A pull request from a fork runs with a read-only token and no secrets. See the next lessons on testing and secrets.
  • Default branch rules. schedule and some workflow_dispatch behavior depend on the workflow file existing on the default branch.
  • No shared disk between jobs. Files created in one job are gone in the next. Use artifacts or put the work in one job.
  • Action versions. @v4 follows the v4 line. Pinning to a full commit SHA is stricter if you want to avoid moving tags.

Troubleshooting#

SymptomLikely causeFix
Workflow does not appear in the Actions tabFile is not in .github/workflows or has a YAML errorMove it, and check the file for tabs or bad indents
Run never triggers on pushbranches filter does not matchCompare the filter with the branch you pushed
Invalid workflow file with a line numberYAML syntax errorFix the indent or quoting on the named line
fatal: not a git repository or missing filesNo checkout stepAdd actions/checkout@v4 first
Resource not accessible by integrationToken lacks a permissionAdd the needed key under permissions
Job skippedneeds job failed or if was falseOpen the earlier job, or check the condition
Unable to resolve actionTypo in owner, name or versionCheck the action name and that the tag exists

Quick reference#

yaml
on: [push, pull_request, workflow_dispatch]
permissions:
  contents: read
jobs:
  name:
    runs-on: ubuntu-latest
    needs: other-job
    if: github.ref == 'refs/heads/main'
    env:
      KEY: value
    steps:
      - uses: actions/checkout@v4
      - run: echo "hello"

Next#

Continue with Run tests on every push and pull request.

Check what you learned4 questions
1Where must a workflow file live to be picked up?
2What does needs: build do on a job?
3Which step key runs a shell command?
4Why set permissions explicitly in a workflow?

Frequently asked questions

Where do GitHub Actions workflow files go?

In the .github/workflows directory at the root of the repository, as .yml or .yaml files. GitHub reads every file there and runs the ones whose on events match.

What does runs-on ubuntu-latest mean?

It asks GitHub to run the job on a fresh hosted Ubuntu virtual machine. The machine is discarded when the job ends, so nothing persists between runs unless you cache or upload it.

What is the difference between uses and run in a step?

uses runs a reusable action, such as actions/checkout@v4. run executes shell commands on the runner.

How do I run a workflow manually?

Add workflow_dispatch to the on section. A Run workflow button then appears on the workflow page in the Actions tab.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios