git » chasquid » commit 4bd2b9e

gitlab-ci: Push a `latest` tag to Docker repositories

author Alberto Bertogli
2021-08-14 10:03:07 UTC
committer Alberto Bertogli
2021-08-14 13:36:24 UTC
parent a3c06b6ee43705e96f97e43181ce4dbf87a088e5

gitlab-ci: Push a `latest` tag to Docker repositories

When doing a `docker pull`, if a tag is not specified, it defaults to
`latest`. We currently don't push such a tag, so it can cause confusion
in some cases (e.g. https://github.com/albertito/chasquid/issues/21).

To help prevent this, make the docker automatic builds for `master` also
apply the `latest` tag.

As a part of this, update the obsolete `$CI_BUILD_REF_NAME` to
`$CI_COMMIT_REF_NAME`.

.gitlab-ci.yml +14 -4

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index e9aa0ff..782e718 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -76,8 +76,13 @@ gitlab:
   script:
     - docker info
     - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
-    - docker build -t $CI_REGISTRY_IMAGE:$CI_BUILD_REF_NAME -f docker/Dockerfile .
-    - docker push $CI_REGISTRY_IMAGE:$CI_BUILD_REF_NAME
+    - docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME -f docker/Dockerfile .
+    - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME
+    - |
+      if [ $CI_COMMIT_REF_NAME == master ]; then
+          docker tag $CI_REGISTRY_IMAGE:master $CI_REGISTRY_IMAGE:latest
+          docker push $CI_REGISTRY_IMAGE:latest
+      fi
 
 # Build docker image, upload to dockerhub registry.
 dockerhub:
@@ -90,5 +95,10 @@ dockerhub:
   script:
     - docker info
     - docker login -u $DOCKER_REGISTRY_USER -p $DOCKER_REGISTRY_PASSWORD docker.io
-    - docker build -t index.docker.io/albertito/chasquid:$CI_BUILD_REF_NAME -f docker/Dockerfile .
-    - docker push index.docker.io/albertito/chasquid:$CI_BUILD_REF_NAME
+    - docker build -t index.docker.io/albertito/chasquid:$CI_COMMIT_REF_NAME -f docker/Dockerfile .
+    - docker push index.docker.io/albertito/chasquid:$CI_COMMIT_REF_NAME
+    - |
+      if [ $CI_COMMIT_REF_NAME == master ]; then
+          docker tag index.docker.io/albertito/chasquid:master index.docker.io/albertito/chasquid:latest
+          docker push index.docker.io/albertito/chasquid:latest
+      fi