Backend: Gitlab-CI: Support Nested Variables expention in rules:if
Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.
Summary
If a variable is defined in the variables
section of .gitlab-ci.yml
using a value containing another variable, the resulting variable will have the right value inside scripts, but not if it is used inside a rules:if
clause.
Note - this is not a bug, since this was not supported from day one, this issue is converted into a feature request
Example
Create a project with default branch 'master' and give it the following .gitlab-ci.yml
:
variables:
# Main development branch in Git, i.e. the one which should trigger a build when merged to
# Usually this will be the Gitlab default branch (named `master` by default), but some repos use a different one
MAIN_DEVELOPMENT_BRANCH: '${CI_DEFAULT_BRANCH}'
# FIXME: For testing, same as above but hardcoded instead of using the value from $CI_DEFAULT_BRANCH
MAIN_DEVELOPMENT_BRANCH_HARDCODED: 'master'
# Control when a pipeline is created
workflow:
rules:
# FIXME: For this test, always create a pipeline
- when: 'always'
# Create pipeline for commits to the main development branch (usually 'master')
- if: '$CI_COMMIT_BRANCH == $MAIN_DEVELOPMENT_BRANCH'
when: 'always'
# Otherwise, do not create a pipeline
- when: 'never'
stages:
- 'show-vars'
########
# Jobs #
########
show-vars:
stage: 'show-vars'
tags:
- 'build'
when: 'always'
script:
- "echo \"'${CI_COMMIT_BRANCH}'\""
- "echo \"'${CI_DEFAULT_BRANCH}'\""
- "echo \"'${MAIN_DEVELOPMENT_BRANCH}'\""
- "echo \"'${MAIN_DEVELOPMENT_BRANCH_HARDCODED}'\""
is-master:
stage: 'show-vars'
tags:
- 'build'
rules:
- if: '$CI_COMMIT_BRANCH == "master"'
when: 'always'
- when: 'never'
script:
- "echo '$CI_COMMIT_BRANCH == \"master\"'"
is-default:
stage: 'show-vars'
tags:
- 'build'
rules:
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
when: 'always'
- when: 'never'
script:
- "echo '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'"
is-main:
stage: 'show-vars'
tags:
- 'build'
rules:
- if: '$CI_COMMIT_BRANCH == $MAIN_DEVELOPMENT_BRANCH'
when: 'always'
- when: 'never'
script:
- "echo '$CI_COMMIT_BRANCH == $MAIN_DEVELOPMENT_BRANCH'"
is-main-hardcoded:
stage: 'show-vars'
tags:
- 'build'
rules:
- if: '$CI_COMMIT_BRANCH == $MAIN_DEVELOPMENT_BRANCH_HARDCODED'
when: 'always'
- when: 'never'
script:
- "echo '$CI_COMMIT_BRANCH == $MAIN_DEVELOPMENT_BRANCH_HARDCODED'"
Push a commit to master
and observe the resulting pipeline.
Currently job 'is-main' does not run, which is expected behavior, once we'll implement variable expention, all 5 jobs should run.
Relevant logs and/or screenshots
The output of job show-vars
shows that $MAIN_DEVELOPMENT_BRANCH
has the right value when inside the job:
Running with gitlab-runner 13.10.0 (54944146)
on [redacted] T7gGNuxr
Preparing the "shell" executor
00:00
Using Shell executor...
Preparing environment
00:00
Running on [redacted]...
Getting source from Git repository
00:01
Fetching changes with git depth set to 50...
Initialized empty Git repository in /home/[redacted]/cicd_test/.git/
Created fresh repository.
Checking out 3e8e7aa5 as master...
Skipping Git submodules setup
Executing "step_script" stage of the job script
00:00
$ echo "'${CI_COMMIT_BRANCH}'"
'master'
$ echo "'${CI_DEFAULT_BRANCH}'"
'master'
$ echo "'${MAIN_DEVELOPMENT_BRANCH}'"
'master'
$ echo "'${MAIN_DEVELOPMENT_BRANCH_HARDCODED}'"
'master'
Cleaning up file based variables
00:00
Job succeeded
Related Issues
See also #323922 (closed)
Proposal
- Support variable expention in
rules:if
for nested variables. - Ensure necessary documentation is updated including:
- The table in https://docs.gitlab.com/ee/ci/variables/where_variables_can_be_used.html#gitlab-ciyml-file should be updated so that
rules:if
"can be expanded?" is true.
- The table in https://docs.gitlab.com/ee/ci/variables/where_variables_can_be_used.html#gitlab-ciyml-file should be updated so that