Sub topic 7
Defining pipelines as code
This chapter assumes you know basic Git, can read simple YAML, and have run tests for a small application from the command line. It covers what it means to keep a pipeline definition in the repository, how to read one, and the errors that most often break it.
What "pipeline as code" means
A pipeline definition is a text file that lists triggers, jobs, and steps. Pipeline as code means that file lives in the same repository as the application and changes through the same commits and pull requests.
The alternative is to click through a CI server's web interface to configure jobs. That setup has no history, no review, and no easy way to copy it to another project.
Keeping the definition in the repository gives you several benefits:
- Version history. You can see who changed a step and when, and you can revert a bad change.
- Review. A change to the pipeline goes through a pull request like any other change.
- Branch consistency. Each branch carries the pipeline that matches its code. An old release branch builds with its old steps.
- Reproducibility. A new repository or a new teammate starts from a working file.
Reading a definition
The example below is a GitHub Actions workflow. Other tools, such as GitLab CI and Jenkins, use different file names and syntax, but the same ideas apply.
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: 20
cache: npm
- run: npm ci
- run: npm testThe file is stored at .github/workflows/ci.yml. It has four parts:
namelabels the workflow in the interface.onlists the triggers. Here the workflow runs on every push tomainand on every pull request.jobsholds one or more jobs. This file has one job, calledtest.stepsis the ordered list of actions inside a job. Ausesstep runs a reusable action. Arunstep executes a shell command.
The job runs on a fresh virtual machine (runs-on: ubuntu-latest). Nothing from earlier runs is left over, so the steps must set up everything they need. That is why the file checks out the code and installs Node.js before running tests.
Pipelines are code, so treat them like code
Because the file is code, the usual engineering habits apply.
- Pin versions.
actions/checkout@v4pins a major version. An unpinned reference can change behavior without any commit in your repository. - Keep steps small and named. Short steps make failures easy to locate in the logs.
- Avoid duplication. Most tools let you share steps through reusable workflows, templates, or shared libraries.
- Test changes on a branch. Open a pull request and watch the run before merging.
Common errors
| Symptom | Likely cause |
|---|---|
| Workflow never starts | The file is in the wrong folder, or the trigger does not match the branch or event |
| The file is rejected | YAML indentation is wrong, or a key is misspelled |
| A command passes locally but fails in CI | The runner is a clean machine, so a tool, version, or environment variable is missing |
| A step is skipped or cannot find files | Each job gets its own machine, so files from one job are not available in another unless you pass them as artifacts |
Exercise
A teammate edits the pipeline in the CI server's web interface and the build breaks. Why does keeping the definition in the repository make this easier to fix?
Show answer
The change appears in the commit history and went through review, so you can see who changed what and revert it. A change made in a web interface usually leaves no such record.
Exercise
Your workflow has a build job that creates a dist/ folder and a deploy job that cannot find it. What is the cause, and what is the fix?
Show answer
Each job runs on its own fresh machine, so files do not carry over. Upload dist/ as an artifact in the build job and download it in the deploy job.