diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 915974ba0b351b5c918f815a361323f6de61c61b..8f7a4e6af65b5a5b746731b2d02b1c839d002c43 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -95,7 +95,7 @@ test_build_args_dockerfile:
   - test $(cat /tmp/test1) == "test_arg1"
   - test $(cat /tmp/test2) == "test_arg2"
 
-# Test for image by overriding the FROM firewalld
+# Test for image by overriding the FROM
 build_override_from_dockerfile:
   <<: *build_test_image
   variables:
@@ -110,3 +110,23 @@ test_override_from_dockerfile:
   script:
   - grep -qi CentOS /etc/redhat-release
   - test $(cat /tmp/test) == "simple_dockerfile"
+
+# If all tests pass and we are running on master, retag the image to latest. This image will be used for user docker builds from now on.
+build_stable_image:
+  stage: promote_tested_image_to_latest
+  only:
+  - master
+  script:
+  - echo "Obtain token to authenticate with the gitlab registry"
+  - JWT_PULL_PUSH_TOKEN=$(curl -q -u gitlab-ci-token:${CI_JOB_TOKEN} "https://${GITLAB_HOST}/jwt/auth?service=container_registry&scope=repository:${CI_PROJECT_PATH}:pull,push" | cut -d\" -f4 )
+  - 'curl "https://${CI_REGISTRY}/v2/${CI_PROJECT_PATH}/tags/list" -H "Authorization: Bearer ${JWT_PULL_PUSH_TOKEN}"'
+  - echo "Pulling the manifest of tag:${OLDTAG}"
+  - 'curl "https://${CI_REGISTRY}/v2/${CI_PROJECT_PATH}/manifests/${OLDTAG}" -H "Authorization: Bearer ${JWT_PULL_PUSH_TOKEN}" -H "accept: application/vnd.docker.distribution.manifest.v2+json" > manifest.json'
+  - 'echo "Pushing new tag: ${NEWTAG}"'
+  - 'curl -XPUT "https://${CI_REGISTRY}/v2/${CI_PROJECT_PATH}/manifests/${NEWTAG}" -H "Authorization: Bearer ${JWT_PULL_PUSH_TOKEN}" -H "content-type: application/vnd.docker.distribution.manifest.v2+json" -d "@manifest.json" -v'
+  - echo "List of tags in registry"
+  - 'curl "https://${CI_REGISTRY}/v2/${CI_PROJECT_PATH}/tags/list" -H "Authorization: Bearer ${JWT_PULL_PUSH_TOKEN}"'
+  variables:
+    GITLAB_HOST: gitlab.cern.ch
+    OLDTAG: "test_${CI_COMMIT_REF_NAME}"
+    NEWTAG: "latest"
\ No newline at end of file