Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting environment variables from one stage to the next in GitLab CI

Tags:

Is there a way to export environment variables from one stage to the next in GitLab CI? I'm looking for something similar to the job artifacts feature, only for environment variables instead of files.

Let's say I'm configuring the build in a configure stage and want to store the results as (secret, protected) environment variables for the next stages to use. I could safe the configuration in files and store them as job artifacts but I'm concerned about secrets beeing made available in files than can be downloaded by everyone.

like image 273
Bastian Venthur Avatar asked Jun 27 '17 07:06

Bastian Venthur


People also ask

How do you pass a variable from one stage to another in GitLab CI?

Just use artifacts, store the values in file then pass it to next job.

Where are environment variables stored GitLab?

Jobs and pipelines as environments The variables can be stored in the project/group/instance settings and be made available to jobs in pipelines.

How many ways we can specify variables in GitLab?

There are two places defined variables can be used.


2 Answers

Since Gitlab 13 you can inherit environment variables like this:

build:   stage: build   script:     - echo "BUILD_VERSION=hello" >> build.env   artifacts:     reports:       dotenv: build.env  deploy:   stage: deploy   script:     - echo $BUILD_VERSION # => hello   dependencies:     - build 

Note: for GitLab < 13.1 you should enable this first in Gitlab Rails console:

Feature.enable(:ci_dependency_variables) 
like image 106
hd.deman Avatar answered Sep 18 '22 06:09

hd.deman


No this feature is not here yet, but there is already an issue for this topic.

My suggestion would be that you are saving the variables in a files and cache them, as these will be not downloadable and will be removed on finish of the job. If you want to be 100% sure you can delete it manually. See the clean_up stage.

e.g.

cache:  paths:   - save_file  stages:   - job_name_1  - job_name_2  - clean_up  job_name_1:  script:   - (your_task) >> save_file  job_name_2:  script:   - cat save_file | do_something_with_content  clean_up:  script:   - rm save_file  when: always  
like image 25
secustor Avatar answered Sep 21 '22 06:09

secustor