shahar
March 4, 2024, 3:23pm
1
Hey there,
I’m trying to write a .gitlab-ci.yml file, and I want to retrieve a variable from a text file.
The text file looks like this:
2024-02-25
Here’s the gitlab-ci.yml:
variables:
DOCKER_IMG_DEV_TAG: $(cat scripts/dev_image_tag.txt)
(^ This is not working ^)
This tag tells Kubernetes which image version it needs to use.
Any help will be appreciated.
variables
as configuration attribute are static at configuration parse time. If you want to populate the variables at runtime, environment variables can be helpful.
If the date is generated in a previous job, you could write it into a .env
artifact, and then load this variable into the environment in a follow-up job .
A more simplified approach could be exporting the environment variable using export
.
build-job:
script:
- export DOCKER_IMG_DEV_TAG=$(cat scripts/dev_image_tag.txt)
but can also be combined with .env.
build-job:
stage: build
script:
- echo "DOCKER_IMG_DEV_TAG=$(cat scripts/dev_image_tag.txt)" >> build.env
artifacts:
reports:
dotenv: build.env
test-job:
stage: test
script:
- echo "$DOCKER_IMG_DEV_TAG"
I’ve created an MR with a test pipeline in dot-env (!25) · Merge requests · Michael Friedrich / ci-cd-playground · GitLab
1 Like
shahar
March 7, 2024, 2:03pm
3
Hey,
Thanks for getting back to me so quickly.
Your advice really came in handy, and although I took a different approach to solve the problem, your guidance was super helpful.
1 Like
Glad you could solve it. Would you mind adding your solution to this thread, so that others can benefit from your approach? Thanks
1 Like
shahar
March 10, 2024, 8:40am
5
Sure,
but I just changed the text file to yaml and include it like this:
image_tag.yml:
variables:
DOCKER_IMG_TAG: "2024-03-09"
.gitlab-ci.yml:
include: 'image_tag.yml'
and then i use DOCKER_IMG_TAG as a regular variable.
1 Like
Oh nice, did not think of includes with variables
1 Like