是否可以在我的 Azure DevOps 构建管道 "publish artifact" 任务中有条件地设置项目名称?

Is it possible to conditionally set the artifact name in my Azure DevOps build pipeline "publish artifact" task?

我想知道是否可以在我的 Azure DevOps 构建管道 "publish artifact" 任务中有条件地设置我的构建工件的名称?我想根据构建管道的输入生成不同的工件。比如说,基于输入管道变量,我想生成三个工件之一("red"、"blue"、"green")。是否可以根据输入变量指定在我的 "publish artifact" 任务中生成的工件,还是 easier/better 只是生成三个构建管道?

Is it possible to conditionally set the artifact name in my Azure DevOps build pipeline “publish artifact” task?

恐怕没有这种开箱即用的方法。如果你想有条件地设置工件名称,我们必须在管道中使用嵌套变量。

但是,目前嵌套变量的值(如$(CustomArtifactName_$(Build.SourceBranchName))) 在构建管道中尚不支持.

作为解决方法,您可以添加 Run Inline Powershell 任务以根据输入管道变量设置变量。

在我这边,我使用Build_SourceBranchName作为输入管道变量。然后我在 Inline Powershell 任务中添加以下脚本:

- task: InlinePowershell@1
  displayName: 'Inline Powershell'
  inputs:
    Script:

     $branch = $Env:Build_SourceBranchName

     if ($branch -eq "TestA5")
     {
       Write-Host "##vso[task.setvariable variable=CustomArtifactName]Red"
     }
     else
     {
       Write-Host "##vso[task.setvariable variable=CustomArtifactName]Blue"
     }

然后在 Publish Build Artifacts 任务中,我将 ArtifactName 设置为 drop-$(CustomArtifactName)

- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact: drop'
  inputs:
    ArtifactName: 'drop-$(CustomArtifactName)'

希望对您有所帮助。

这是 bash 版本,以防您 运行 Linux 上的任务。 NAME 是自定义变量,它使用预定义变量作为值。

trigger_model_version=RELEASE_ARTIFACTS_${NAME^^}_BUILDNUMBER
export version=${!trigger_model_version}
echo Deploying ${NAME}:${version}