Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set gitlab-ci variables dynamically?

Tags:

gitlab-ci

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

like image 613
Bharath Thiruveedula Avatar asked Jun 13 '26 05:06

Bharath Thiruveedula


1 Answers

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 ..."

like image 162
Ivan Avatar answered Jun 18 '26 00:06

Ivan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!