Gitlab-CI:将变量传递给触发阶段?

use*_*578 14 gitlab-ci

如何在“create_profile”阶段计算变量,然后在下一阶段“trigger_upload”中接管该变量。

我的第二个管道“git-project/profile-uploader”在“trigger_upload”阶段触发,应该获取此变量。

这是我目前的方法:

---

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
Run Code Online (Sandbox Code Playgroud)

目前,管道“git-project/profile-uploader”按预期获取 PROFILE_NAME“hello world”,而 PROFILE_VERSION 的值为“none”。PROFILE_VERSION 的值应为“0.1.0” - 在“create_profile”阶段计算。

dan*_*elz 21

您需要通过dotenv报告工件传递变量,如本答案中所述。因此,应用于您的示例时,管道将如下所示:

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
Run Code Online (Sandbox Code Playgroud)