Skip to content

GitLab CI's variables block does not expand other variables, so a default goes in before_script

November 4, 2024

In GitLab CI, the variables block doesn’t expand other variables at definition time. So this doesn’t work as expected:

variables:
  DEPLOY_USER: "${DEPLOY_USER:-$CI_REGISTRY_USER}"
  DEPLOY_TOKEN: "${DEPLOY_TOKEN:-$CI_JOB_TOKEN}"

GitLab CI doesn’t resolve the $CI_REGISTRY_USER and $CI_JOB_TOKEN references in the variables block, because it doesn’t support nested variable expansion there.

The fallback goes into before_script instead. Normal shell expansion applies there:

before_script:
  - export DEPLOY_USER="${DEPLOY_USER:-$CI_REGISTRY_USER}"
  - export DEPLOY_TOKEN="${DEPLOY_TOKEN:-$CI_JOB_TOKEN}"

This comes up mostly in reusable CI templates, where you want to allow overriding a variable but fall back to a CI-provided default.

—

Back to TIL

Next: scrollIntoView with false aligns an element to the bottom of its scroll container

Phil-Bastian Berndt

Phil-Bastian Berndt
Tech Lead at Naymspace