Skip to content
Visual explainer 3h to learn 5 steps

Workflows, events, jobs & steps

Write a CI workflow that runs tests on push and pull_request.

By Invicly Tech, , in Git, GitHub & GitHub Actions. About 11 minutes to read.

Topic 9 of 18

In short

A workflow is a YAML file in .github/workflows. Events trigger it, it contains jobs that run on runners, and each job is a list of steps that run commands or reusable actions.

Explained simply

GitHub Actions is a robot that does work for you whenever something happens in your repo. You describe the work in a YAML file inside .github/workflows. When the event you chose happens, such as a push or a new pull request, GitHub starts a fresh machine and follows your instructions.

The file has a simple shape. on: says which events start it. jobs: lists the pieces of work. Each job says which kind of machine to use (runs-on) and lists steps. A step either runs shell commands (run:) or uses a ready-made action from the Marketplace (uses:), like actions/checkout, which downloads your code onto the machine.

The machine is called a runner. GitHub gives you a clean one for every job and throws it away afterwards. That is great for reliability, because nothing is left over from yesterday, but it also means jobs cannot share files on disk. You pass data between them on purpose.

Every run shows up on the Actions tab with live logs, and on pull requests as a green tick or red cross. Most teams start with one workflow: on every pull request, install dependencies and run the tests. That alone catches a huge number of bugs before they reach main.

Press Play on a diagram to walk through it one step at a time.

MapAnatomy of a workflow
.github/workflows/ci.yml
  • on: events
    • push
    • pull_request
    • workflow_dispatch
    • schedule
  • job: test
    • runs-on: ubuntu-latest
    • step: actions/checkout
    • step: run tests
  • job: build
    • needs: test
    • step: build package
SequenceA push to a pull request branch
Developer
GitHub
Runner
  1. 1git push to feature branch
    Developer to GitHub: git push to feature branch
  2. 2pull_request synchronize matches ci.yml
    GitHub to GitHub: pull_request synchronize matches ci.yml
  3. 3Job assigned with a fresh token
    GitHub to Runner: Job assigned with a fresh token
  4. 4Checkout, install, test; stream logs
    Runner to GitHub: Checkout, install, test; stream logs
  5. 5Job result: success
    Runner to GitHub: Job result: success
  6. 6Green check on the pull request
    GitHub to Developer: Green check on the pull request

In depth

Events and filters

push runs when commits are pushed, and you can limit it with branches, tags and paths filters, for example only when files in src/ change. pull_request runs by default for the activity types opened, synchronize (new commits pushed) and reopened. It tests the merge result: GitHub checks out refs/pull/N/merge, a temporary merge of the PR into its base, so you test what main would look like after merging.

workflow_dispatch adds a Run workflow button with typed inputs. schedule takes cron syntax in UTC; the shortest interval is every 5 minutes, runs can be delayed when GitHub is busy, and scheduled workflows only run from the default branch. In public repos, scheduled workflows are disabled automatically after 60 days without repository activity.

  • pull_request from a fork gets a read-only token and no secrets
  • A paths-ignore filter skips runs when only docs files change
  • workflow_dispatch and schedule workflows must exist on the default branch

What happens when a run starts

When an event fires, GitHub reads the workflow files at that commit, checks each file's on: section and filters, and creates a run for every match. Jobs with no needs are queued at once; the rest wait for their dependencies. A runner with matching labels picks up each job.

For every job, GitHub creates a fresh GITHUB_TOKEN with the permissions you set, downloads the actions the job uses, and then runs the steps in order. If a step fails, later steps are skipped unless they use if: always() or failure(). A job on a GitHub-hosted runner may run for at most 6 hours; the default timeout-minutes is 360, and setting a lower one stops a stuck job from burning minutes.

FlowFrom event to green tick
  1. 1
    Event fires

    push, pull_request, schedule, manual

  2. 2
    Match workflows

    on: section and filters at that commit

  3. 3
    Queue jobs

    needs decides the order

  4. 4
    Runner picks up job

    Fresh VM and a new GITHUB_TOKEN

  5. 5
    Run steps

    In order; a failure skips the rest

  6. 6
    Report

    Logs, check status on commit and PR

Expressions, contexts and conditions

Anything inside ${{ }} is an expression, evaluated before the step runs. Contexts are the objects you can read: github (event, ref, sha, actor), env, vars, secrets, inputs, matrix, steps, needs, runner and job. Functions include contains, startsWith, format, toJSON, fromJSON and hashFiles.

if: conditions are expressions too, and the ${{ }} is optional there: if: github.ref == 'refs/heads/main'. Every step and job has an implicit success() check. Use always() to run cleanup no matter what, failure() to send an alert only when something broke, and cancelled() to react to cancellations. One more surprise: each run: step is a new shell process, so a cd or a shell variable does not carry over to the next step.

  • Default shell on Linux and macOS: bash -e (stops on the first failing command)
  • shell: bash adds -o pipefail, so a failing command in a pipe also fails the step
  • defaults: run: working-directory: app sets the folder for every run step

Passing data between steps and jobs

Inside a job, steps share the workspace, so files written by one step are there for the next. For values, write name=value lines to the file in $GITHUB_OUTPUT and read them as steps.ID.outputs.name. To set an environment variable for all later steps, append NAME=value to $GITHUB_ENV. To add a folder to PATH, append it to $GITHUB_PATH. Markdown appended to $GITHUB_STEP_SUMMARY appears on the run's summary page.

