From f311dcc5f6989c6f8614f819a88e2288ea02263c Mon Sep 17 00:00:00 2001
From: Stefan Hynek <stefan.hynek@uni-goettingen.de>
Date: Tue, 10 May 2022 12:50:26 +0200
Subject: [PATCH] ci(gitlab): don't run build on tags but on merge requests

tag with release version if applicable; else tag with commit short sha
---
 .gitlab-ci.yml | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index cdb1d73..3991fc0 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -7,7 +7,6 @@ workflow:
   rules:
     - if: $CI_MERGE_REQUEST_IID
     - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
-    - if: $CI_COMMIT_TAG
 
 stages:
   - test
@@ -55,17 +54,20 @@ build container image:
     name: gcr.io/kaniko-project/executor:debug
     entrypoint: [""]
   script:
-    - "[[ -n $CI_COMMIT_TAG ]] && export version=$CI_COMMIT_TAG || export version=$CI_COMMIT_SHORT_SHA"
+    # always succeed and don't print error message
+    - tag=$(git tag --contains $CI_COMMIT_SHORT_SHA 2>&1) || true
+    # use tag for version if not empty; else commit sha
+    - "[[ -n $tag ]] && export version=$tag || export version=$CI_COMMIT_SHORT_SHA"
     - mkdir -p /kaniko/.docker
     - echo "{\"auths\":{\"$CI_REGISTRY\":{\"auth\":\"$(echo -n ${CI_REGISTRY_USER}:${CI_REGISTRY_PASSWORD} | base64 | tr -d '\n')\"},\"$harbor_registry\":{\"auth\":\"$HARBOR_ROBOT_TOKEN64\"}}}" > /kaniko/.docker/config.json
     - /kaniko/executor
       --context $CI_PROJECT_DIR
       --dockerfile $CI_PROJECT_DIR/Dockerfile
       --build-arg build_date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
-      --build-arg vcs_ref=${version}
+      --build-arg vcs_ref=${CI_COMMIT_SHORT_SHA}
       --build-arg version=${version}
       --destination $CI_REGISTRY_IMAGE/repdav:$version
       --destination $harbor_registry/$harbor_repo/repdav:$version
   rules:
+    - if: $CI_MERGE_REQUEST_IID
     - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
-    - if: $CI_COMMIT_TAG
-- 
GitLab