A publish job that tags every image 0.0.17 gives the registry one name for many builds. Each green pipeline pushes over the last one, and after that the tag says nothing about which commit is running. Tag the image with the commit, and the name in the cluster is one git show accepts.
What a fixed tag costs
A constant such as VERSION: "0.0.17" in .gitlab-ci.yml fails in two places, the registry and the node.
In the registry, the second push moves the tag to a new digest. The previous image is still stored until garbage collection, but no name selects it, so there is nothing to roll back to by tag.
On the node, the Deployment already says api:0.0.17, so its manifest has not changed and nothing rolls. Forcing a restart does not fix that. Kubernetes defaults imagePullPolicy to IfNotPresent for every tag except latest. A node that already holds api:0.0.17 starts the old image, and a node that has never seen it pulls the new one. Two pods of one Deployment can then run different code under the same name.
The publish job
This job goes in .gitlab-ci.yml. It builds the image with the .NET SDK, tags it with the short commit SHA, and keeps the digest of what it pushed.
publish:
stage: publish
image: mcr.microsoft.com/dotnet/sdk:10.0
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
variables:
DOTNET_CONTAINER_REGISTRY_UNAME: $REGISTRY_USER
DOTNET_CONTAINER_REGISTRY_PWORD: $REGISTRY_PASSWORD
script:
- dotnet publish Api/Api.csproj -c Release -t:PublishContainer
-p:ContainerRegistry=registry.example.com
-p:ContainerRepository=api
-p:ContainerImageTag=$CI_COMMIT_SHORT_SHA
--getProperty:GeneratedContainerDigest > digest.txt
artifacts:
paths: [digest.txt]
expire_in: 30 days
$CI_COMMIT_SHORT_SHA is the first eight characters of the commit, and it is the same value in every job of the pipeline. When the rule matches a tag pipeline, $CI_COMMIT_TAG is the value to use. Putting the SHA only in the artifact name while ContainerImageTag stays a fixed string records the commit in GitLab and throws it away at the registry.
The SDK image has no Docker client, so there is no docker login. The two DOTNET_CONTAINER_REGISTRY variables carry the credentials, taken from masked CI/CD variables.
A -p: value on the command line overrides the same property in Api.csproj, so the project file can keep a registry for local use. A local dotnet publish -t:PublishContainer with no tag property produces latest, which keeps a laptop build off the names the pipeline owns.
--getProperty:GeneratedContainerDigest prints the digest of the pushed image and nothing else, and the job saves it. The tag is unique to the commit, but it can still move. A retried job, or a base image that was patched since the first run, pushes a different digest under the same SHA. A deploy that must run exactly what this job built pins api@sha256:<digest> with the value in digest.txt.
More than one image
A release with three images, api, web, and migration, stays aligned when each publish job passes $CI_COMMIT_SHORT_SHA. One job that hardcodes 1.4.2 splits the release. The manifest then needs two values for one commit, and nothing ties the migration that ran to the API it was built with.
Reading it back
After a deploy, ask the cluster what it is running. The answer is a commit:
kubectl get deployment api -o jsonpath='{.spec.template.spec.containers[0].image}'
# registry.example.com/api:3f9c2a1b
git show 3f9c2a1b
A rollback is the same edit with an older SHA, and it works because that tag still exists.
Nothing in the publish job touches the cluster. It produces an image named for a commit and a file with its digest, and the running site changes only when a manifest is updated to that name. The registry is where this design needs attention. One tag per commit grows without limit, so the cleanup policy has to keep every tag a manifest still names.