I have a 1 repository that have a test-ci.yml:
stages:
- build
workflow:
rules:
- if $CI_COMMIT_BRANCH == "main"
variables:
SOME_VARIABLE: $SOME_PROD_VARIABLE
SECOND_VARIABLE: $SECOND_PROD_VARIABLE
workflow:
rules:
- if $CI_COMMIT_BRANCH == "non-prod"
variables:
SOME_VARIABLE: $SOME_NP_VARIABLE
SECOND_VARIABLE: $SECOND_NP_VARIABLE
build-job:
stage: build
script:
- echo $SOME_VARIABLE
- echo $SECOND_VARIABLE
2 repository .gitlab-ci.yml:
include:
- project: 'firstrepo'
file:
- 'test-ci.yml'
variables:
PORT: 5000
workflow:
rules:
- if $CI_COMMIT_BRANCH == "non-prod"
variables:
SECOND_VARIABLE: $SECOND_NP_VARIABLE
workflow:
rules:
- if $CI_COMMIT_BRANCH == "main"
variables:
SECOND_VARIABLE: $SECOND_PROD_VARIABLE
i want the second project's workflow to overwrite/add the variables, but it replaces all the variables so $SOME_VARIABLE is None.
According to the configuration merging mechanism in Gitlab CI/CD (https://docs.gitlab.com/ee/ci/yaml/includes.html#merge-method-for-include) workflow rules from the main file (here .gitlab-ci.yml) override the rules from included file:
When the key exists in both A and B, and one of the values is not a hash map, use the value from B.
Here the key is workflow.rules and it is a sequence that exists in both files. In test-ci.yml you effectively have:
workflow:
rules:
- if $CI_COMMIT_BRANCH == "main"
variables:
SOME_VARIABLE: $SOME_PROD_VARIABLE
SECOND_VARIABLE: $SECOND_PROD_VARIABLE
- if $CI_COMMIT_BRANCH == "non-prod"
variables:
SOME_VARIABLE: $SOME_NP_VARIABLE
SECOND_VARIABLE: $SECOND_NP_VARIABLE
And in .gitlab-ci.yml:
workflow:
rules:
- if $CI_COMMIT_BRANCH == "non-prod"
variables:
SECOND_VARIABLE: $SECOND_NP_VARIABLE
- if $CI_COMMIT_BRANCH == "main"
variables:
SECOND_VARIABLE: $SECOND_PROD_VARIABLE
Upon merging the second definition of rules overrides the first one so SOME_VARIABLE is missing.
I see two possible solutions here: