Run jobs only when specific variable values are passed

I’m trying to execute a job only if a specific value is passed as a variable

  only:
    variables:
      - $UPSTREAM_PROJECT
      - $UPSTREAM_VERSION
      - $TARGET_ENV == "dev"

Unfortunately this is always triggered and Gitlab CI seems to ignore the ==.

I also tried

      - $TARGET_ENV =~ /dev/

but that also always get’s triggered

According to https://docs.gitlab.com/ee/ci/yaml/#onlyexcept-advanced

it should only be trigger if all conditions match

  • What are you seeing, and how does that differ from what you expect to see?

  • Consider including screenshots, error messages, and/or other helpful visuals

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

    • *GitLab (Hint: /help): 13.2.4
    • *Runner (Hint: /admin/runners): 13.2.1
  • Add the CI configuration from .gitlab-ci.yml and other configuration if relevant (e.g. docker-compose.yml)

upstream:
  only:
    variables:
      - $UPSTREAM_PROJECT
      - $UPSTREAM_VERSION
      - $TARGET_ENV =~ /dev/
  stage: deploy
  environment:
    name: dev
  image: registry.acme.com/image:bb9531c8
  script:
    - echo ...
  • What troubleshooting steps have you already taken? Can you link to any docs or other resources so we know where you have been?

Other than above I also tried to combine only and except but that also always triggered

I’ve read
https://docs.gitlab.com/ee/ci/yaml/#onlyexcept-advanced

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

I think the problem is that you have also other two variables.

In the documentation you linked, it’s written:

(any of refs) AND (any of variables) AND (any of changes) AND (if Kubernetes is active)

and I understand it as:

(any of refs) AND ( var1 OR var2 OR var3) AND (any of changes) AND (if Kubernetes is active)

Looking at the docs for “Environment Variable Expressions,”, it says “If any of the conditions in variables evaluates to true when using only , a new job is created.” I think that might be what’s causing the issue here. Have you considered switching to rules instead? I’ve had better success with rules than with only/except.