Skip to content

Can't use some CI vars in rules

Summary

Some CI environment variables can't be used in rules definition.

Steps to reproduce

  1. Define a gitlab-CI workflow:
# Execute pipeline only when triggered from the web UI
workflow:
  rules:
  - if: '$CI_PIPELINE_SOURCE == "web"'

stages:
- stage1
- stage2
- stage3

# Regex is matching $CI_JOB_STAGE anywhere in a comma-separated list string.
job1:
  stage: stage1
  rules:
  - if: '$SKIP_STAGES =~ /(^|.*,)$CI_JOB_STAGE(,.*|$)/'
    when: never
  - when: on_success
  script:
  - echo "Running $CI_JOB_NAME in $CI_JOB_STAGE"

# Regex is matching 'stage1' anywhere in a comma-separated list string.
job1a:
  stage: stage1
  rules:
  - if: '$SKIP_STAGES =~ /(^|.*,)stage1(,.*|$)/'
    when: never
  - when: on_success
  script:
  - echo "Running $CI_JOB_NAME in $CI_JOB_STAGE"

job2:
  stage: stage2
  rules:
  - if: '$SKIP_STAGES =~ /(^|.*,)$CI_JOB_STAGE(,.*|$)/'
    when: never
  - when: on_success
  script:
  - echo "Running $CI_JOB_NAME in $CI_JOB_STAGE"

job2a:
  stage: stage2
  rules:
  - if: '$SKIP_STAGES =~ /(^|.*,)stage2(,.*|$)/'
    when: never
  - when: on_success
  script:
  - echo "Running $CI_JOB_NAME in $CI_JOB_STAGE"

job3:
  stage: stage3
  rules:
  - if: '$SKIP_STAGES =~ /(^|.*,)$CI_JOB_STAGE(,.*|$)/'
    when: never
  - when: on_success
  script:
  - echo "Running $CI_JOB_NAME in $CI_JOB_STAGE"

job3a:
  stage: stage3
  rules:
  - if: '$SKIP_STAGES =~ /(^|.*,)stage3(,.*|$)/'
    when: never
  - when: on_success
  script:
  - echo "Running $CI_JOB_NAME in $CI_JOB_STAGE"
  1. Run pipeline with SKIP_STAGES="stage1,stage2"
  2. See that only jobs with fixed stage name gets skipped (job1a and job2a), not those using $CI_JOB_STAGE (job1 and job2).

Example Project

https://gitlab.com/dr4Ke/gitlab-issue-ci-vars-in-rules

What is the current bug behavior?

Using these kind of rules:

  rules:
  - if: '$SKIP_STAGES =~ /(^|.*,)$CI_JOB_STAGE(,.*|$)/'
    when: never
  - when: on_success

When running a pipeline with SKIP_STAGES="stage1,stage2", the job in stage1 and stage2 should be ignored, but they are not.

What is the expected correct behavior?

Ignore jobs from the stages defined in the SKIP_STAGES variable. The pipeline should only contain jobs from stage3.

Relevant logs and/or screenshots

Execution without SKIP_STAGES defined:

Capture_d_écran_de_2020-06-25_12-21-32

Execution with SKIP_STAGES="stage1,stage2":

Capture_d_écran_de_2020-06-25_12-20-25

I didn't find any logs related to how the rules are processed. If I can find them somewhere, please kindly point me to the right component and log file. I guess it's the gitlab-task-runner that process them, but I didn't see logs related to rules.

Output of checks

This bug happens on GitLab.com, and on gitlab v13.1.0.

Possible fixes

It seems that the CI_JOB_STAGE don't get interpreted in the rules evaluation process.