gitlabgitlab-cigitlab-ci-runner

Gitlab CI has empty variable defined in same CI file


I’m running this minimal .gitlab-ci.yml script on gitlab.com with default shared runners:

variables:
  VAR: Value

job:
  only:
    - main

  run:
  - name: test
    script: echo VAR=$VAR

This is the output

steps:
    - name: test
      script: echo VAR=$VAR
VAR=

What the hell is going on? How do I actually get the value of my variable? This seems to be directly contradicting the docs on variables.

Note that setting CI_DEBUG_TRACE: true shows CI_RUNNER_VERSION=17.4.0~pre.110.g27400594 and

++ export VAR=Value
++ VAR=Value

So it seems to be the script that is unable to access/inherit the environment rather than the variables being ignored. Running script: env confirms this:

steps:
    - name: test
      script: env
PWD=/builds
SHLVL=0
PATH=/go/bin:/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_=/usr/bin/env

I’ve looked for this issue but all potential dupes I’ve found are misses: this isn’t the same as having that empty variables in the environment section, or protected variables, or loading variables from source, etc.


Solution

  • I think the issue here is not with the variables but with the run: syntax which is, as of writing, still marked as “Experiment” and “not ready for production use”. I just tested the same with the usual single script syntax, and the variables worked as expected:

    variables:
      VAR: Value
    
    job:
      only:
        - main
      script:
        - echo VAR=$VAR
    

    And in the output I see:

    Executing "step_script" stage of the job script
    $ echo VAR=$VAR
    VAR=Value
    Cleaning up project directory and file based variables
    Job succeeded