CI/CD Pipeline for Versioned Docker Releases Using GitLab
CI/CD Pipeline for Versioned Docker Releases Using GitLab
To make deployments easier and more reliable, I have created a GitLab CI/CD pipeline.
Whenever I push code to a release-X.X.X branch, GitLab automatically builds a Docker image with the updated index.html, tags the repository, and pushes the image to DockerHub. A manual deployment job is also available, giving me full control over when to release the image.
Figure: GitLab CI/CD flow for versioned Docker releases
Prerequisites
To set this up, I already had:
- An EC2 instance with Docker installed and accessible via SSH
- A GitLab repository for storing the
.gitlab-ci.ymlfile - A DockerHub account with credentials ready
Pipeline Configuration
.gitlab-ci.yml
stages:
- build
- tag
- deploy
variables:
DOCKER_IMAGE: vamsi193/release-cicd
workflow:
rules:
- if: '$CI_COMMIT_REF_NAME =~ /^release-\d+\.\d+\.\d+$/'
when: always
- when: never
build_image:
stage: build
tags:
- my-runner
script:
- echo "$DOCKERHUB_PASSWORD" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin
- TAG_NAME="${CI_COMMIT_REF_NAME#release-}"
- echo "TAG_NAME=$TAG_NAME" > tag.env
- docker build -t $DOCKER_IMAGE:$TAG_NAME .
- docker push $DOCKER_IMAGE:$TAG_NAME
artifacts:
reports:
dotenv: tag.env
tag_repo:
stage: tag
tags:
- my-runner
dependencies:
- build_image
script:
- git tag v$TAG_NAME
- git push https://oauth2:${CI_GITLAB_PAT}@gitlab.com/vamsi193/release-cicd.git v$TAG_NAME
manual_deploy:
stage: deploy
tags:
- my-runner
script:
- echo "Stopping existing container if it exists..."
- docker stop release-app || true
- docker rm release-app || true
- docker run -d --name release-app -p 80:80 $DOCKER_IMAGE:$TAG_NAME
when: manual
Workflow Logic
The workflow block ensures the pipeline runs only when the branch name follows the release-X.X.X format:
1
2
3
4
5
workflow:
rules:
- if: '$CI_COMMIT_REF_NAME =~ /^release-\d+\.\d+\.\d+$/'
when: always
- when: never
Stage Breakdown
build_image: Builds the Docker image, tags it with the version, and pushes it to DockerHub.tag_repo: Automatically tags the Git repository withvX.X.X.manual_deploy: Lets me deploy the container manually when I’m ready — no automatic production deploys.
Insights from this Pipeline
Built and pushed Docker images when a release tag is created DockerHub
Tagged the Git repository with the version GitLab Repository
Deployed the image manually in a safe and controlled way Pipeline View
This post is licensed under CC BY 4.0 by the author.


