7

How to set gitlab-ci varibales through script not just in "varibales" section in .gitlab-ci.yaml?So that I can set variables in one job and use in different job

3 Answers3

10

There is currently no way in GitLab to pass environment variable between stages or jobs.

But there is a request for that: https://gitlab.com/gitlab-org/gitlab/-/issues/22638

Current workaround is to use artifacts - basically pass files.
We had a similar use case - get Java app version from pom.xml and pass it to various jobs later in the pipeline.

How we did it in .gitlab-ci.yml:

stages:
  - prepare
  - package

variables:
  VARIABLES_FILE: ./variables.txt  # "." is required for image that have sh not bash

get-version:
  stage: build
  script:
    - APP_VERSION=...
    - echo "export APP_VERSION=$APP_VERSION" > $VARIABLES_FILE
  artifacts:
    paths:
      - $VARIABLES_FILE
package:
  stage: package
  script:
    - source $VARIABLES_FILE
    - echo "Use env var APP_VERSION here as you like ..."

Ivan
  • 9,089
  • 4
  • 61
  • 74
1

If you run a script you can set an environment variable

export MY_VAR=the-value

once the environment variable is set it should persist in the current environment.

Now for why you do not want to do that.

A tool like Gitlab CI is meant to achieve repeatability in your artifacts. Consistency is the matter here. What happens if a second job has to pick up a variable from the first? Then you have multiple paths!

# CI is a sequence
first -> second -> third -> fourth -> ...

# not a graph

first -> second A -> third ...
     \> second B />

How did you get to third? Now if you had to debug third which path do you test? If the build in third is broken who is responsible second A or second B?

If you need a variable use it now, not later in another job/script. Whenever you want to write a longer sequence of commands make it a script and execute the script!

edoput
  • 1,212
  • 9
  • 17
0

You can use either Artifact or Cache to achieve this, see the official documentation for more information around Artifact and Cache:

https://docs.gitlab.com/ee/ci/caching/#how-cache-is-different-from-artifacts

kwick
  • 333
  • 3
  • 9