Variable from text file

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

image

1 Like

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 :slight_smile:

1 Like

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 :slight_smile:

1 Like