Sobes.tech
Middle

How to transfer a variable between jobs in a CI/CD process?

sobes.tech AI

Answer from AI

Variables are usually transferred between jobs in a CI/CD process using artifacts or environment variables. Let's consider typical methods:

  1. Artifacts: The previous job saves the variable value in a file, which is declared as an artifact. The next job downloads this artifact and reads the value from the file. Suitable for large data volumes or files.

    # .gitlab-ci.yml (example for Gitlab CI)
    job_producer:
      stage: build
      script:
        - echo "MY_VARIABLE_VALUE" > variable.txt
      artifacts:
        paths:
          - variable.txt
    
    job_consumer:
      stage: deploy
      dependencies:
        - job_producer
      script:
        - MY_VARIABLE=$(cat variable.txt)
        - echo "Variable value is: $MY_VARIABLE"
    
  2. Environment variables: Some CI/CD systems allow setting environment variables for subsequent jobs directly from the current job's script.

    • Gitlab CI: Use artifacts:reports:dotenv.

      # .gitlab-ci.yml (example with dotenv)
      job_producer:
        stage: build
        script:
          - echo "MY_VARIABLE=MY_VARIABLE_VALUE" > variables.env
        artifacts:
          reports:
            dotenv: variables.env
      
      job_consumer:
        stage: deploy
        needs:
          - job: job_producer
            artifacts: true # Ensure artifacts are uploaded
        script:
          - echo "Variable value is: $MY_VARIABLE" # Variable is available directly
      
    • Jenkins Pipeline: Use stash and unstash functions for files or env for environment variables.

      // Jenkinsfile (example with env)
      pipeline {
          agent any
          stages {
              stage('Producer') {
                  steps {
                      script {
                          env.MY_VARIABLE = "MY_VARIABLE_VALUE"
                      }
                      echo "Set MY_VARIABLE in producer: ${env.MY_VARIABLE}"
                  }
              }
              stage('Consumer') {
                  steps {
                      echo "Value in consumer: ${env.MY_VARIABLE}" // Variable is available
                  }
              }
          }
      }
      

      or with stash/unstash:

      // Jenkinsfile (example with stash/unstash)
      pipeline {
          agent any
          stages {
              stage('Producer') {
                  steps {
                      sh 'echo "MY_VARIABLE_VALUE" > variable.txt'
                      stash name: 'variable_file', includes: 'variable.txt'
                  }
              }
              stage('Consumer') {
                  steps {
                      unstash 'variable_file'
                      script {
                         def myVariable = readFile 'variable.txt'
                         echo "Value in consumer: ${myVariable}"
                      }
                  }
              }
          }
      }
      
  3. CI/CD-specific APIs: Some platforms provide APIs for saving and retrieving variables. This is a less common method for standard cases.

  4. Shared volume: When jobs run on the same host or cluster, a shared volume can be used for temporary data storage.

The choice of method depends on the CI/CD platform used, as well as the type and volume of data being transferred. Artifacts and environment variables via platform-specific mechanisms (dotenv, set +e) are the most common and reliable approaches.