Search code examples
gitlab-ci

Gitlab-CI: pass variable to a trigger stage?


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


Solution

  • 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