Between jobs, map step outputs to job outputs, then read them in another job as needs.JOB.outputs.name. Job outputs are strings, limited to 1 MB per job, and are meant for small values like a version or a tag. For files, upload an artifact in one job and download it in the next.

MapWhere data can travel
A workflow run
  • Step to step (same job)
    • Files in the workspace
    • $GITHUB_OUTPUT values
    • $GITHUB_ENV variables
    • $GITHUB_PATH entries
  • Job to job
    • Job outputs via needs
    • Artifacts for files
  • Run to run
    • Caches
    • Artifacts by run ID
    • Releases and packages
  • To people
    • Logs
    • $GITHUB_STEP_SUMMARY
    • Check status on the PR

Learn it in 5 short steps

About 35m each. Track them with a timer in your plan.

  1. 1
    Learn workflows, jobs, steps and runners~35m
  2. 2
    Trigger on push and pull_request~35m
  3. 3
    Use marketplace actions like checkout~35m
  4. 4
    Pass data between steps with outputs~35m
  5. 5
    Write a test workflow for a project~35m

Key points

  • Jobs run in parallel on separate runners unless linked with needs.
  • Steps in one job share the same machine and workspace, in order.
  • uses runs a published action; run executes shell commands.
  • Write step outputs to $GITHUB_OUTPUT and read them via steps.id.outputs.

Key terms

Tap a card to flip it. 0/5 known

Examples

yaml
name: ci
on:
  push: { branches: [main] }
  pull_request:
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with: { node-version: 22 }
      - run: npm ci && npm test
      - id: ver
        run: echo "version=$(jq -r .version package.json)" >> "$GITHUB_OUTPUT"
      - run: echo "Tested ${{ steps.ver.outputs.version }}"

Test on push to main and on PRs, then pass a step output.

Compute a version once, use it in two jobs

Your build job and your docker job both need the same version string. Step 1: a version job writes it to $GITHUB_OUTPUT. Step 2: the job maps that step output to a job output. Step 3: the other jobs declare needs: version and read needs.version.outputs.tag. Both later jobs run in parallel and are guaranteed to use the same value.

yaml
jobs:
  version:
    runs-on: ubuntu-latest
    outputs:
      tag: ${{ steps.v.outputs.tag }}
    steps:
      - id: v
        run: echo "tag=1.0.${{ github.run_number }}" >> "$GITHUB_OUTPUT"
  build:
    needs: version
    runs-on: ubuntu-latest
    steps:
      - run: echo "Building ${{ needs.version.outputs.tag }}"
  docker:
    needs: version
    runs-on: ubuntu-latest
    steps:
      - run: echo "Tagging image ${{ needs.version.outputs.tag }}"

Stop wasting minutes on outdated runs

Developers push to a PR five times in ten minutes, and five CI runs pile up. Step 1: add a concurrency group per workflow and branch. Step 2: set cancel-in-progress so a new push cancels the older run on the same branch. Step 3: add a timeout so a hung test cannot run for 6 hours. Step 4: add workflow_dispatch so you can also start it by hand.

yaml
name: ci
on:
  pull_request:
  push: { branches: [main] }
  workflow_dispatch:
concurrency:
  group: ci-${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true
jobs:
  test:
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v4
      - run: make test

Quick reference

  • pull_request default types: opened, synchronize, reopened
  • echo "name=value" >> "$GITHUB_OUTPUT" sets a step output; read it as steps.ID.outputs.name
  • echo "NAME=value" >> "$GITHUB_ENV" sets an env var for all later steps in the job
  • if: always() runs a step even after failures; if: failure() only after one
  • timeout-minutes defaults to 360; set it lower on every job
  • concurrency with cancel-in-progress: true cancels older runs in the same group
  • schedule cron is UTC, runs only on the default branch, minimum every 5 minutes
  • Each run: step is a new shell; cd and shell variables do not carry over

Common mistakes

  • Placing the YAML outside .github/workflows, so it never runs.
  • Expecting files from one job to exist in another; jobs do not share disks.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1Which commit does a pull_request workflow test by default?

Question 2

2Step 1 runs cd app. Step 2 runs npm test. Where does npm test run?

Question 3

3How does job B read a value produced by a step in job A?

Question 4

4A cleanup step must run even if the tests failed. What do you add?

Question 5

5You add a schedule trigger on a feature branch, but it never runs. Why?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Write a workflow that runs on every pull request and on pushes to main, checks out the code and prints the event name and the commit SHA.

  2. Exercise 2medium

    Add a manual trigger with a choice input called level (debug or info) and print it. Also write a line to the job summary.

  3. Exercise 3hard

    Create two jobs, lint and test, that run in parallel, and a third job notify that runs only if either one failed.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Is GitHub Actions free?
Public repositories get free minutes on standard GitHub-hosted runners. Private repositories get a monthly allowance of minutes and storage that depends on the plan, then pay per minute.
What is the difference between an action and a workflow?
A workflow is your whole automation file with events and jobs. An action is a reusable building block, like actions/checkout, that you call from a step.
Why does my workflow not appear in the Actions tab?
The file must be in .github/workflows with a .yml or .yaml extension and valid YAML. If it has a syntax error, GitHub shows the error on the Actions tab instead of running it.
Can I run GitHub Actions locally?
Not officially. The community tool act runs many workflows in Docker on your machine, but it does not perfectly match GitHub-hosted runners.

Go deeper

ShareXLinkedInWhatsApp