12

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

user5580578
  • 1,134
  • 1
  • 12
  • 28

2 Answers2

18

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
danielnelz
  • 3,794
  • 4
  • 25
  • 35
1

There is a feature since long ago in gitlab for this, while the accepted answer will work and was the solution to this, since this feature it is much more convinient to achieve this.

You just simply declare variables in the trigger job and it will be passed to the downstream pipeline.

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

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

APPLICATION_NAME: "helloworld" and APPLICATION_VERSION: "none" variables will be available in the triggered pipeline running in the git-project/profile-uploader repo and you can simply reference them in the triggered pipeline's jobs as you would with any environment variables.

https://docs.gitlab.com/ee/ci/pipelines/downstream_pipelines.html#pass-cicd-variables-to-a-downstream-pipeline

https://gitlab.com/gitlab-org/gitlab/-/merge_requests/13197

zsolt
  • 1,233
  • 8
  • 18