Ask Your Question

Revision history [back]

Environment variables can be transferred from one GitLab CI stage to the subsequent one by using the "artifacts" feature.

To achieve this, variables can be saved as artifacts in one stage and then retrieved in the subsequent stage using the "dependencies" keyword.

Here is an example:

stages:
  - build
  - test

build:
  stage: build
  script:
    - export MY_VAR="hello world"
    - echo $MY_VAR
  artifacts:
    paths:
      - artifacts/

test:
  stage: test
  script:
    - echo $MY_VAR
  dependencies:
    - build

In the "build" stage, an environment variable "MY_VAR" is set and saved as an artifact. In the "test" stage, the environment variable is retrieved using the "dependencies" keyword and echoed.

Note that the artifact path is specified as "artifacts/", so make sure to use the same path when retrieving the artifact in the subsequent stage.