Why I can't echo $CI_COMMIT_TAG in my gitlab-ci.yml?

Hello, everyone!
When I use the $CI_COMMIT_TAG in my gitlab-ci.yml, I found it is empty.
Here is my gitlab-ci.yml.


tag-job:
  stage: tag
  script:
    - export
    - echo "$CI_COMMIT_TAG"
    - echo "$CI_COMMIT_REF_NAME"
  only:
    - dev-docker
  tags:
    - smart_platform_runner

Hi @hunger_venus

It looks like dev-docker is a branch. Is that right? In which case, you want to restrict this job to only run when a tag has been pushed.

Hi, @hunger_venus.

What @snim2 said about CI_COMMIT_TAG is correct; it’s:

Available only in pipelines for tags.

See Choose when to run jobs | GitLab for more examples but the rule can look like:

tag-job:
  stage: tag
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - export
    - echo "$CI_COMMIT_TAG"
    - echo "$CI_COMMIT_REF_NAME"