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.
Asked: 2022-05-21 11:00:00 +0000
Seen: 16 times
Last updated: Jun 08 '22