How to get the list of work items linked to a build pipeline via the REST API in Azure - azure-devops-rest-api

I'd like to get the list of work items (id) that are linked to a current build pipeline from some PowerShell script using the Azure Rest API, how can I do that?
Azure Devops Server 2019

How to get the list of work items linked to a build pipeline via the REST API in Azure
There is a out of box API to get the list of work items linked to a build pipeline:
Builds - Get Build Work Items Refs::
GET https://{instance}/{collection}/{project}/_apis/build/builds/{buildId}/workitems?api-version=5.0
The powershell script:
$url = "http://{instance}/{collection}/{project}/_apis/build/builds/2945/workitems?api-version=5.0"
$LinkedworkItems= Invoke-RestMethod -Uri $url -Headers #{
Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
} -Method Get
Write-Host "Linked work items= $($LinkedworkItems| ConvertTo-Json -Depth 100)"
Note: Need go to the Agent Phase and select Allow Scripts to Access OAuth Token:
The result:
Hope this helps.

Related

trigger the jenkins job from octopus

I want to call the jenkins job(automated smoke suite) automatically from the octopus(deployment tool), once the deployment is successful.
The simplest way of doing this I found is, to create a script that uses the Jenkins REST API to trigger the jenkins job. And then add that script as a step at the end of deployment in octopus.
Created a powershell script,
to call the REST API
Invoke-WebRequest -Uri "http://jenkinsserver/jenkins/job/Run%20a%20script/build" -Headers $BuildHeaders -Method Postenter code here
I am getting error " Invoke-WebRequest : HTTP ERROR 403 No valid crumb was included in the request".
In jenkins due to security issue I can not change configuration.
Please guide me to proceed.
There is a community step, Jenkins - Queue Job, for triggering a Jenkins job.
More information about Octopus Community Step Templates - https://octopus.com/docs/projects/community-step-templates

Unable to replace docker tag in release pipeline automatically

my build pipeline is in azureDevopsservices and my release pipeline in azuredevopsServer
build pipeline is creating docker image and keeping in image azure container registry and working fine
release pipeline is deploying docker image in AKS cluster with the help deployment object
but i am unable to replace image tag in my deployment file dynamically.
what i am looking for is once the build pipeline is done, release pipeline should trigger automcatically and it should replace the docker tag automatically.
I have tried using replace token task as well but it not working.
Based on your requirement, you could refer to the following process to automatically trigger the release on Azure Devops server and replace the tag with the variable.
my build pipeline is in azureDevopsservices and my release pipeline in azuredevopsServer
To automatically trigger the release in Azure Devops Server, you can refer to my reply in your other ticket.
Replace docker tag in release pipeline
As far as I know, the Replace token task can meet your requirement.
From your docker tag format, it seems that this is the $(build.buildnumber) variable in Azure Devops Service Build Pipeline.
You could set the variable in Release Pipeline and set Settable at release time option.
In deployment manifest, you could set the image with the following format: image:mycontainerregistry/myapp:#{Version}#
Use the Replace token task and select the target file.
When you use Rest API to trigger the release , you could pass the varible to release pipeline at the same time.
Here is the sample:
$token = "PAT"
$url = "https://{instance}/{collection}/{project}/_apis/release/releases?api-version=5.0"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$JSON = #'
{
"definitionId": ID,
"variables": {
"Version": {
"value": "$(Build.buildnumber)"
}
}
}
'#
$response = Invoke-RestMethod -Uri $url -Headers #{Authorization = "Basic $token"} -Method Post -ContentType application/json -body $JSON

How to trigger build after release is completed successfully in azure devops?

I have build and release definition in azure-devops. I want the build to be triggered after release is done, I don't repeat the steps, I want to trigger already existing build definition. I am trying to figure how to do this in azure-devops.
When I tell build it is end to end test. The idea is to run the end to end test after release is done. Note I already have E2E Tests build, here I have repeated the steps, instead I want to just refer the existing build and trigger it. Any suggestion or help on how to do this will be of great help.
This is not currently supported. This document has all Azure DevOps trigger events. Currently release completion cannot trigger a build.
As a workaround, we could open release definition and add task power shell at the end to call REST API to trigger build.
Power shell script:
$token = "{PAT}"
$url = "https://dev.azure.com/{Org name}/{project name}/_apis/build/builds?api-version=6.1-preview.6"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$JSON = #"
{
"definition": {
"id": {Build Definition ID}
}
}
"#
$response = Invoke-RestMethod -Uri $url -Headers #{Authorization = "Basic $token"} -Method Post -ContentType application/json -body $JSON
Also, we could add the extension Trigger Azure DevOps Pipeline
and then add task Trigger Azure DevOps Pipeline at the end of the job to trigger build.
Result:

