0
votes

We have a product that we build through Azure DevOps. This product is made up of about 20 different services that each get built in their own pipelines. At the end of each pipeline we push the output (a docker image) to an Azure Container Registry using the Build ID as the ID for that image.

We then have a DevOps Release that deploys to our Test (and then Prod) environments. It looks for the latest build for each pipeline and uses that ID to determine the relevant image to deploy for each service.

I'm now trying to build a build a new DevOps Release that will allow our QA team to deploy a given Branch to a new QA environment. I want the user to be able to select a Branch and for DevOps to determine the last build for each pipeline for that specific branch - or if that branch has never been built for a given pipeline then to fallback to the last Master branch build for that pipeline.

I can't see how to add a Branch variable that will then re-calculate the Artifacts when triggering a manual build. And, while I can get it to find the latest build for a particular branch but no way of getting it to fall back to Master if no build has been done for that Branch.

Is it possible to do this? If not, is there another approach to achieving the same outcome?

1
Hi, Just checking in to see whether this issue is still blocking you now? Any update for this issue? If have any misunderstanding, could you re-describe more details about scenario?Vito Liu
I've got it working. However using Node.JS to determine the build ID rather than bash script as the bash script provided didn't work. Thanks for the help!Matthew van Boheemen
Hi, If this answer is helpful, would you please accept it as the answer? Or kindly share your solution here and accept it. So it could help other community members who get the same issues and we could archive this thread. Thanks. Have a nice day. :)Vito Liu

1 Answers

1
votes

I'm now trying to build a build a new DevOps Release that will allow our QA team to deploy a given Branch to a new QA environment. I want the user to be able to select a Branch and for DevOps to determine the last build for each pipeline for that specific branch

You are creating a new release pipeline definition to deploy QA environment, and this release pipeline only deploy QA environment, right? If yes, we could configure the stage Pre-deployment conditions. Steps: Open Pre-deployment conditions->enable Artifact filters, check the pic below.

enter image description here

I can't see how to add a Branch variable that will then re-calculate the Artifacts when triggering a manual build. And, while I can get it to find the latest build for a particular branch but no way of getting it to fall back to Master if no build has been done for that Branch.

We could add task bash and enter the script printenv to list all variable and check the build source branch. Check the below sample, we could get the latest master branch build ID if no build has been done for that Branch.

$URL="https://dev.azure.com/{Org name}/{project name}/_apis/build/Builds?branchName=refs/heads/{target branch}&definitions={build definition ID}"
$PAT="{PAT}"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))

$Result = Invoke-RestMethod -Uri $URL -Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -Method get
If($Result.count -eq "0")
{
#back to Master if no build has been done for the target Branch 
$MasterBranchURL = "https://dev.azure.com/{Org name}/{project name}/_apis/build/Builds?branchName=refs/heads/master&definitions={build definition ID}"
$MasterBranchResult = Invoke-RestMethod -Uri $MasterBranchURL -Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -Method get

    foreach($Build in $MasterBranchResult.value){
       If($Build.result -eq "succeeded"){
       #Get the master branch latest success build ID
       $ID = $Build.id
       echo "##vso[task.setvariable variable=BuildID;isOutput=true]$ID"
       break
       }
    }
}