Can 2 GitLab jobs save variables in the same .env file?

Viewed 326

I have 2 GitLab CI/CD Jobs doing some stuff that generates variables.

For example, the first job saves in a .env file

job1: 
  script:
    - echo "A_VARIABLE=${A_VARIABLE}" >> job.env
  artifacts:
    reports:
      dotenv:
        - job.env

And so does the second job.

job2: 
  script:
    - echo "ANOTHER_VARIABLE=${ANOTHER_VARIABLE}" >> job.env
  artifacts:
    reports:
      dotenv:
        - job.env

But can it save to that same job.env file without scratching existing data ? I know I can just group the 2 jobs but I would prefer not mixing them to make it cleaner

1 Answers

I didn't test it, but this should work from my understanding. Saving the file to an artifact will restore it before job2 starts. So it should be possible to append to the file.

Probably would test it with some simpler structure like this

job2: 
  script:
    - echo "ANOTHER_VARIABLE=${ANOTHER_VARIABLE}" >> job.env
  artifacts:
    paths:
        - job.env
Related