Search code examples
gitlabgitlab-cigitlab-ce

How to make IF statements add up as an AND operator in gitlab-ci job?


I have a job template .deploy_to_staging:template with some rule that is extended by another job deploy_to_staging_triggered that adds up another rule:

.deploy_to_staging:template:
 <<: *job_deploy_definition
 stage: deploy
 rules:
   - if: '$JOB_TRIGGERED == "DEFAULT"'

deploy_to_staging:triggered:
  extends: .deploy_to_staging:template
  rules:
    - if: '$ENV_TRIGGERED == "STAGING"'

When this is executed, it seems that Gitlab performs a merge that internally creates a job that looks like this one:

deploy_to_staging:mergedJob (following merge)
 <<: *job_deploy_definition
 stage: deploy
 rules:
   - if: '$CI_COMMIT_TAG && $JOB_TRIGGERED == "DEFAULT"'
   - if: '$ENV_TRIGGERED == "STAGING"'

Gitlab evaluates rules in order until first match according to their docs. So the above if rules acts like OR operators that is logically equivalent to this single IF statement:

- if : '$JOB_TRIGGERED == "DEFAULT" || $ENV_TRIGGERED == "STAGING"'

However I would like all conditions to be verified in order for the job to be executed, i.e having an AND operator instead:

- if : '$JOB_TRIGGERED == "DEFAULT" && $ENV_TRIGGERED == "STAGING"'

How can this be achieved?


Solution

  • Unfortunately, as of Gitlab version 13.9 this isn't possible. The closest they've come to using the rules keyword with templates is allowing conditions to decide if an include is actually included or not using rules (https://gitlab.com/gitlab-org/gitlab/-/issues/216673).

    If you're a paying customer, talking to Support (or your sales contact? not sure) will help. Generally Gitlab relies on the community to build wanted features since it is Open Source, but when enough paying customers want a feature, they'll work on it directly.