Skip to content

Allow passing of env vars between job stages/steps

Arran Walker requested to merge 29391-pass-values-between-jobs into main

What does this MR do?

Adds SourceEnv to the abstract shell, allowing user scripts to pass environment variables between job stages/steps via the $GITLAB_ENV dotenv file.

At the start of each job stage/step, the file that $GITLAB_ENV points to will be automatically source'd.

Example:

job:
  # To export an environment to `after_script`, you'd typically have to add it to a script file and then `source` it in `after_script`.
  # With this change, we have the convenience of doing that via `$GITLAB_ENV` which will load the variables to all build stages in a shell abstract way.
  script: echo "MY_SCRIPT_ENV=foobar" >> $GITLAB_ENV

  # We can now use our variable in `after_script`:
  after_script: echo $MY_SCRIPT_ENV

  # Something that was previously impossible: we can now use our `GITLAB_ENV` exported variables for artifacts and caches:
  artifacts:
    paths:
      - my/path/$MY_SCRIPT_ENV

Why was this MR needed?

So that scripts have greater control over environment variables.

What's the best way to test this MR?

job:
  script:
    - echo "MY_ARTIFACT_PATH=foobar" >> $GITLAB_ENV
    - mkdir foobar
    - echo "file" >> foobar/file

  after_script:
    - echo "my artifact path is ${MY_ARTIFACT_PATH}"

  artifacts:
    paths:
      - "$MY_ARTIFACT_PATH/file"

What are the relevant issue numbers?

Closes #29391 (closed)

Edited by Arran Walker

Merge request reports