Sub topic 1
The shape of a push-based deployment pipeline
A push-based pipeline is a CI/CD setup where an outside system, here a GitHub Actions runner, pushes a change into the cluster. This chapter gives you the overall shape. Later chapters build each stage in detail.
Prerequisites
This chapter assumes you already know:
- Basic Git and GitHub usage, including branches and pull requests.
- Docker images and registries, and how a tag points to an image.
- Core Kubernetes objects: Pod, Deployment, Service, and Namespace.
- Basic
kubectlusage. - How to read YAML.
The three stages
Every push-based pipeline in this tutorial has the same three stages.
- Build. The workflow checks out the code and builds a container image.
- Push. The workflow uploads the image to a registry, tagged so you can identify it later.
- Deploy. The workflow authenticates to the cluster and tells the Deployment to use the new image. It then waits to see whether the rollout succeeds.
The workflow runs these stages when something happens in the repository, usually a push to main. The cluster does nothing until the workflow reaches it. That is what "push-based" means: the pipeline initiates the change.
What the workflow looks like
The example below is the full pipeline in one file. Do not worry about every line yet. Notice that it has two jobs, and that the deploy job waits for build with needs.
name: deploy
on:
push:
branches: [main]
permissions:
contents: read
packages: write
env:
IMAGE: ghcr.io/${{ github.repository }}
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ env.IMAGE }}:${{ github.sha }}
deploy:
needs: build
runs-on: ubuntu-latest
environment: production
concurrency:
group: deploy-production
cancel-in-progress: false
steps:
- name: Write kubeconfig
env:
KUBE_CONFIG: ${{ secrets.KUBE_CONFIG }}
run: |
mkdir -p "$HOME/.kube"
printf '%s' "$KUBE_CONFIG" > "$HOME/.kube/config"
chmod 600 "$HOME/.kube/config"
- name: Update image
run: kubectl set image deployment/web web="$IMAGE:$GITHUB_SHA" -n shop
- name: Wait for rollout
run: kubectl rollout status deployment/web -n shop --timeout=120sThree design choices in this file shape the rest of the tutorial:
- The image tag is the commit SHA, so every image maps to exactly one commit.
- The
deployjob uses aproductionenvironment, which can require approval before it runs. - The last step fails the job if the rollout does not finish, so a broken release shows up as a red build.
Where the risk sits
The deploy job holds a credential that can change your cluster. That makes the pipeline a target. Most of the remaining chapters reduce that risk.
| Concern | How the pipeline addresses it |
|---|---|
| Credential is stored long term | Use OIDC federation for short-lived credentials |
| Credential can do too much | Bind it to a namespaced Role |
| Two deploys collide | Use a concurrency group |
| A release is bad | Check rollout status, then run `kubectl rollout undo` |
| Anyone can deploy to production | Gate the job with an environment |
Push-based versus pull-based
In a pull-based model, such as GitOps with Argo CD or Flux, an agent inside the cluster watches a Git repository and applies changes itself. The CI system never needs cluster credentials. Push-based pipelines are simpler to set up and easy to read from top to bottom, but they require you to give the CI system access to the cluster. The final chapter returns to this trade-off.
Exercise
In the workflow above, why does the deploy job declare needs: build, and what happens to deploy if build fails?
Show answer
needs: build makes deploy wait until build finishes successfully, so the image exists in the registry before the cluster tries to pull it. If build fails, deploy is skipped and nothing changes in the cluster.
Exercise
Name the three stages of the pipeline and say what makes it "push-based".
Show answer
The stages are build, push, and deploy. It is push-based because the CI runner initiates the change by authenticating to the cluster and updating the Deployment, instead of an agent in the cluster pulling changes from Git.