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.
Just use artifacts, store the values in file then pass it to next job.
Jobs and pipelines as environments The variables can be stored in the project/group/instance settings and be made available to jobs in pipelines.
There are two places defined variables can be used.
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With