# 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.

URL: https://levelrail.com/learn/ci-cd/github-actions-workflow-anatomy/
Updated: 2026-11-27

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](https://levelrail.com/learn/ci-cd/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](https://levelrail.com/learn/ci-cd/cache-dependencies-in-github-actions/)), [matrix builds](https://levelrail.com/learn/ci-cd/matrix-builds-and-branch-protection/)) and [deploying over SSH](https://levelrail.com/learn/ci-cd/deploy-over-ssh-from-github-actions/)). 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](https://levelrail.com/learn/glossary/#secret)) there. Secrets come from the `secrets` context and are covered in [Secrets and variables in CI pipelines](https://levelrail.com/learn/ci-cd/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

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| Workflow does not appear in the Actions tab | File is not in .github/workflows or has a YAML error | Move it, and check the file for tabs or bad indents |
| Run never triggers on push | `branches` filter does not match | Compare the filter with the branch you pushed |
| `Invalid workflow file` with a line number | YAML syntax error | Fix the indent or quoting on the named line |
| `fatal: not a git repository` or missing files | No checkout step | Add `actions/checkout@v4` first |
| `Resource not accessible by integration` | Token lacks a permission | Add the needed key under `permissions` |
| Job skipped | `needs` job failed or `if` was false | Open the earlier job, or check the condition |
| `Unable to resolve action` | Typo in owner, name or version | Check 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"
```

## Related lessons

- [CI/CD pipeline stages explained](https://levelrail.com/learn/ci-cd/ci-cd-pipeline-stages-explained/)): the stages these jobs implement.
- [Run tests on every push and pull request](https://levelrail.com/learn/ci-cd/run-tests-on-every-push-and-pull-request/)): a real Node workflow.
- [Secrets and variables in CI pipelines](https://levelrail.com/learn/ci-cd/secrets-and-variables-in-ci-pipelines/)): what to do instead of env for sensitive values.
- [Your first Git repository](https://levelrail.com/learn/git/your-first-git-repository/)): the repository the workflow lives in.
- [Git workflow that triggers a deploy](https://levelrail.com/learn/git/git-workflow-that-triggers-a-deploy/)): branches and pushes as triggers.

## Next

Continue with [Run tests on every push and pull request](https://levelrail.com/learn/ci-cd/run-tests-on-every-push-and-pull-request/)).

## 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

- [GitHub Docs, workflow syntax for GitHub Actions](https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-syntax)
- [GitHub Docs, events that trigger workflows](https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows)
