Search code examples
automationgitlabcontinuous-integrationgitlab-cigitlab-ci-runner

Tagging in gitlab CI running multiple times


I created GitLab job to create git tag and push to the repository as following:

  add tag:
  extends: .install_dependencies
  stage: deploy
  image:
    name: nexus-docker.ship.gov.sg/python:latest
    entrypoint: ["/bin/bash"]
  script:
    - echo "This job deploys something from the $CI_COMMIT_BRANCH branch."
    - git tag -a v"$VERSION" -m version"$VERSION"
    - git push origin v"$VERSION"
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_COMMIT_BRANCH

But every-time I pushed from my commit branch, the pipeline is triggered by 2 times: from branch and then tag. Since the tag is already created in first pipeline, then the second triggered by tag is failed. I only want to make run one time for this job based on the rules.

pipeline triggered 2 times for same commit


Solution

  • In the Gitlab CI yaml there is a keyword except: to tell the CI when not to run. What you'll want to do is add this to the task so it only runs once to tag, and then ignores the tag when that comes. This is what your pipeline script might look like:

      add tag:
      extends: .install_dependencies
      stage: deploy
      ...
      except:
        - tags
    
      script:
        - echo "This job deploys something from the $CI_COMMIT_BRANCH branch."
      ...