3
votes

I was wondering if it is possible to conditionally set the name of my build artifact in my Azure DevOps build pipeline "publish artifact" task? I want to produce different artifacts based on the input to my build pipeline. Say, based on the input pipeline variables, I want to produce one of three artifacts ("red", "blue", "green"). Is it possible to specify the artifact being produced in my "publish artifact" task based on the input variable, or is it easier/better just to produce three build pipelines?

2
What is "input pipeline variables"? why you don't put the variable in the artifact name?Shayki Abramczyk

2 Answers

5
votes

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

I am afraid there is no such out of box way to do that. If you want to conditionally set the artifact name, we have to use the nested variables in the pipeline.

However, At this moment, the value of nested variables (like $(CustomArtifactName_$(Build.SourceBranchName))) are not yet supported in the build pipelines.

As workaround, you could add a Run Inline Powershell task to set the variable based on the input pipeline variables.

In my side, I use Build_SourceBranchName as input pipeline variables. Then I add following scripts in the Inline Powershell task:

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

Then in the Publish Build Artifacts task, I set the ArtifactName with drop-$(CustomArtifactName)

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

enter image description here

Hope this helps.

0
votes

This is the bash version in case you run the task on Linux. The NAME is a custom variable which uses the pre-defined variable as value.

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