From 3857fce64d1e9033b3017c2ab2b91922a9ec36bc Mon Sep 17 00:00:00 2001 From: Adam Cohen Date: Fri, 26 Apr 2024 21:40:46 +1000 Subject: [PATCH 1/2] Add docs for passing env vars between sections --- doc/ci/variables/index.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/doc/ci/variables/index.md b/doc/ci/variables/index.md index 1e49880248f058..5122bb60ec5339 100644 --- a/doc/ci/variables/index.md +++ b/doc/ci/variables/index.md @@ -576,6 +576,43 @@ test-job6: - build-job2 ``` +### Pass an environment variable from the `script` section to another section in the same job + +Use `$GITLAB_ENV` to pass environment variables defined in the `script` section to another section. + +For example: + +```yaml +build-job: + stage: build + script: + - echo "ARCH=$(arch)" >> $GITLAB_ENV + - touch some-file-$(arch) + artifacts: + paths: + - some-file-$ARCH +``` + +If the variable also needs to be referenced in other stages, you'll need to write the variable to both the `$GITLAB_ENV` and `.env` files: + +```yaml +build-job: + stage: build + script: + - echo "ARCH=$(arch)" | tee >> $GITLAB_ENV build.env + - touch some-file-$(arch) + artifacts: + paths: + - some-file-$ARCH + reports: + dotenv: build.env + +release-job: + stage: release + script: + - curl --upload-file some-file-$ARCH "https://example.com/some-file-$ARCH" +``` + ### Store multiple values in one variable You cannot create a CI/CD variable that is an array of values, but you -- GitLab From c9d8d44ef50a29cd02ca16c812069e1a10826f56 Mon Sep 17 00:00:00 2001 From: Adam Cohen Date: Mon, 29 Apr 2024 03:02:27 +0000 Subject: [PATCH 2/2] Apply 1 suggestion(s) to 1 file(s) Co-authored-by: Russell Dickenson --- doc/ci/variables/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/ci/variables/index.md b/doc/ci/variables/index.md index 5122bb60ec5339..76b1efa3e70879 100644 --- a/doc/ci/variables/index.md +++ b/doc/ci/variables/index.md @@ -593,7 +593,7 @@ build-job: - some-file-$ARCH ``` -If the variable also needs to be referenced in other stages, you'll need to write the variable to both the `$GITLAB_ENV` and `.env` files: +To also reference the variable in other stages, write the variable to both the `$GITLAB_ENV` and `.env` files: ```yaml build-job: -- GitLab