Search code examples
variablesgitlabgitlab-ci

How to save multiple variables in a apipeline for another job


So I am looking through the gitlab ci documentation for passing variables to other jobs. However, how would I pass more than one? And how would I access more than one?

build:
  stage: build
  script:
    - echo "BUILD_VERSION=hello" >> build.env
  artifacts:
    reports:
      dotenv: build.env

deploy:
  stage: deploy
  script:
    - echo "$BUILD_VERSION"  # Output is: 'hello'
  dependencies:
    - build

Solution

  • You can save several values in an artifact and than source it in a following job:

    build:
      stage: build
      script:
        - echo "BUILD_VERSION=hello" >> build.env
        - echo "ANOTHER_VARIABLE=world" >> build.env
      artifacts:
        reports:
          dotenv: build.env
    
    deploy:
      stage: deploy
      script:
        - source build.env
        - echo "$BUILD_VERSION"  # Output is: 'hello'
        - echo "$ANOTHER_VARIABLE"  # Outpur is 'world'
      dependencies:
        - build
    

    If you need to export the variables in the environment, just replace the source command by the following one:

      script:
        - export $(cat build.env | xargs)