Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitlab-CI: pass variable to a trigger stage?

Tags:

gitlab-ci

How can I calculate a variable in the stage "create_profile", which should then be taken over in the next stage "trigger_upload".

My second pipeline "git-project/profile-uploader" which gets triggered in the stage "trigger_upload" should get this variable.

This is my current approach:

---

variables:
  APPLICATION_NAME: "helloworld"
  APPLICATION_VERSION: "none"

create_profile:
stage: build
script:
 # calculate APPLICATION_VERSION
 - APPLICATION_VERSION="0.1.0"
 - echo "Create profile '${APPLICATION_NAME}' with version '${APPLICATION_VERSION}'"
artifacts:
  paths:
   - profile

trigger_upload:
  stage: release
  variables:
    PROFILE_NAME: "${APPLICATION_NAME}"
    PROFILE_VERSION: "${APPLICATION_VERSION}"
  trigger:
    project: git-project/profile-uploader
    strategy: depend

At the moment the pipeline "git-project/profile-uploader" gets the PROFILE_NAME "hello world" as expected and the PROFILE_VERSION has the value "none". PROFILE_VERSION should have the value "0.1.0" - calculated in the stage "create_profile".

like image 209
user5580578 Avatar asked May 12 '21 10:05

user5580578


People also ask

How do you pass variables in GitLab CI?

An alternative is to use Gitlab Variables. Go to your project page, Settings tab -> CI/CD, find Variables and click on the Expand button. Here you can define variable names and values, which will be automatically passed into the gitlab pipelines, and are available as environment variables there.

How do you pass variables between jobs in GitLab?

Passing environment variables (or other data) between CI jobs is now possible. By using the dependencies keyword (or needs keyword for DAG pipelines), a job can inherit variables from other jobs if they are sourced with dotenv report artifacts.


1 Answers

You need to pass the variable via dotenv report artifacts as described in this answer. So applied to your example the pipeline will look like this:

variables:
  APPLICATION_NAME: "helloworld"
  APPLICATION_VERSION: "none"

stages:
  - build
  - release

create_profile:
  stage: build
  script:
  # calculate APPLICATION_VERSION
    - echo "APPLICATION_VERSION=0.1.0" >> build.env
    - echo "Create profile '${APPLICATION_NAME}' with version '${APPLICATION_VERSION}'"
  artifacts:
    paths:
      - profile
    reports:
      dotenv: build.env
    
trigger_upload:
  stage: release
  variables:
    PROFILE_NAME: "${APPLICATION_NAME}"
    PROFILE_VERSION: "${APPLICATION_VERSION}"
  trigger:
    project: git-project/profile-uploader
    strategy: depend
  needs:
    - job: create_profile
      artifacts: true
like image 134
danielnelz Avatar answered Oct 12 '22 08:10

danielnelz