Get Azure Devops Pipeline Step Runtimes - rest

Looking through the Azure Devops APIs it appears like it isn't possible to get the runtime of individual steps in a pipeline run?
Pipeline steps
I'm looking to scrap the runtime of each step in a pipeline run across all runs of a pipeline.
I've looked at the Azure Devops APIs, and while I see how to get pipeline run details here, it doesn't appear like the API includes step level information.

You can use Timeline - Get REST API to achieve the runtime of individual steps in a pipeline run. Please refer to doc:Timeline-GET-REST APT
For example:
URL
GET https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}/timeline/{timelineId}?api-version=5.1
Test in postman:
The response body returns a list of build details that include job/step/task type and name and startTime and finishTime according timeline.

Related

Querying the agent a build step was run on from Azure DevOps via REST API

I need to extract information from a finished pipeline build in Azure DevOps. I can get the tree of steps in the executed plan for that build and the attached logs. The final piece of information that I'm looking for is the name of the build agent that was used to execute a certain step. How can I query the agent? The web frontend clearly shows the info, but I don't see it exposed via the documented API.
Querying the agent a build step was run on from Azure DevOps via REST API
First, there are Use predefined variables, which provide a list of predefined variables. You could use the Agent.Name to get the name of the build agent.
If you want to use REST API to get the name of it, you could use the REST API Builds - Get Build Log:
GET https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}/logs/{logId}?api-version=6.0
Then we need to parse the log to get the name of the agent:

Azure Devops Service Now - how to get CHANGE_REQUEST_NUMBER/CHANGE_CORRELATION_ID for YAML pipeline

Following the MS documentation here: https://learn.microsoft.com/en-us/azure/devops/pipelines/release/approvals/servicenow?view=azure-devops
I've set up an Azure DevOps environment with the Service Now Pre-deployment check and can successfully create a new standard change request in Service Now and update it using the UpdateServiceNowChangeRequest#2 task.
However, I'm struggling to find a way to access the CHANGE_REQUEST_NUMBER and CHANGE_CORRELATION_ID that are available to the UpdateServiceNowChangeRequest#2 tasks
The documentation describes how to get to these using the release pipeline by having a name for the gate and using $(PREDEPLOYGATE.mygatename.CHANGE_REQUEST_NUMBER), but there is no option in the YAML setup to name a gate and I can't seem to find another way to get to these variables
I've tried just enumerating all the environment variables in the pwsh task (i.e. gci env:\ ) but nothing relating to these variables shows up.

Pipelines -> Runs -> Run pipeline howto provide pipeline resource in request body

I have a parent pipeline which publish pipeline artifacts.
And the child pipeline which has a parent pipeline added as a resource pipeline and consumes the parent's artifacts.
I would like to programatically (by REST API) create a run of multistage child pipeline and provide the parent pipeline resource.
How to craft a request body for pipelines' run endpoint?
According to ms docs PipelineResourceParameters contains only version? Are there any examples how to use it?
Is it a resource name and buildnumber?
https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/runs/run%20pipeline?view=azure-devops-rest-6.1#pipelineresourceparameters
Where can I find any examples of requests bodies?
The pipeline resource is set in advance in the pipelines. By default, the pipeline will select last successful run as the resource. We can choose the pipeline resource version when we run the pipeline.
Here is my request body sample:
{
"resources":{
"pipelines":{
"Parent":{
"version":"20201225.1"
}
}
}
}
If you want to find the sample of Rest API, in addition to referring to the examples in the official documentation, you can also check the developer tool (F12) in the browser. For example, run a pipeline manually and check the rest api:

In Azure Devops I want my yaml pipeline only to execute after the previous execution of the same pipeline has completed

I tried batch: true setting described here, but it seems to:
ignore commits that are pushed when a pipeline is running. I want the last commit to trigger a pipeline after the current run of that pipeline has finished
be ignored when you publish directly from CI by pressing build
Has someone found a way to configure a pipeline to run as I have described.
You can try adding a Invoke REST API check on the agent pool. The rest api will get the previous build and evaluate its status. So when new build is queued targeting the agent pool. The Invoke REST API will be invoked, this new build will only start when the response of the rest api is evaluated to true.
Please check below steps:
1, create a service connection to your azure devops organization.
Go Project Setting--> Service connections under Pipelines-->Click new service connection--> select Generic to create a generic service connection.
Then Edit the service connection type the information shown in below screenshot. Check here to get a Personal access token.
2, add Invoke REST API check on the agent pool**.
Go Project Setting--> Agent Pools under Pipelines-->Select the agent pool--> Click the 3dots -->Click Approvals and checks.
.
3, Click the "+"--> Choose Invoke REST API
4, Edit the Invokde Rest API
Select the generic service connection to your azure devops created in the first step.
Set the fields as below:
URL suffix and parameters: _apis/build/builds?definitions=the DefinitionId of your pipeline&$top=2&queryOrder=queueTimeDescending&api-version=5.1
Success criteria: eq(root['value'][1]['status'], 'completed')
please check here for more information about build rest api.
Note: Since the Invoke Rest api check is set on agent pool scope. It may have effects on other pipelines that target this agent pool. For example, if the desired yaml pipeline is waiting its previous run to complete. And now another pipeline targeting this same agent pool is triggered, it will have to wait for the previous run of desired yaml pipeline to complete too.

Add terraform plan output as PR comment in Azure Devops Build Pipeline

In my current assignment, I am integrating terraform to our Azure DevOps CI/CD pipeline. My requirements are as follows:
The creation of PR should trigger a build pipeline.
A task in the build pipeline should publish a comment to the raised PR. The content of comment will
be the terraform plan output i.e the new infrastructure that is going to be deployed.
Once the PR is approved and code merged to master, a CD pipeline would be triggered and that will
deploy the infrastructure to Azure.
Up until now, I am all sorted on 1 and 3 requirements but I have no clue how can I publish a comment on PR with the contents from terraform plan command. Is there any built-in task for this? If not how can I achieve this? Is this possible at all? If so, can someone point out a resource that can help or just show the same .yml file?
I have searched a lot but did not find anything. My guess is you cannot add a comment from the build pipeline. Need your suggestions.
Add terraform plan output as PR comment in Azure Devops Build Pipeline
I am afraid there is no such a out of box way to Add terraform plan output as PR comment at this moment.
The workaround I am thinking of right now is invoke Rest API to create a PR comment with the contents from terraform plan command.
We could use the Rest API Pull Request Thread Comments - Create:
POST https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullRequests/{pullRequestId}/threads/{threadId}/comments?api-version=5.1
to create comment to the PR. We could write the contents from terraform plan command as Request Body:
{
"content": "YourContentsFromTerraformPlan ",
"parentCommentId": 1,
"commentType": 1
}
But above API need the pullRequestId. So, we also need another API to get the pullRequestId for current project Pull Requests - Get Pull Requests By Project:
GET https://dev.azure.com/{organization}/{project}/_apis/git/pullrequests?api-version=5.1
It will return a series of pullRequestIds, then we could use powershell argument Select-Object -first 1 like: $LastPullRequestId= $pullRequestIds.value.id | Select-Object -first 1 to get the latest pullRequestId.
So, we could add two inline powershell task in the build pipeline to invoke Rest API to get the latest pullRequestId, then use this pullRequestId to create a PR comment.
Hope this helps.