Milica Nikolić
Milica Nikolić

Reputation: 195

How can I pass value of variable from one job to next job in pipeline in GitLab CI?

Am I able to pass value from variable that I created in one job to next job so I can do some checks of that value in next job of the same stage ? I would like to have first job that creates some variable and then assigns value to that variable and next job, in same stage that would do check of that value ? I need this for specific use case in my pipeline ?

I was going through the documentation on GitLab and I couldn't find any recource that would help me with solving this case ?

Any help with this would be really appreciated. Thanks a lot! :)

Upvotes: 6

Views: 5364

Answers (1)

Patrick
Patrick

Reputation: 3230

Yes, you do this by using the dotenv file artifact. You'll create a file in one job that has a set of values in it like this:

FIRST_VAR=1234
SECOND_VAR=hello_world

Then set that as a dotenv type artifact according to the documentation, and that will make downstream jobs have those variables be set.

A pseudo-example .gitlab-ci.yml file would be:


generate-dot-env-file:
  image: alpine
  script:
    - echo "FIRST_VAR=1234" >> my_file
    - echo "SECOND_VAR=hello_world" >> my_file
  artifacts:
    reports:
      dotenv: my_file

read-dot-env-file:
  image: alpine
  # needed so it reads the report/artifacts
  needs: generate-dot-env-file
  script:
    - echo $FIRST_VAR #outputs 1234

Upvotes: 9

Related Questions