Sub topic 7
Defining pipelines as code
This chapter assumes you know basic Git, can read simple YAML, and have seen a pipeline run (see the earlier chapters on pipeline anatomy and build artifacts). Here you learn what it means to keep the pipeline definition in the repository, why teams do it, and how to avoid the errors that come with it.
What "pipeline as code" means
Pipeline as code means the steps of your pipeline live in a text file, committed to the same repository as the application. The CI/CD service reads that file when a trigger fires and runs what it describes.
The alternative is a pipeline configured through a web interface. Someone clicks through forms to add a build step, set a timeout, or change a deploy target. The configuration lives inside the CI server. Nobody can see who changed it or why, and nobody can recreate it if the server is lost.
A file in the repository fixes these problems because it gets everything Git already provides.
| Property | Pipeline in a web UI | Pipeline as code |
|---|---|---|
| History | Often partial or missing | Full Git history, with author and message |
| Review | None, changes apply at once | Pull request review like any other change |
| Rollback | Reconstruct by hand | `git revert` |
| Reproducibility | Depends on server state | Recreated from the repository |
| Branch-specific changes | Hard | The file differs per branch |
| Copying to a new project | Manual re-entry | Copy the file |
Where the file lives
Each tool looks for its definition in a fixed location.
- GitHub Actions reads every YAML file in
.github/workflows/. - GitLab CI reads
.gitlab-ci.ymlat the repository root. - Jenkins reads a
Jenkinsfileat the repository root. - CircleCI reads
.circleci/config.yml.
These tools differ in syntax, but they share the same model: a trigger starts a pipeline, the pipeline contains jobs, and each job runs steps on a runner. The examples in this chapter use GitHub Actions.
Reading a pipeline file
Here is the basic workflow from the plan. Read it from the top down.
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 has four layers.
nameis a label shown in the interface.ondeclares the triggers. This workflow runs on every push tomainand on every pull request.jobslists independent units of work. This file has one job,test.stepsare the commands inside a job, run in order. A step either calls a reusable action withusesor runs a shell command withrun.
The runs-on key chooses the machine image. Every job starts on a fresh runner, so nothing carries over from one job to another unless you pass it explicitly. That is why the two-job example later in this chapter uploads and downloads an artifact.
Because the file is declarative, it describes what should happen and leaves the scheduling to the service. You do not write code that starts the runner or watches the exit codes. A step that exits with a non-zero code fails the job, and a failed job fails the pipeline.
Dependencies between jobs
By default, jobs in one workflow run in parallel. To order them, use needs. The second example from the plan builds, tests, and then deploys.
name: ci-cd
on:
push:
branches: [main]
jobs:
build:
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 test
- run: npm run build
- uses: actions/upload-artifact@v4
with:
name: dist
path: dist/
deploy:
needs: build
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
with:
name: dist
path: dist/
- run: ./scripts/deploy.sh dist/Three details matter here.
needs: buildmakesdeploywait forbuildand run only if it succeeds.- The
distartifact carries the built output from one runner to the other. The deploy job does not rebuild, so what you tested is what you ship. environment: productionlinks the job to an environment defined in the repository settings. You can require manual approval there. With an approval gate, this pipeline is continuous delivery. Without one, it is continuous deployment.
The script scripts/deploy.sh is a placeholder for your own deployment logic.
The diagram shows the order in which the jobs run.
Keeping the code in the pipeline small
A common mistake is writing long shell logic directly in YAML. YAML has no debugger, no unit tests, and awkward quoting rules. A better habit is to keep the pipeline thin and put the real work in scripts that you can also run on your laptop.
The deploy job above follows this rule. The YAML decides when and where to run, and scripts/deploy.sh decides how. You can test the script locally, and you can move to another CI service by rewriting only the thin YAML.
A related benefit is that developers can reproduce a failing step. If npm ci && npm test fails in CI, the same commands fail on a clean checkout locally.
Reuse and structure
As the number of repositories grows, copies of nearly identical pipeline files drift apart. Most tools offer ways to share definitions.
- Reusable workflows, in GitHub Actions, let one workflow call another with
uses:and inputs. - Templates and
include, in GitLab CI, pull shared fragments from another file or repository. - Shared libraries, in Jenkins, hold common Groovy functions.
Reuse reduces duplication, but it adds a dependency. If a shared definition changes, every pipeline that uses it changes too. Pin shared definitions to a version so that you control when each project upgrades.
The same applies to third-party actions. actions/checkout@v4 pins a major version. A tag can still move, so for actions from sources you do not fully trust, pin to a full commit SHA. Security practices for pipelines are covered in a later chapter.
Reviewing and testing pipeline changes
Because the pipeline is code, a change to it should go through a pull request. Reviewers should look at the following:
- Which triggers changed, since a broader trigger can run privileged jobs on code from unknown contributors.
- Whether new steps use secrets or add permissions.
- Whether a deploy job lost its
needsor itsenvironment, which would silently remove a gate.
You can check a pipeline change in three ways. First, open a pull request and let the workflow run on the branch, since pull requests are one of the triggers. Second, use a linter such as actionlint for GitHub Actions to catch syntax and expression errors before pushing. Third, run the underlying scripts locally.
# Install actionlint, then check every workflow in the repository
actionlint .github/workflows/*.ymlCommon errors
These problems appear often when teams first move to pipeline as code.
- Indentation mistakes. YAML is whitespace-sensitive. A
stepsentry indented one level too far changes the structure or produces a parse error. A linter finds these quickly. - Assuming state carries between jobs. Each job gets a clean runner. Files created in
buildare not indeployunless you upload and download them. - Forgetting
needs. Without it, jobs run in parallel, so a deploy can start while tests are still running. - Unquoted values that YAML reinterprets. A value like
node-version: 20.10is read as a number, which can turn20.10into20.1. Quote version strings that have a trailing zero:"20.10". - Triggers that are too broad. Running a deploy on every branch instead of
maincan release unreviewed code. - Environment drift. A pipeline that works because of tools preinstalled on a runner image can break when the image updates. Pin tool versions with setup actions, as
setup-nodedoes here. - Slow feedback. Running everything serially makes each commit wait. Split independent work into parallel jobs and use caching, such as
cache: npm.
Limits of the approach
Pipeline as code does not remove all configuration from the CI service. Secrets, approval rules, and runner settings usually live in the service's settings, not in the file, because they must not be committed. The file refers to them by name. Your pipeline is therefore reproducible only if you also document these settings, or manage them with infrastructure as code tools.
Also, the file shows what runs, but not always why a step exists. Add short comments to unusual steps, and keep the commit messages for pipeline changes descriptive.
Exercise
In the ci-cd workflow, a teammate deletes the line needs: build from the deploy job. Describe what changes when the workflow runs, and why the deploy job could also fail even in the best case.
Show answer
Without needs, the build and deploy jobs start in parallel. The deploy job no longer waits for the tests to pass, so it can run (and release) even when the tests fail. In the best case it still fails, because the dist artifact is not uploaded yet when download-artifact runs, and each job has its own clean runner with no shared files.
Exercise
Why is it better to put deployment logic in scripts/deploy.sh than to write it as many lines of shell inside the workflow YAML?
Show answer
A script can be run and tested locally, reviewed as normal code, and reused if you change CI tools. The YAML stays short and only decides when and where the script runs.