Skip to content

`after_script` does not have access to ENV variables exported in `before_script` or `scripts`

Summary

As a fork from #3088 (closed), (in fear of it not being re-opened), here is an explicit ticket about the problem.

Effectively:

  1. Variables exist: https://docs.gitlab.com/ee/ci/yaml/#variables
  2. There is documentation that states where they can and cannot be used: https://docs.gitlab.com/ee/ci/variables/where_variables_can_be_used.html

Users have tested and debugged to prove that in fact, variables defined ARE NOT available in after_script as expected. One user @tmaczukin has made claim in #3088 (comment 58897807) that this is by design.

This needs to be documented.

Steps to reproduce

variables:
  CI_DEBUG_TRACE: "true"
  FOO: "foo"

test:
  stage: test
  before_script:
    - "echo IN before_script"
    - "export BAR=\"bar\""
    - "echo foo : ${FOO}"
    - "echo bar : ${BAR}"
  script:
    - "echo IN script"
    - "echo foo : ${FOO}"
    - "echo bar : ${BAR}"
  after_script:
    - "echo IN after_script"
    - "echo foo : ${FOO}"
    - "echo bar : ${BAR}"

Actual behavior

(snipping the ONEROUS output from debug to what's relevant here)

BEFORE_SCRIPT:

$ echo IN before_script
IN before_script
$ export BAR="bar"
$ echo foo : ${FOO}
foo : foo
$ echo bar : ${BAR}
bar : bar
$ echo IN script

SCRIPT:

IN script
$ echo foo : ${FOO}
foo : foo
$ echo bar : ${BAR}
bar : bar

AFTER_SCRIPT:

Running after script...
$ echo IN after_script
IN after_script
$ echo foo : ${FOO}
foo : foo
$ echo bar : ${BAR}
bar :

Expected behavior

Whoops! BAR is empty in the after_script, it was defined in the before_script. FOO is available, it was defined in the variables section.

I'm unable to find in documentation any statements or explanations that deem this as "expected behaviour".

Used GitLab Runner version

gitlab IC pipeline, v11.7.10