Understanding What Goes into a GitHub Actions Workflow

Contents

Introduction

When you join a project whose environment is already fairly set up, I think you can end up in a state like this:

"When I open a PR on GitHub, some tests and checks run automatically, but honestly I don't really understand them..."

What it looks like (https://github.com/microsoft/vscode ) A GitHub pull request merge box showing Review required and All checks have passed, with a list of successful Code OSS checks

In this post, using a minimal workflow as the example, I organize:

  • "which files" GitHub Actions runs and "in what order"
  • points to look at when reading an existing workflow.yml from top to bottom
  • parts that are fine to change and parts to handle carefully

The files GitHub Actions looks at

Only YAML files (.yml / .yaml) placed under .github/workflows/ at the repository root are recognized as workflows.

.github/workflows/*.yml

Note

In a monorepo, a yml file created under each project's folder is not recognized.

The overall structure of a workflow

A GitHub Actions workflow definition file has a hierarchy like this.

  • workflow (the whole file)
    • trigger (on)
    • shared settings (env / concurrency, etc.)
    • jobs
      • job
        • job settings
        • steps
          • step

This post explains the file below from top to bottom, in order of the hierarchy.

name: CI

run-name: Run #${{ github.run_number }} by ${{ github.actor }}

on:  
  push:  
    branches: [main]  
  pull_request:  

env:
  NODE_ENV: test

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

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

name

name: CI
  • The name shown in the GitHub Actions UI
  • The name defined here is shown, not the file name of the yml file
A pull request merge box where the single check is listed as CI / typecheck (pull_request)

run-name

run-name: Run
  • The display name of a GitHub Actions "workflow run" (one run)
  • It is shown as the title in places like the Actions tab

The Actions tab listing a workflow run titled Run, with CI #2 below it

on

on:  
  push:  
    branches: [main]  
  pull_request:  
  • Defines when this workflow runs
    • push:branches: [main] → runs when something is pushed to the main branch
    • pull_request → runs when a PR is created or updated

The events you can use are listed here. https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows

env

env:
  NODE_ENV: test
  • Defines environment variables you can use in a GitHub Actions workflow/job/step
  • Specified in the form key:value

concurrency

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true
  • Controls workflows / jobs with the same concurrency.group running at the same time

jobs

jobs:
  typecheck:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
      - run: npm ci
  • The unit of execution in GitHub Actions
  • Jobs run in parallel by default
  • Each job runs on its own independent runner (execution environment)
  • typecheck is the name of the job
The Checks tab of a pull request showing the CI workflow (on: pull_request) with the typecheck job

runs-on

runs-on: ubuntu-latest
  • Defines the runner (execution environment) the job runs on

steps

steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
      - run: npm ci
  • Steps run in series by default
  • uses
    • Used to call an existing "Action" (a building block)
    • Common operations such as checkout are defined as ready-made Actions you can use
    • The execution environment depends on how the Action is implemented
  • run
    • Used to run commands in a shell
    • Runs as shell commands on the runner specified for the job

Wrapping up

In this post, I read through a GitHub Actions workflow.yml from top to bottom, based on a minimal setup you often see in real work.

There are many other items you can define, so take a look at the page below. https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-syntax

Translated from the Japanese original.

Thanks. I'll look into it.

Spotted a mistake?

Tell me what's wrong and I'll fix it. Only I see what you send.