Expand tag variables after evaluating rules:variables to be able to adjust tags from rules

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

Summary

The following code always sets the tag to UNKNOWN instead of using the branch name:

stages:
  - test

test-job:
  stage: test
  rules:
    - if: $CI_COMMIT_BRANCH 
      variables:
        DEPLOY_ENVIRONMENT: "$CI_COMMIT_BRANCH"
  script:
    - echo "The DEPLOY_ENVIRONMENT value is '$DEPLOY_ENVIRONMENT', check the tag value."
  tags:
    - $DEPLOY_ENVIRONMENT
  variables:
    DEPLOY_ENVIRONMENT: "UNKNOWN"

Haven't looked into the gitlab code, but seems clear that the variables for the tags are expanded before checking the rules, if the order is reversed this code will work as expected.

Proposal

Review how and when variable expansion is applied for tags and make sure that the expansion is done after evaluating the rules.

How does this benefit the user?

  1. It makes the behavior consistent with the documentation.
  2. Allows to define a single job that can be executed on different runners evaluating complex conditions that are not on the existing tags (in my case I've had to split a job into multiple ones and that forced me to modify dependencies in multiple places).

Links/References

The issue #382571 (closed) is another example of the same use case.

Edited by 🤖 GitLab Bot 🤖