Sub topic 1
Anatomy of a workflow file
A GitHub Actions workflow is a YAML file that tells GitHub what to run and when. Every deployment pipeline in this tutorial is built from this structure, so it helps to know each part before adding build, test, and deploy steps.
Prerequisites
This chapter assumes you can:
- Use Git and GitHub: commit, push, create branches, and open pull requests.
- Read and write YAML, including indentation, lists, and mappings.
- Build and run your application from the command line.
Where workflow files live
GitHub reads workflows from the .github/workflows/ directory at the root of a repository. Each file ending in .yml or .yaml is one workflow. A repository can have many workflows, and each runs independently.
The file must be on the branch that triggers it. A workflow that exists only on a feature branch will not run for pushes to main.
The top-level keys
A workflow has three keys you will use in almost every file: name, on, and jobs.
name: CI
on:
push:
branches: [main]
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Hello from a workflow"nameis the label shown in the Actions tab. It is optional. If you omit it, GitHub shows the file path.onlists the events that start the workflow. Here a push tomainor any pull request starts it. The next chapter covers events in detail.jobsis a mapping of job IDs to job definitions. Here the only job ID istest.
Other top-level keys, such as permissions, env, and concurrency, appear in later chapters.
Jobs and steps
A job is a group of steps that run on one machine. The runs-on key picks that machine, called a runner. ubuntu-latest is a GitHub-hosted Linux runner.
A step is one unit of work. It does one of two things:
runexecutes a shell command.usesruns a prebuilt action, such asactions/checkout@v4, which copies your repository onto the runner.
Steps in a job run in order and share the runner's filesystem. Jobs run in parallel by default, and each gets a fresh runner. A job does not see files from another job unless you pass them explicitly, which the artifacts chapter covers.
How a run is structured
Each trigger event creates a workflow run. The run contains jobs, and each job contains steps.
Common errors
The same few mistakes cause most first failures.
| Symptom | Likely cause |
|---|---|
| The workflow never appears in the Actions tab | The file is not in `.github/workflows/`, or it is not on the default branch for events that need it |
| "Invalid workflow file" | Bad indentation or a misspelled key, such as `job:` instead of `jobs:` |
| The workflow does not run on a push | The branch filter in `on.push.branches` does not match the pushed branch |
| `command not found` in a step | The tool is not installed on the runner, so use a setup action first |
Exercise
A workflow has on: push and one job with two steps, run: ls and uses: actions/checkout@v4, in that order. The job runs but ls shows an empty directory. Why, and how do you fix it?
Show answer
Steps run in order, and the repository is only copied onto the runner by the checkout step. ls runs before checkout, so the directory is empty. Move actions/checkout@v4 to be the first step.