'How to trigger a gitlab-ci job manually but only when tagged?

I have tested when: manual and it works as expected. It creates a pipeline with a job that I can push play on.

But suppose I only want a job to have the manual option if a tag was created. Otherwise, skip this job.

This does not seem to work for me:

tag-triggered-manual-job:
  stage: publish
  only:
    variables:
      - $CI_COMMIT_TAG =~ /^product-build-.*/
  when: manual
  script:
    - script goes here


Solution 1:[1]

If you rewrite your job with rules it should work as excpected:

tag-triggered-manual-job:
  stage: publish
  script:
    - script goes here
  rules:
    - if: '$CI_COMMIT_TAG =~ /^product-build-.*/'
      when: manual

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 danielnelz