Sobes.tech
Junior — Middle

How to configure GitLab CI to run builds only when a specific tag is present?

sobes.tech AI

Answer from AI

To run a build in GitLab CI only when a specific tag is present, you need to use the only or rules directive with a tag condition in the .gitlab-ci.yml file. For example, if you want to run a job only for the v1.0 tag:

build_job:
  script:
    - echo "Build for tag v1.0"
  only:
    - tags
  except:
    - /^((?!v1\.0).)*$/  # exclude all tags except v1.0

Or using rules (more flexible method):

build_job:
  script:
    - echo "Build for tag v1.0"
  rules:
    - if: '$CI_COMMIT_TAG == "v1.0"'
      when: always
    - when: never

This way, the job runs only when a tag named v1.0 is created. You can replace the tag name with the desired one or use regular expressions for more complex conditions.