Allow conditional variables without triggering the pipeline

Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.

Being relatively new to gitlab pipeline I was unfamiliar with the concept that the first match in the rules section triggers the pipeline. So I was confident that something like this would work:

 rules:
    - if: $CI_COMMIT_BRANCH == "dev"
      variables:
        DEPLOYMENT_ENV: "development"
    - if: $CI_COMMIT_BRANCH == "staging"
      variables:
        DEPLOYMENT_ENV: "staging"
    - if: $CI_COMMIT_BRANCH == "main"
      variables:
        DEPLOYMENT_ENV: "production"
    - changes:
        - foo/**/*
      when: always
 environment: $DEPLOYMENT_ENV

meaning that when I make a commit in dev branch the job runs only when something in the "foo" folder changes AND the "DEPLOYMENT_ENV" variable is set to "development". Instead it matches the first condition, and it never makes it to the changes condition.

Basically what I am asking is to have the possibility to set the variable without triggering the pipeline, for example with a custom flag like skip-match.

I am aware it is not a beautiful solution, also because for this specific use case it would be enough to be able to define variables via bash commands like DEPLOYMENT_ENV: $(my_script.sh). As for setting the variable I know I can do so in the script section, but this doesn't make it available in the environment field.

This is a problem for me because I am trying to create separate pipelines for each folder in a monorepo. The pipeline depends on environment variables that are scoped by the environment. Without this feature, even optimizing the yml with anchors and references, with just 5 folders I end up creating 20 or 30 jobs instead of just 10 (build + deploy) * 5, because of the combination of environment variable + branch + job type for each folder. I don't want to think what it would be if I had 10 or 20 folders.

The alternative is to have only manual jobs, which in my case is far from ideal.

I hope I am missing something obvious and someone shows me the way

Edited by 🤖 GitLab Bot 🤖