Creating a rule for a branch/tag containing a slash

How do I create a rule that only runs a job when a branch or tag has a specific prefix, and that prefix contains a slash?

I’ve tried various permutations on:

  if: $CI_COMMIT_TAG =~ /^releases[/]component[/]/
  if: $CI_COMMIT_TAG =~ /^releases\/component\//
  if: "'$CI_COMMIT_TAG' =~ '/^releases\\/component\\//'"

No matter what I try I get one of three outcomes:

  • YAML error
  • Always runs the job
  • Never runs the job

Ah, in one of my earlier tests I had two errors that was causing the invalid YAML.

So after trying some more variations, these two work:

# without quotes, only single escape needed
if: $CI_COMMIT_TAG =~ /^releases\/component\//

# with quotes around the whole YAML string, double escape required
if: "$CI_COMMIT_TAG =~ /^releases\\/component\\//"