Git CI/CD
Continuous Integration (CI) means every push is automatically built and tested, so problems are found minutes after they are introduced instead of weeks later. Continuous Delivery/Deployment (CD) goes further: changes that pass are automatically packaged and released.
Git is the trigger for all of it: a push, a new pull request or a tag starts a pipeline on a CI service.
The services
| Service | Configuration file |
|---|---|
| GitHub Actions | .github/workflows/*.yml |
| GitLab CI/CD | .gitlab-ci.yml |
| Bitbucket Pipelines | bitbucket-pipelines.yml |
| Jenkins | Jenkinsfile |
| CircleCI | .circleci/config.yml |
The configuration is committed in the repository, so it is versioned and reviewed like code.
A GitHub Actions workflow
# .github/workflows/ci.yml
name: CI
on:
push:
branches: [main]
pull_request: # every PR gets a check
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python: ["3.12", "3.13"]
steps:
- uses: actions/checkout@v4 # git clone of the pushed commit
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python }}
- run: pip install -r requirements.txt
- run: pytest
deploy:
needs: test # only after tests pass
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: ./deploy.sh
env:
API_TOKEN: ${{ secrets.API_TOKEN }}| Concept | Meaning |
|---|---|
trigger (on:) |
which Git events start the workflow: push, pull_request, tag, schedule |
| job | a group of steps on one fresh virtual machine; jobs run in parallel unless needs: orders them |
| step | one command (run:) or reusable action (uses:) |
| matrix | run the same job for several versions or platforms |
| secrets | encrypted values (tokens, passwords) injected at run time, never committed |
| status check | the green tick or red cross shown on the commit and PR |
What a CI runner does, step by step
A CI job starts from a clean clone of exactly the pushed commit, not from anyone's working folder. That is why it catches "works on my machine" problems such as a file you forgot to commit. This script imitates a runner:
git init -q --bare ~/github/calc.git
mkdir calc && cd calc && git init -q
cat > calc.sh <<'EOF'
add() { echo $(( $1 + $2 )); }
EOF
cat > test.sh <<'EOF'
set -e
source ./calc.sh
source ./helpers.sh # oops: helpers.sh exists only on the developer's laptop
[ "$(add 2 3)" = 5 ]
log "test add: ok"
EOF
echo 'log() { echo "[log] $*"; }' > helpers.sh
git add calc.sh test.sh && git commit -q -m "Add calculator and tests"
git remote add origin ~/github/calc.git && git push -q -u origin main
echo "== on the laptop:"; bash test.sh
# --- the CI runner: fresh clone of the pushed commit, run the tests ---
ci() {
rm -rf ~/runner && git clone -q ~/github/calc.git ~/runner && cd ~/runner
echo "== CI for $(git rev-parse --short HEAD):"
if bash test.sh 2>&1; then echo "status: success"; else echo "status: failure"; fi
cd ~/calc
}
ci
git add helpers.sh && git commit -q -m "Add missing helpers" && git push -q
ci== on the laptop:
[log] test add: ok
== CI for cb4ba8f:
test.sh: line 3: ./helpers.sh: No such file or directory
status: failure
== CI for d4ecc6f:
[log] test add: ok
status: success
The laptop passed because helpers.sh was there, untracked. The clean CI clone failed until the file was committed.
Gating merges
On GitHub, branch protection rules (or rulesets) for main can require that status checks pass and that a reviewer approves before a PR can be merged. Combined with CI, broken code can't reach main.
The same idea can be enforced by the Git server itself with a pre-receive hook, a script on the server that can reject a push. Hosted services don't let you install these, but self-hosted servers (and this example) do:
git init -q --bare ~/server/app.git
cat > ~/server/app.git/hooks/pre-receive <<'EOF'
#!/bin/bash
# reject pushes to main whose newest commit message contains "WIP"
while read old new ref; do
if [ "$ref" = refs/heads/main ] && git log -1 --format=%s "$new" | grep -q WIP; then
echo "rejected: no WIP commits on main"
exit 1
fi
done
EOF
chmod +x ~/server/app.git/hooks/pre-receive
mkdir app && cd app && git init -q
echo "v1" > app.txt && git add . && git commit -q -m "WIP half-done feature"
git remote add origin ~/server/app.git
git push -q origin main
echo "push exit status: $?"
git commit -q --amend -m "Add feature"
git push -q origin main && echo "push accepted"remote: rejected: no WIP commits on main
To /home/learner/server/app.git
! [remote rejected] main -> main (pre-receive hook declined)
error: failed to push some refs to '/home/learner/server/app.git'
push exit status: 1
push accepted
Output from server-side hooks is shown to the pusher prefixed with remote:.
Releases from tags
A common CD pattern: pushing a tag like v1.4.0 triggers a workflow that builds the release, attaches binaries and publishes packages.
on:
push:
tags: ["v*"]Test yourself
Why does CI start from a fresh clone instead of a developer's folder?
- It's faster
- It tests exactly what is committed, catching missing files and local-only setup
- Developers' folders are private
- Git requires it
Where do GitHub Actions workflows live?
- In the GitHub settings only
- In .github/workflows/*.yml inside the repository
- In .git/hooks
- In package.json
How should a deployment token be provided to a CI job?
- Committed in the workflow file
- As an encrypted secret of the repository/organization
- In the README
- Hard-coded in the script