Changed build number is not shown at the environment where application was deployed - azure-devops

I have build that is used as release pipeline written in yaml. It takes resources out of the build pipeline and deploys it to an environment.
Build details after running update build task
Inside the build I'm runnig this task that is able to change build number.
##vso[build.updatebuildnumber]$versionValue
However in the env view it shows build number in default format #20230131.3 on BuildName
Environment view
If I run this request:
$releaseUri = "https://dev.azure.com/$($organization)/$($project)/\_apis/build/builds/4577?api-version=7.0"
Invoke-RestMethod -Uri $releaseUri -Method Get -ContentType "application/json" -Headers #{Authorization=("Basic {0}" -f $base64AuthInfo)}
I'm getting correct build number
buildNumber : rel-2023.1.30-4448-7a8e9f4
But why it is not shown on environment overview for a given deployment?
I tried updating manually buildNumber
[$updateBuildUri = "https://dev.azure.com/$($organization)/$($project)/_apis/build/builds/4595?api-version=7.0"
$body = "{
`"buildNumber`": `"Adam1234`"
}"
Invoke-RestMethod -Uri $updateBuildUri -Method PATCH -ContentType "application/json" -Headers #{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body $body][1]
but without any results at the environment view.

Related

From where I get all publish artifact of all stages of azure build pipeline instead of searching from all stages

I am publishing build artifact of each run over azure pipeline, whatever artifact I have published than can be accessible to that specific stage(run), But my query is in Azure is there any build artifacts repository where we can get all the build artifacts for all the builds of that pipeline ?
To get all the build artifacts for all pipeline runs, you need to combine the following two Rest APIs: Builds - List and Artifacts - List
Here is a PowerShell example:
$token = "PAT"
$url="https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=6.0"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$response = Invoke-RestMethod -Uri $url -Headers #{Authorization = "Basic $token"} -Method Get -ContentType application/json
ForEach( $buildid in $response.value.id )
{
echo $buildid
$url1= "https://dev.azure.com/{organization}/{project}/_apis/build/builds/$($buildid)/artifacts?api-version=4.1"
$response1 = Invoke-RestMethod -Uri $url1 -Headers #{Authorization = "Basic $token"} -Method Get -ContentType application/json
echo $response1 | ConvertTo-Json
}
This sample will traverse all pipeline runs and get relevant artifacts information.
Update:
When you use Classic Pipeline, you could add a PowerShell task and input the PowerShell Script. Please change the parameters in the script to make it suitable for your organization

Azure DevOps pipelines: Cancel multiple pending jobs in queue

In Azure DevOps pipelines, How do I cancel all pending jobs for a job pool. I've got lots queued and couldn't see where I can cancel all the jobs I have waiting.
Azure devops doesnot yet have this feature to cancel all the pending jobs in batch from the UI partal.
You can write scripts to call rest api to cancel all the pending jobs as walkaround. Check out below steps:
First, use list build rest api to get all the pending jobs.
https://dev.azure.com/{organization}/{project}/_apis/build/builds?statusFilter=notStarted&api-version=5.1
Then, use update build api to cancel the pending jobs:
PATCH https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}?api-version=5.1
See below powershell scripts for reference:
Check here to get a Personal access token that will be used in below scripts.
$url= "https://dev.azure.com/{organization}/{project}/_apis/build/builds?statusFilter=notStarted&api-version=5.1"
$pat="Personal Access Token"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($pat)"))
$pendingJobs=Invoke-RestMethod -Uri $url-Headers #{Authorization = ("Basic {0}" -f $base64AuthInfo)} -Method get -ContentType "application/json"
$jobsToCancel = $pendingJobs.value
#Pending jobs donot consume the job agents in the agent pool. To filter the definition name to cancel pending jobs for a particular pipeline, you can use below filter criteria.
#$jobsToCancel = $pendingJobs.value | where {$_.definition.Name -eq "{Name of your pipeline }"}
#call update api to cancel each job.
ForEach($build in $jobsToCancel)
{
$build.status = "Cancelling"
$body = $build | ConvertTo-Json -Depth 10
$urlToCancel = "https://dev.azure.com/{organization}/{project}/_apis/build/builds/$($build.id)?api-version=5.1"
Invoke-RestMethod -Uri $urlToCancel -Method Patch -ContentType application/json -Body $body -Header #{Authorization = ("Basic {0}" -f $base64AuthInfo)}
}
You can also submit a new feature request(Click Suggest a feature and choose azure devops) to Microsoft development team for supporting cancelling pending jobs in batch. Hopefully they will consider adding this feature in the future sprint.
I find that using v6 of the api works, but instead of PATCH use DELETE.
(Reusing some of the code from #Levi Lu-MSFT)
$url= "https://dev.azure.com/{organization}/{project}/_apis/build/builds?statusFilter=notStarted&api-version=6.0-preview"
$pendingJobs=Invoke-RestMethod -Method GET -UseDefaultCredentials -Uri $url -ContentType "application/json"
$jobsToCancel = $pendingJobs.value
#$jobsToCancel = $pendingJobs.value | Where {$_.definition.Name -eq "{Name of your pipeline }"}
ForEach($build in $jobsToCancel)
{
$urlToCancel = "https://dev.azure.com/{organization}/{project}/_apis/build/builds/$($build.id)?api-version=6.0-preview"
Invoke-RestMethod -Uri $urlToCancel -Method DELETE -UseDefaultCredentials -ContentType application/json -Body $body
}

How to get status of the release (success, failure) with powershell using Azure DevOps Rest API

I am using post method to create release in Azure DevOps:
$url = "https://vsrm.dev.azure.com/"+$organization+"/"+$project+"/_apis/release/releases?api-version=5.1"
$body = #{definitionId = 9} | ConvertTo-Json -Depth 4
Invoke-RestMethod -Uri $url -Method POST -Body $body -ContentType "application/json" -Headers #{Authorization = ("Basic {0}" -f $base64AuthInfo)}
My pipeline is simple with single stage which executes automatically. Release creation and execution works fine.
But how to get status of my stage/s using powershell and REST API. I need to keep executing script while Release is in progress and initiate script failure if any of the stages fails.
Any ideas?
Using DefinitionID and EnvironmentIDs (you will get this from your initial POST response), you can query the last release and check for "deploymentStatus" value in intervals and exit when the status changes to "succeeded/failed/cancelled".
(collectionURL)/(teamproject)/_apis/Release/deployments?definitionId="+RELEASE_DEFINITIONID+"&definitionEnvironmentId="+RELEASE_DEFINITIONENVIRONMENTID?api-version=1.0

Equivalent for TeamCity's system.build.start.date in Azure Pipelines

Is there an equivalent for the TeamCity variable system.build.start.date in Azure Pipelines? I couldn't find it in the predefined variables (https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=vsts)
It doesn't appear like there is. It is available on the Get Builds API though. You could add a custom PowerShell task as the first step and use it to set a variable for future tasks to reference within the same Agent phase.
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes((":{0}" -f $env:SYSTEM_ACCESSTOKEN)))
$uri = "https://dev.azure.com/{oraganization}/{project}/_apis/build/builds/$($env:BUILD_BUILDID)?api-version=5.0"
$buildStartTime = Invoke-RestMethod -Uri $uri -Method Get -Headers #{Authorization=("Basic {0}" -f $base64AuthInfo)} |
Select-Object -ExpandProperty startTime
Write-Host "##vso[task.setvariable variable=BuildStartTime;]$buildStartTime"

VSTS build history for a Build definition

I have a VSTS build definitions in our project, Can i get the build results (passed/failed) that ran using that definition (builds started running using the definition since last 6 months) ?
Currently i am only getting last 20 build information ran from that build definition.
You can get it through Build REST API with PowerShell: Get a list of builds.
For example:
param(
[string]$sinceDate,
[string]$token,
[string]$defId
)
$uri="https://[acccount.visualstudio.com/DefaultCollection/[project]/_apis/build/builds?definitions=$defId&deletedFilter=1&minFinishTime=$sinceDate"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "test",$token)))
$result= Invoke-RestMethod -Method Get -Uri $Uri -ContentType "application/json" -Headers #{Authorization=("Basic {0}" -f $base64AuthInfo)}
$statusResult=$result.value | select-object -Property id,buildNumber,result
Arguments:
-sinceDate "12/1/2017" -token "[personal access token]" -defId "94"