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

2

5

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?

Covariance answered 17/5, 2019 at 22:53 Comment(1)
What is "input pipeline variables"? why you don't put the variable in the artifact name?Koski
M
9

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.

Makeyevka answered 20/5, 2019 at 6:9 Comment(2)
Still not supported on Sep 21?Rodent
still not working june 23..Camilla
P
0

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}
Parachronism answered 16/2, 2020 at 12:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.