In this lesson, you will learn how to implement semantic versioning (SemVer) to ensure your container images provide clear, predictable release history within your CI/CD pipelines.
The Problem with "Latest"
Relying on the :latest tag is a common anti-pattern that destroys traceability. When you deploy myapp:latest, you cannot easily determine which specific commit produced that image, making rollbacks difficult and environment parity impossible to guarantee. Semantic Versioning—formatted as MAJOR.MINOR.PATCH (e.g., 1.4.2)—acts as a contract that communicates the nature of changes to the rest of the infrastructure.
Implementing SemVer in the Pipeline
To enforce this, your CI pipeline must calculate the next version based on your versioning strategy. A robust approach is to tag each build with the SemVer version and the short Git commit SHA for absolute traceability.
In a multi-stage pipeline, your build step should automate this logic:
# Calculate version (e.g., using a tool like 'git-semver' or dynamic bash)
VERSION="1.2.0"
COMMIT_SHA=$(git rev-parse --short HEAD)
# Tag and push images to your registry
docker build -t my-registry/app:$VERSION .
docker tag my-registry/app:$VERSION my-registry/app:$VERSION-$COMMIT_SHA
docker push my-registry/app:$VERSION
docker push my-registry/app:$VERSION-$COMMIT_SHA
Traceability and Deployments
When you update your Kubernetes manifests via GitOps or automated deployment scripts, you should point to the specific SemVer tag (or the SHA-suffixed tag for immutable integrity). This ensures that if v1.2.0 introduces a regression, you have a definitive, human-readable identifier to roll back to.
By separating the Version (which signals features or breaking changes) from the Git SHA (which provides granular auditability), you create an environment where the state of the cluster is always synchronized with the version control system. This linkage is critical for debugging deployments and performing impact analysis during security patching.
Key Takeaway: Never deploy using the "latest" tag; instead, use Semantic Versioning combined with Git SHAs to ensure every container image is immutable, version-controlled, and fully traceable to the source code.