Problem to solve
In my CI I want to conditionally pass environment variables between stages, but I also wanted to pass environment variables in a script section to another section of the job (see docs)
Use
$GITLAB_ENV
to pass environment variables defined in thescript
section to another section.
What I am observing is that the variables stored in the GITLAB_ENV
file on one job are cached between pipelines. It seems that its cached per runner.
- Is this expected behaviour?
- Is there a possibility that I could reset
GITLAB_ENV
? - Is there any more detailed docs or information on
GITLAB_ENV
?
If so, then the current way in which the documentation is written is misleading, because GITLAB_ENV
is more than just a variable to use to share env variables between sections but actually between pipeline jobs in the same runner!
Steps to reproduce
- Try to store an environment variable using
artifacts:reports:dotenv
andGITLAB_ENV
- Run the CI job on
branch_a
and then onbranch_b
- Ensure that the CI jobs run on different runners
Configuration
...
setup-el-env-on-branch:
image: docker:20
stage: init
script:
- cat "$GITLAB_ENV" # debug
- echo "PKG_TAG_NAME=$PKG_TAG_NAME" | tee -a $GITLAB_ENV >> build.env
- cat build.env # debug
- echo "PKG_TAG_NAME=$PKG_TAG_NAME" # debug
artifacts:
reports:
dotenv:
- build.env
variables:
PKG_TAG_NAME: "$CI_COMMIT_REF_SLUG"
package:
image: docker:20
stage: package
variables:
PKG_TAG_NAME: "$CI_COMMIT_REF_SLUG"
before_script:
- cat "$GITLAB_ENV"
- echo "PKG_TAG_NAME=$PKG_TAG_NAME"
- cat build.env
...
Versions
Please select whether options apply, and add the version information.
- Self-managed
- Dedicated
- Self-hosted Runners
Versions
- GitLab: v17.6.3-ee
- GitLab Runner, if self-hosted: 17.5.4