How to setup concurrent build in azure devops service - tfs

I need to build the code one after another in TFS. When the first build pipeline is completed, the second build pipeline should be triggered automatically
If you are using Azure DevOps service:
You could simply chain related builds together using build completion triggers.
Add a build completion trigger to run your build upon the successful
completion of the triggering build. You can select any other build in
the same project.
After you add a build completion trigger, select the triggering build.
If the triggering build is sourced from a Git repo, you can also
specify branch filters. If you want to use wildcard characters, then
type the branch specification (for example, features/modules/*) and
then press Enter.
Source Link
If you are using on-premise and your TFS version do not support build completion triggers:
There are two ways to run another build in your current build.
Option 1: add PowerShell task in your current build definition to queue another build by REST API
Assume another build id is 5, so you can add PowerShell task with the script:
$body = #{
definition = #{
id = 5
}
}
$Uri = "http://account.visualstudio.com/DefaultCollection/project/_apis/build/builds?api-version=2.0"
$buildresponse = Invoke-RestMethod -Method Post -UseDefaultCredentials -ContentType application/json -Uri $Uri -Body (ConvertTo-Json $body)
Option 2: install related extension in Market place
There are some extensions you can install for your VSTS account, then you can add the task to queue another build. such as Queue Build(s) Task, Trigger New Build, Queue New Build etc.

TFS rest API - queue build on same machine

I am trying to see if it is possible to queue a build on the same VM as another build. Note, I am in the vNext system. I know it is possible to configuration information about the agent queues. but I want to get a little more specific than that. I'm not sure if what I want falls outside of build best practices, but I'd like to ask anyway.
Consider the following setup for builds A & B:
There are 4 agents: 2 on server 1, 2 on server 2 Any agent can pick up build A or build B .... I know how to set up the demands so only agents on those VMs can pick up this builds.
Build A uses the rest API to queue build B, and wait for it to complete. Right now, under current configurations, the spawned build B can be picked up by any of the 3 remaining agents on (so the free agent on the same machine as build A, or any of the agents on the other server).
I want to try to run build B on the same server as build A when build A launches build B. In other words, if an agent on server 1 picks up build A, I want the build B it launches to use the same server (using the other agent on the machine). Conversely, if the server 2 picks up build A, I want the build B it launches to be picked up by server 2 as well. Assuming I know things like the Agent ID, Agemt Machine Name, etc, I wanted to use the rest API in c# to simulating launching build B to try to control which machine picks it up.
I could configure agents so that both builds only run on 1 machine, but I am trying to avoid running too many agents on one machine. I could configure the above 4 agents to run on 1 machine, but I want to distribute the agents as much as possible. I know it is possible to specify information about agents queues / pools etc, bot not the machine. I am trying to avoid having to restrict the number of machines that run a set of agents.
I know this may seem a bit unusual ... but I am dealing with a scenario where I want to share information between the builds that will be simplified if they run on the same machine. If build B is launched by itself, it doesn't matter what machine picks it up. If this isn't possible, I will try other ways to share information between the builds.
Is it possible to do this?
Yes, you can achieve that by set up the demands with REST API when queue the build.
For example, you can use below PowerShell script to queue a build with the specific agent:
Param(
[string]$collectionurl = "http://server:8080/tfs/DefaultCollection/",
[string]$projectName = "ProjectName",
[string]$keepForever = "true",
[string]$BuildDefinitionId = "1",
[string]$user = "username",
[string]$token = "password"
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
function CreateJsonBody
{
$value = #"
{
"definition": {
"id": $BuildDefinitionId
},
"sourceBranch": "$/xxxx",
"demands":["Agent.Name -equals Agent1"]
}
}
"#
return $value
}
$json = CreateJsonBody
$uri = "$($collectionurl)/$($projectName)/_apis/build/builds?api-version=2.0"
$result = Invoke-RestMethod -Uri $uri -Method Post -Body $json -ContentType "application/json" -Headers #{Authorization=("Basic {0}" -f $base64AuthInfo)}
In your scenario: (Assume that Agent1 is the name of the agent, you can get it from Agent Capabilities)
Server1 : Agent1, Agent2
Server2 : Agent3, Agent4
If Build A queued with Agent1, then you an set "demands":["Agent.Name -equals Agent2"] to queue Build B.
Same thing, if Build A queued with Agent3, then you an set "demands":["Agent.Name -equals Agent4"] to queue Build B.
You can also create a simple PowerShell script and add a PowerShell step as the end of the build step in definition A, then run the PS script to queue build B once Build A completed.
If you want to queue build with the REST API in C#, then you can reference below thread:
Queue vNext build from c# using REST API (Tfs on premise)

Resources