passing variables between 2 stages, if stages have jobs in parallel

Problem to solve

I need to pass variables from Stage test to stage deploy.
Stage test has multiple jobs running in parallel(test0,test1,test2).
Every job(test) when succeeded, pass a variable called EVAL*=$CI_JOB_NAME (with * the number accorded to it [0,1,2]).
In deploy job should these variable in array stored(if any), and when we iterate through it we can see which test job has succeeded(according to name) and ready to deploy(merging to master). This approach is meant to be used with 50 tests in parallel. My Configuration is below, syntax is not relevant, it is only to demonstrate the idea.

test0:
  stage: test
  tags: [ci_test]
  script:
     - do something
     - if something: EVAL0=$CI_JOB_NAME
  artifacts:
    when: always 
test1:
  stage: test
  tags: [ci_test]
  script:
     - do something
     - if something: EVAL1=$CI_JOB_NAME
  artifacts:
    when: always
test2:
  stage: test
  tags: [ci_test]
  script:
     - do something
     - if something: EVAL2=$CI_JOB_NAME
  artifacts:
    when: always
deploy test:
  stage: deploy
  tags: [ci_test]
  script:
     - array+=($EVAL(i) for in (0,1,2)) 
     - for v in array; do echo v and do something with v;done
  artifacts:
    when: always

Question

Is it possible in Gitlab to do so, and if yes can you lead me through that procedure.

Hi, @samGit. The environment variables created during jobs are lost when the job finishes.

I would recommend saving your variables to files that can be collected by the GitLab Runner via the artifacts .gitlab-ci.yml attribute. The artifacts from all jobs will then be available to the job(s) in your next stage(s).

Here’s a minimal example: .gitlab-ci.yml · master · thiagocsf-group / copy-artefacts · GitLab

2 Likes

See also: artifacts:reports:dotenv.

2 Likes

What about a job and a template, are these considered a unique job? I have tried declaring the variable in the job and calling it in the template, but it doesn’t work.

deploy:
  stage: deploy
  script:
    - echo "DEPLOYING"
  only:
    refs:
      - tags
  variables:
    VAR_NUM: "0000001"
   <<: *job_deploy_template

.deploy_template: &job_deploy_template
  image: python:alpine
  stage: deploy
  script:
    - ...
    - ...
    - echo "$VAR_ENUM"

You have typo in there, that’s why it doesn’t work for you.

Heads up: In March 2024, the link is now https://docs.gitlab.com/ee/ci/yaml/artifacts_reports.html#artifactsreportsdotenv.