Ask Your Question
0

How can environment variables be transferred from one GitLab CI stage to the subsequent one?

asked 2022-05-21 11:00:00 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-06-08 13:00:00 +0000

huitzilopochtli gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-05-21 11:00:00 +0000

Seen: 16 times

Last updated: Jun 08 '22