2

I have enabled automatic release generation via the GitLab CI/CD. I am using something like this:

curl --header "Content-Type: application/json" --header "PRIVATE-TOKEN: $GITLAB_ACCESS_TOKEN" \
     --data "{ \
       \"name\": \"$GV_SEMVER\", \
       \"tag_name\": \"$GV_SEMVER\", \
       \"ref\": \"$CI_COMMIT_SHA\", \
       \"assets\": { \"links\": [ \
         {\"name\": \"File\", \"url\": \"https://download.example.com/package-$SEMVER.zip\", \"filepath\": \"/package.zip\", \"link_type\": \"other\"}
       ] } }" \
     --request POST "$CI_API_V4_URL/projects/$CI_PROJECT_ID/releases"

The GITLAB_ACCESS_TOKEN is injected properly and it creates a tag and generates the release. Although this is fine, but the creation of the tag triggered a new pipeline again.

I also tried to use the following approach (it doesn't specify the commit SHA during the API request):

git tag $GV_SEMVER
git push --push-option=ci.skip --tags http://root:$GITLAB_ACCESS_TOKEN@$CI_SERVER_HOST/$CI_PROJECT_PATH.git HEAD:$CI_COMMIT_BRANCH
curl --header "Content-Type: application/json" --header "PRIVATE-TOKEN: $GITLAB_ACCESS_TOKEN" \
     --data "{ \
       \"name\": \"$GV_SEMVER\", \
       \"tag_name\": \"$GV_SEMVER\", \
       \"assets\": { \"links\": [ \
         {\"name\": \"File\", \"url\": \"https://download.example.com/package-$SEMVER.zip\", \"filepath\": \"/package.zip\", \"link_type\": \"other\"}
       ] } }" \
     --request POST "$CI_API_V4_URL/projects/$CI_PROJECT_ID/releases"

The git push pushes the new tag to GIT and the ci.skip option prevents a new CI build. But when I have a longer pipeline, then some people may have pushed data and the git push fails. I would rather use the API, but I need a way to disable the CI build.

Ramon de Klein
  • 5,172
  • 2
  • 41
  • 64
  • It seems it's not supported. There is already a feature-request for this: https://gitlab.com/gitlab-org/gitlab/-/issues/26422 – Ramon de Klein May 26 '21 at 11:13

2 Answers2

1

Add the following workflow to the .gitlab-ci.yml to disable CI/CD when a tag is created:

workflow:
  rules:
    - if: $CI_COMMIT_TAG
      when: never
    - when: always

This will effectively disable the CI/CD when the CI_COMMIT_TAG is set (typically when the build is trigger because of creating a tag).

Ramon de Klein
  • 5,172
  • 2
  • 41
  • 64
1

After a lot of time, I found the solution. Add the following code at the top of your .gitlab-ci.yml

workflow:
 rules:
 - if: $CI_COMMIT_TAG
   when: never
 - when: always

Don't add the -o skip.ci when you are pushing your tag : git push origin --tags, also don't add the [skip ci] to your tag commit message

When you use skip ci the workflow is overwritten and you still have a skipped pipeline in your history.

PS: the workflow feature is available since the 12.5 version of Gitlab. The full version of this problem : https://gitlab.com/gitlab-org/gitlab/-/issues/16290

Floran
  • 304
  • 2
  • 14