Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitlab CI - $CI_COMMIT_TAG is empty

I need the tag value of my last pushed git commit in my gitlab-ci.yml when building. In the build process I build a docker image and after the build I want to push this images tagged with the same tag as my git commit. So far my understanding is that the environment variable $CI_COMMIT_TAG should do the work. Nevertheless when I echo out $CI_COMMIT_TAG in my gitlab-ci.yml is it just empty.

Here is my gitlab-ci.yml:

    stages:
      - build
    
    build-dev:
      stage: build
      environment: development
      only:
        - master
      tags:
        - ms-doorman
      script:
        - echo $CI_COMMIT_TAG

Here the git commands to start the job.

$ git commit -am "test git tags"
$ git tag test-tag
$ git push --tags origin master
like image 775
Darius Mann Avatar asked Aug 12 '20 04:08

Darius Mann


1 Answers

I found a nice issue in Gitlab which describes this behavior very well:

When you will push a commit to GitLab, then it will start a pipeline without CI_BUILD_TAG variable. When you make a tag on this commit and push this tag to GitLab, then another pipeline (this time for the tag, not for the commit) will be started. In that case CI_BUILD_TAG will be present.

Maybe you can use the workflow:rules to avoid the error.

only on tags:
  rules:
    - if: '$CI_COMMIT_TAG != null'
  script:
    - echo only on tags
    - env

like image 55
flaxel Avatar answered Oct 16 '22 14:10

flaxel