Yogesh
Yogesh

Reputation: 303

How to use environment variable into parallel stages of the GitLab pipeline

I have created a sample pipeline where I want to execute multiple stages (staticscan, integration, unittest) in parallelly. In .pre stage I have created a environment variable which will get used in all the stages. When the pipeline got executed then that environment variable is not available in parallel stages. If I have changed the pipeline configuration to execute all the stages sequentially then the environment variable will be available in all the stages.

Is there any way to access the environment variable created in first stage into rest of the stages of the pipeline?

stages:
  - pre_test 
  - download
  - build
  - staticscan
  - integration
  - unittest
  - validate
   
pre_test:
  stage: .pre
  script:
    - DATE_TIME="$(date -d $CI_PIPELINE_CREATED_AT +$DATE_FORMAT)"
    - echo "DATE_TIME=$DATE_TIME"
    - echo "DATE_TIME=$DATE_TIME" >> build.env
  after_script:
    - >
        if [ $CI_JOB_STATUS == 'success' ]; then
            echo "PRE_TEST_STATUS=success" >> build.env
        else
            echo "PRE_TEST_STATUS=failed" >> build.env
        fi
  artifacts:
    reports:
      dotenv: build.env
download:
  stage: download
  script:
    - echo "===== Download starts ====="
    - echo "===== Download Ends ====="
  tags:
    - linux
  after_script:
    - >
        if [ $CI_JOB_STATUS == 'success' ]; then
            echo "DOWNLOAD_STATUS=success" >> build.env
        else
            echo "DOWNLOAD_STATUS=failed" >> build.env
        fi
  artifacts:
    reports:
      dotenv: build.env
build:
  stage: build
  script:
    - echo "===== Build starts ====="
    - echo "===== Build Ends ====="
  tags:
    - linux
  after_script:
    - >
        if [ $CI_JOB_STATUS == 'success' ]; then
            echo "BUILD_STATUS=success" >> build.env
        else
            echo "BUILD_STATUS=failed" >> build.env
        fi
  artifacts:
    reports:
      dotenv: build.env
staticscan:
  stage: staticscan
  needs:
    - jobs: build
      artifacts: true        
  script:
    - echo "===== staticscan starts ====="
    - echo "---- ${DATE_TIME} ----"
    - echo "===== staticscan Ends ====="
  tags:
    - linux
  after_script:
    - >
        if [ $CI_JOB_STATUS == 'success' ]; then
            echo "STATICSCAN_STATUS=success" >> build.env
        else
            echo "STATICSCAN_STATUS=failed" >> build.env
        fi
  artifacts:
    reports:
      dotenv: build.env
integration:
  stage: integration
  needs:
    - jobs: build
      artifacts: true        
  script:
    - echo "===== integration starts ====="
    - echo "---- ${DATE_TIME} ----"     
    - echo "===== integration Ends ====="
  tags:
    - linux
  after_script:
    - >
        if [ $CI_JOB_STATUS == 'success' ]; then
            echo "INTEGRATION_STATUS=success" >> build.env
        else
            echo "INTEGRATION_STATUS=failed" >> build.env
        fi
  artifacts:
    reports:
      dotenv: build.env
unittest:
  stage: unittest
  needs:
    - jobs: build
      artifacts: true        
  script:
    - echo "===== unittest starts ====="
    - echo "---- ${DATE_TIME} ----"
    - echo "===== unittest Ends ====="
  tags:
    - linux
  after_script:
    - >
        if [ $CI_JOB_STATUS == 'success' ]; then
            echo "UNITTEST_STATUS=success" >> build.env
        else
            echo "UNITTEST_STATUS=failed" >> build.env
        fi
  artifacts:
    reports:
      dotenv: build.env
validate:
  stage: validate
  script:
    - echo "===== validate starts ====="
    - echo "===== validate Ends ====="
  tags:
    - linux
  after_script:
    - >
        if [ $CI_JOB_STATUS == 'success' ]; then
            echo "VALIDATE_STATUS=success" >> build.env
        else
            echo "VALIDATE_STATUS=failed" >> build.env
        fi
  artifacts:
    reports:
      dotenv: build.env

Upvotes: -1

Views: 29

Answers (1)

Kynes
Kynes

Reputation: 46

Stages are groups of one or more jobs. Stages run sequentially one after another.

Jobs in one stage run parallel by default (may be changed by needs/rules).

So, the first stage should have only one job creating env variable. Further stages may have many jobs (run parallel) but since it will be next stages, env variable should be accessible.

stages:
- preparation
- execution

job1_create_env:
  stage: preparation
  script: # create env here

job2:
  stage: execution
  script: ...

job3
  stage: execution
  script: ..

job2 & job3 will run parallel but after job1

Upvotes: 1

Related Questions