'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 1:[1]

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1