Converting only:ref to only variable

Describe your question in as much detail as possible:
We are using only:ref in our pipelines,

.project_a_conditions_only:
  only:
     refs:
       - /^(.+\/(bug|story|task)\/.+)|(.+\/(revert|patch)\/.+)/
     variables:
       - $PROJECT_A

I’m trying to convert the same into

variables:
  - BRANCH_REF_REGEX = /^(.+\/(bug|story|task)\/.+)|(.+\/(revert|patch)\/.+)/
.project_a_conditions_only:
  only:
     variables:
       - $CI_COMMIT_REF_NAME =~ $BRANCH_REF_REGEX && $PROJECT_A
  • What are you seeing, and how does that differ from what you expect to see?
    I’m not seeing my job in the pipeline anymore. Is the above syntax good or should I change it to something else?

I’m expecting my job to be working as expected.

  • What version are you on? Are you using self-managed or GitLab.com?

    • *GitLab (Hint: /help): 14.8.5-ee
    • *Runner (Hint: /admin/runners): 14.10.0
  • What troubleshooting steps have you already taken? Can you link to any docs or other resources so we know where you have been?
    I have searched a lot of places but could not find one…

Thanks for taking the time to be thorough in your request, it really helps! :blush:

To be honest, if I were you I’d use rules here, which are pretty much the same as only/except, but more flexible.

So, your snippet above would become something like this:

variables:
  - BRANCH_REF_REGEX = /^(.+\/(bug|story|task)\/.+)|(.+\/(revert|patch)\/.+)/

.project_a_conditions_only:
  rules:
     - '$CI_COMMIT_REF_NAME =~ $BRANCH_REF_REGEX && $PROJECT_A'
        when: on_success
    - when: never

Hi @snim2 , Ya that’s true, It is ideal to move towards rules, but we have this case in one of the monolithic repo which counts to nearly 80 references, changing all of them to rules at the moment is a very costly thing. Currently, I’m looking for a quick win solution. We will be moving towards the rules end of this year.

That sounds tricky!

I notice in your snippet you say:

.project_a_conditions_only:
  only:
     variable:
       - $CI_COMMIT_REF_NAME =~ $BRANCH_REF_REGEX && $PROJECT_A

But in the docs they use:

.project_a_conditions_only:
  only:
     variables:
       - $CI_COMMIT_REF_NAME =~ $BRANCH_REF_REGEX && $PROJECT_A

But presumably the lint would have picked that up already?

There is an issue for this Backend: Regexp CI/CD variables are evaluated as strings (#35438) · Issues · GitLab.org / GitLab · GitLab
I am not following it anymore, but you can read up and see whats the status of this feature.