Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass variables between gitlab-ci jobs?

I have a gitlab-ci like this:

stages:
- calculation
- execution

calculation-job:
  stage: calculation
  script: ./calculate_something_and_output_results.sh
  tags:
  - my-runner

execution-job:
  stage: execution
  script: ./execute_something_with_calculation_results.sh foo
  tags:
  - my-runner

The foo argument in execution-job is base on the results of calculation-job. I want to pass the results from one job to another job via variables. How can I do that?

like image 671
Hank Chow Avatar asked Dec 07 '25 13:12

Hank Chow


2 Answers

As a simpler version of what @bjhend answered (no need for export or source statements), since GitLab 13.1 the docs. recommend using a dotenv artifact.

stages:
- calculation
- execution

calculation-job:
  stage: calculation
  script:
  # Output format must feature one "VARIABLE=value" statement per line (see docs.)
  - ./calculate_something_and_output_results.sh >> calculation.env
  tags:
  - my-runner
  artifacts:
    reports:
      dotenv: calculation.env

execution-job:
  stage: execution
  script:
  # Any variables created by above are now in the environment
  - ./execute_something_with_calculation_results.sh
  tags:
  - my-runner
  # The following is technically not needed, but serves as good documentation
  needs: 
    job: calculation-job
    artifacts: true

If you have a job after the calculation stage that you don't want to use the variables, you can add the following to it:

  needs: 
    job: calculation-job
    artifacts: false
like image 70
esmail Avatar answered Dec 10 '25 03:12

esmail


If you're looking to get the results without storing a file anywhere you can use artifacts: reports: dotenv. This is taken entirely from DarwinJS shared-variables-across-jobs repo.

stages:
  - calculation
  - execution

calculation-job:
  stage: calculation
  script: - |
      # stores new or updates existing env variables, ex. $OUTPUT_VAR1
      ./calculate_something_and_output_results.sh >> deploy.env
  tags:
    - my-runner
  artifacts:
    reports:
      #propagates variables into the pipeline level, but never stores the actual file
      dotenv: deploy.env

execution-job:
  stage: execution
  script: - |
      echo "OUTPUT_VAR1: $OUTPUT_VAR1"
      ./execute_something_with_calculation_results.sh foo
  tags:
    - my-runner
like image 30
tlbignerd Avatar answered Dec 10 '25 02:12

tlbignerd