首页 > 解决方案 > 当一个管道触发另一个管道时如何获取 customSourceVersion 的值?

问题描述

我是天蓝色管道的新手。我正在使用 Azure devops 管道来触发另一个 devops 管道。管道是用 YAML 模式编写的。

管道 A

- task: TriggerBuild@3
    displayName: Trigger Setup Pipeline
    inputs:
      definitionIsInCurrentTeamProject: true
      buildDefinition: '111'
      queueBuildForUserThatTriggeredBuild: true
      ignoreSslCertificateErrors: false
      useSameSourceVersion: false
      useCustomSourceVersion: true
      customSourceVersion: '$(Revision)'
      useSameBranch: true
      waitForQueuedBuildsToFinish: true
      waitForQueuedBuildsToFinishRefreshTime: '60'
      failTaskIfBuildsNotSuccessful: true
      cancelBuildsIfAnyFails: false
      treatPartiallySucceededBuildAsSuccessful: false
      downloadBuildArtifacts: false
      storeInEnvironmentVariable: false
      authenticationMethod: 'OAuth Token'
      password: '$(PersonalToken)'
      enableBuildInQueueCondition: false
      dependentOnSuccessfulBuildCondition: false
      dependentOnFailedBuildCondition: false
      checkbuildsoncurrentbranch: false
      failTaskIfConditionsAreNotFulfilled: false

管道 B

- task: DownloadBuildArtifacts@0
  condition: and(succeeded(), eq(variables['WCBuildType'], 'Internal'))
  inputs:
    buildType: 'specific'
    project: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
    pipeline: '999'
    buildVersionToDownload: 'latestFromBranch'      # I want to use '$(Revision)' vaue here
    branchName: '$(Build.SourceBranch)'
    downloadType: 'single'
    artifactName: 'Compiled'
    downloadPath: 'Artifact'
  displayName: 'Download build artifacts'

我想在管道 B 中使用管道 A 的 $(Revision) 值来下载特定构建版本的工件。我还希望 Pipeline B 的触发器在这两种情况下表现不同:

  1. 每当用户在他的分支中进行任何提交时,它都应该使用 buildVersionToDownload: latestFromBranch
  2. 当用户触发管道 A 时,管道 B 被触发并使用 buildVersionToDownload: $(Revision) 下载构建工件。

标签: azureazure-devopsyamlazure-pipelinesazure-pipelines-yaml

解决方案


TriggerBuild@3任务中添加这一行:

buildParameters: 'Revision: $(Revision)'

它将变量发送到触发的构建。

现在,在 Pipeline B 中,您可以创建if来检查用户是否进行了提交或触发了管道:

${{ if eq(variables['Build.Reason'], 'IndividualCI') }}:
  buildVersionToDownload: 'latestFromBranch'
${{ if ne(variables['Build.Reason'], 'IndividualCI') }}:
  buildVersionToDownload: '$(Revision)'

推荐阅读