CI/CD

A Publish Job Is Not a Rollout

5 min read

A pipeline can finish with a green job called deploy and leave the cluster exactly as it was, because publishing pushes an image to the registry and only a separate job changes the Deployment that runs it. That separate job is a rollout, and it gets its own stage, its own environment, and its own lock.

A rollout stage

GitLab places a job by its stage keyword, not by its name. A job that omits stage runs in test, which is the default, so a job named deploy with no stage runs alongside the tests. The rollout therefore needs a stage of its own, added after publish in the stages: list of .gitlab-ci.yml:

stages: [build, test, publish, rollout]

The first three stages are the ones from Pipelines for Branches and Merge Requests, so rollout comes last in the pipeline, and the job below ties itself to the publish job with needs.

The manual rollout job

The job goes in the same .gitlab-ci.yml:

rollout:
  stage: rollout
  image: registry.example.com/tools/kubectl:1.37
  needs: [publish]
  environment:
    name: production
    url: https://app.example.com
  resource_group: production
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      when: manual
  script:
    - >
      kubectl set image deployment/api
      api=registry.example.com/api:$CI_COMMIT_SHORT_SHA
    - kubectl rollout status deployment/api --timeout=180s

stage: rollout and needs: [publish] put the job after the image exists. The environment block makes GitLab record each run as a deployment to production, with a link to https://app.example.com on the environment page, and GitLab records an environment only for a job that sets this keyword.

resource_group: production makes runs one at a time. When two pushes to the default branch both start a rollout, the second job waits until the first finishes, so two set image calls cannot overlap on the same Deployment.

The rule adds the job only to pipelines on the default branch, and when: manual turns it into a button there. Other branches get no rollout job at all, which is why the manual setting sits inside the rule instead of on the job, where it would draw a button on every branch pipeline.

A manual job added by a rule is not optional. GitLab gave this one allow_failure: false, so every default-branch pipeline stops at the button and reports itself as blocked, not passed, until someone runs the rollout. That is the point when the button is the release decision, but a merge request widget or a status badge that watches the default branch shows blocked after every merge. Adding allow_failure: true to the job lets the pipeline pass with the button still waiting, at the cost of a green pipeline for a commit that never reached production.

The script then does two things in order. kubectl set image points the api container at the image tagged with this commit, as The Image Tag Is the Commit describes, and kubectl rollout status waits up to 180 seconds for the new pods to report ready. The second command is what makes the job's result mean something, because it fails the job when the rollout does not finish in time.

Both commands need a kubectl binary and a way into the cluster, which the job gets from outside the script. image: names an image with kubectl and a shell, pinned in your own registry to the cluster's minor version: 1.37 here, for a k3s cluster that reports v1.37.2+k3s1, and Kubernetes supports kubectl only within one minor version of the server, so the pin moves when the cluster is upgraded. The credentials come from a protected CI/CD variable of type File named KUBECONFIG: GitLab writes its value to a temporary file and sets the variable to that path, which is where kubectl already looks, so the script never mentions it. Give that kubeconfig a service account whose Role covers only the api Deployment's namespace, and keep the variable protected so only the default branch can read it.

One writer for the running tag

kubectl set image changes the running Deployment without writing the tag anywhere else. That suits a cluster that only this job applies, but in a cluster managed from Git the next reconcile puts back whatever tag the manifest holds. In that setup the rollout should write the tag into the manifest and let the GitOps tool apply it, which is the design in Build and Apply Are Separate Jobs and The Cluster Runs the Tag in Git.

What the rollout relies on

The job assumes the database schema is already ready for the new API. A migration Job has to succeed before this rollout, otherwise the new pods start against the old schema, and A Migration Job That Finishes First is that step.

Going back is a separate concern. kubectl rollout undo returns to the previous ReplicaSet while Kubernetes still keeps it, which by default is the last ten, the revisionHistoryLimit every application Deployment in the cluster above reported, but the script itself does not record the tag it replaced. If you need that tag after the old ReplicaSet is gone, write it to the job log or the environment description before set image runs. Undoing an image also leaves the data where it is, so bringing SQL Server back to an earlier state is a restore from backup, and Roll Back the Image and Restore the Disk covers both paths.

What GitLab made of the file

The stages, workflow, build, test, and publish jobs from Pipelines for Branches and Merge Requests and the rollout job above were sent to GitLab's CI lint API as one .gitlab-ci.yml, with a dry run that simulates creating a pipeline on a ref:

Ref, or change Jobs GitLab created
The default branch build, test, publish, and rollout in stage rollout, when: manual, environment production, allow_failure: false
Another branch build and test only
A deploy job added with no stage deploy in stage test, beside the tests

The file was valid, with no warnings. Running the rollout against a cluster was not part of this check.

What remains is a manual job that sets one image on one Deployment and waits for rollout status to report it ready. Publishing that image happened earlier in publish, and restoring SQL Server is outside this pipeline entirely.

Back to articles
Share X LinkedIn
An unhandled error has occurred. Reload 🗙