Automating Image Tags from Git History and CI Metadata
Automating container image tagging strategies in GitHub Actions and GitLab CI using Git tags, branch names, commit SHAs, and semver patterns.
Published on • November 18, 2026
AI Assistant

Manually tagging container images (docker tag app:latest myrepo/app:v1.2.3) during release operations is prone to human error, missed Git commits, and non-reproducible deployments.
A robust CI/CD pipeline automates image tag generation directly from Git repository events, converting branch pushes, semver tags, pull requests, and commit SHAs into structured, predictable registry tags.
In this tutorial, we will design an automated tagging rule set, implement it using GitHub Actions and GitLab CI, and eliminate tag ambiguity across all environments.
Automated Tagging Rules Matrix
+-----------------------------------------------------------------------------------+
| Git Trigger Event Generated Container Image Tags |
+-----------------------------------------------------------------------------------+
| Git Tag `v2.4.1` --> `:2.4.1`, `:2.4`, `:2`, `:latest`, `:sha-7a8b9c0` |
| Push to `main` branch --> `:edge`, `:sha-7a8b9c0` |
| Pull Request #142 --> `:pr-142` |
| Feature branch `feat-payment` --> `:branch-feat-payment` |
+-----------------------------------------------------------------------------------+
Implementation 1: GitHub Actions with docker/metadata-action
The official docker/metadata-action handles complex semver parsing, Git SHA formatting, and branch sanitation automatically.
name: Automated Docker Build & Tag
on:
push:
branches:
- 'main'
- 'develop'
tags:
- 'v*.*.*'
pull_request:
branches:
- 'main'
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Extract automated tag & label metadata based on Git event
- name: Extract Docker Metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository }}
tags: |
# 1. Semver tags for Git releases (v2.4.1 -> 2.4.1, 2.4, 2, latest)
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
# 2. Main branch push gets 'edge' tag
type=edge,branch=main
# 3. Pull Request gets 'pr-142' tag
type=ref,event=pr
# 4. Every build gets short Git SHA (sha-7a8b9c0)
type=sha,prefix=sha-,format=short
- name: Build and Push Image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
Generated Tag Output Examples:
- When Git Tag
v1.5.2is pushed:ghcr.io/myorg/myapp:1.5.2ghcr.io/myorg/myapp:1.5ghcr.io/myorg/myapp:1ghcr.io/myorg/myapp:latestghcr.io/myorg/myapp:sha-4a2b1c3
- When PR #88 is updated:
ghcr.io/myorg/myapp:pr-88
Implementation 2: GitLab CI (.gitlab-ci.yml)
In GitLab CI, predefined environment variables ($CI_COMMIT_TAG, $CI_COMMIT_SHORT_SHA, $CI_COMMIT_REF_SLUG) simplify tag generation:
stages:
- build
variables:
REGISTRY_IMAGE: $CI_REGISTRY_IMAGE
build_image:
stage: build
image: docker:24.0
services:
- docker:24.0-dind
script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
# 1. Always build and tag with short Git SHA
- docker build -t $REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA .
- docker push $REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA
# 2. If building a Git release tag (e.g. v2.4.1)
- |
if [ -n "$CI_COMMIT_TAG" ]; then
docker tag $REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA $REGISTRY_IMAGE:$CI_COMMIT_TAG
docker tag $REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA $REGISTRY_IMAGE:latest
docker push $REGISTRY_IMAGE:$CI_COMMIT_TAG
docker push $REGISTRY_IMAGE:latest
fi
# 3. If building the main default branch
- |
if [ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]; then
docker tag $REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA $REGISTRY_IMAGE:edge
docker push $REGISTRY_IMAGE:edge
fi
Best Practices for Automated Tagging
- Always Include Git SHA Tag: Ensure every single image built in CI gets a
sha-<commit>tag. This guarantees absolute traceability back to source code. - Sanitize Branch Names: Ensure branch names used in tags replace slashes with hyphens (e.g.,
feature/loginbecomesbranch-feature-login), as slashes are invalid in Docker tag strings. - Set Retention Policies on PR Tags: Configure your registry lifecycle rules to automatically delete
pr-*tags after 14 days to prevent storage bloat.
Summary
Automating image tags from Git events ensures 100% traceability across commits, releases, and pull requests while maintaining clean semver tag pointers in production.