'Gitlab CI variable override in workflow

I want TEST_VAR to be overridden by the workflow but when I run a $NIGHTLY pipeline, it still prints "normal". What am I doing wrong here? According the the precedence, I believe it should work...

image: alpine

variables:
  TEST_VAR: "normal"

workflow:
  rules:
    - if: $CI_COMMIT_BRANCH
    - if: $NIGHTLY
      variables:
        TEST_VAR: "nightly"

test:
  script:
    - echo $TEST_VAR


Solution 1:[1]

One more option is hack with child pipelines.

nightly-run:
  variables:
    TEST_VAR: "nightly"
  rules:
    - if: $NIGHTLY
  trigger:
    include: .gitlab-ci.yml
    strategy: depend

It will override global TEST_VAR if "nightly-run" triggers.

Pay attention all jobs that triggered by "parent pipeline" will with CI_PIPELINE_SOURCE == 'parent_pipeline'.

Solution 2:[2]

A solution can be resolve your issus is to update the $TEST_VAR in script part.

script: 
 - |
 - if $NIGHTLY 
   then 
      $TEST_VAR = "nightly"
   else 
      $TEST_VAR = "others values"
   fi

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Novikov
Solution 2 Younes Ben Tlili