Pipeline as Code
In the early days of CI, pipelines were configured by clicking through a server's web UI. Nobody could review a change, nobody knew who broke the build configuration, and a lost server meant a lost pipeline. Today the pipeline is a file in the repository:
| Platform | File |
|---|---|
| GitHub Actions | .github/workflows/*.yml (one file per workflow) |
| GitLab CI | .gitlab-ci.yml (plus included files) |
| Jenkins | Jenkinsfile (Groovy DSL) |
| Azure Pipelines | azure-pipelines.yml |
| Bitbucket Pipelines | bitbucket-pipelines.yml |
What you get from it
- Review. A change to the pipeline is a pull request, with a diff and an approver.
- History.
git log .gitlab-ci.ymltells you when the security scan was removed, and by whom. - Branches. A feature branch can change its own pipeline (add a job for a new service) without affecting
main. - Reproducibility. Checking out an old commit gives you the pipeline that built it.
And one responsibility: whoever can change the pipeline file can run code with the pipeline's credentials. That is why deployment secrets belong to protected environments and protected branches, never to "any job in the repository" (Secrets & Variables, Environments).
Keep the logic in scripts, keep the YAML thin
The most useful habit in this whole course: the pipeline file decides when and where things run; scripts in the repository decide what runs. A make test or ./ci/build.sh works the same on a laptop, in GitHub Actions and in GitLab CI, and you can debug it without pushing commits.
mkdir shop && cd shop
cat > Makefile <<'MK'
.PHONY: lint test build ci
lint:
python3 -m py_compile app.py
test:
python3 -c 'from app import greet; assert greet("ci") == "hello, ci"; print("tests ok")'
build:
mkdir -p dist && cp app.py dist/ && echo "built dist/app.py"
ci: lint test build
MK
cat > app.py <<'PY'
def greet(name):
return f"hello, {name}"
PY
make cipython3 -m py_compile app.py
python3 -c 'from app import greet; assert greet("ci") == "hello, ci"; print("tests ok")'
tests ok
mkdir -p dist && cp app.py dist/ && echo "built dist/app.py"
built dist/app.py
The GitHub Actions job and the GitLab job that run this are then almost identical, and almost empty:
# .github/workflows/ci.yml
name: ci
on: [push, pull_request]
permissions:
contents: read
jobs:
ci:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v7
- run: make ci# .gitlab-ci.yml
ci:
image: python:3.13
script:
- make ciYAML pitfalls that break pipelines
Pipeline files are YAML, and YAML has traps (the Automation Scripting course covers YAML in depth). The ones that most often break CI:
| Trap | Example | Result |
|---|---|---|
| unquoted special values | node-version: 20.10 |
the float 20.1; quote versions: '20.10' |
on as a key in YAML 1.1 parsers |
on: |
some tools read it as true; GitHub reads it correctly, but linters warn |
: inside a command |
- echo key: value |
a mapping, not a string; quote the whole line |
| tabs | indentation with tabs | a parse error |
| anchors across files | <<: *defaults in an included file |
anchors don't cross files; use extends (GitLab) or reusable workflows |
The model shows the first and third traps with a minimal YAML-like reading of scalars (the same rules YAML parsers use for plain scalars):
import re
def plain_scalar(text):
"""How a YAML 1.2 core-schema parser types an unquoted value."""
if re.fullmatch(r"[-+]?[0-9]+", text):
return int(text)
if re.fullmatch(r"[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?", text):
return float(text)
if text in ("true", "false"):
return text == "true"
if text in ("null", "~", ""):
return None
return text
for raw in ["20.10", "'20.10'", "3.10", "24", "true", "echo hello"]:
value = raw[1:-1] if raw.startswith("'") else plain_scalar(raw)
print(f"python-version: {raw:<12} -> {value!r} ({type(value).__name__})")
line = "echo key: value"
print("a list item `- " + line + "` contains ': ', so YAML reads a mapping:", {line.split(": ")[0]: line.split(": ")[1]})python-version: 20.10 -> 20.1 (float)
python-version: '20.10' -> '20.10' (str)
python-version: 3.10 -> 3.1 (float)
python-version: 24 -> 24 (int)
python-version: true -> True (bool)
python-version: echo hello -> 'echo hello' (str)
a list item `- echo key: value` contains ': ', so YAML reads a mapping: {'echo key': 'value'}
3.10 becoming 3.1 has sent many matrices to the wrong Python version. Quote every version number in pipeline files.
Test yourself
Why keep pipeline logic in scripts or a Makefile rather than in long run: blocks?
- YAML can't contain shell
- The same command runs locally and on any CI platform, and can be debugged without pushing
- Scripts are faster
- CI platforms forbid long steps
python-version: 3.10 (unquoted) in a matrix runs which version?
- 3.10
- 3.1, because YAML reads the value as a float
- The latest 3.x
- It fails to parse
What new risk comes with pipeline as code?
- None
- Anyone who can change the pipeline file on a branch can run code with that pipeline's credentials
- Pipelines can't be reviewed
- Secrets are stored in the file