'Can I define a variable from gitlab-ci as a value in a variable in settings (or schedules)?

Here's what I am trying to do.

in .gitlab-ci:

Check schedules pass:
  stage: check
  image: ${myimage}
  script:
    - MY_CI_VAR=aVeryLongVariable
    - echo "$MY_SCHEDULE_VAR"

In schedules: enter image description here

Which is not working.

The reason I want to do this is for picking different variable (out of many in the job) on each schedule.



Solution 1:[1]

Yes, it is possible to use variables within other variables. This feature was released in GitLab 14.3.

However, since you are using GitLab 13.x, this feature won't be available to you.

You may be able to get around this limitation by using a static value and altering your job script accordingly.

myjob:
  before_script: |
    if [[ "$SCHEDULE_VAR" == "abc" ]]; then
        export FOO="$MY_CI_VAR"
    fi
  # ...

In versions of GitLab < 14.3 you can still make use of other variables within variables, but instead by using $$ to preserve variables from evaluation.

Example from the docs:

variables:
  FLAGS: '-al'
  LS_CMD: 'ls "$FLAGS" $$TMP_DIR'
script:
  - 'eval "$LS_CMD"'  # Executes 'ls -al $TMP_DIR'

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 sytech