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

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:

Related

Get Azure Devops Pipeline Step Runtimes

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.

How to get referenced pipeline run via rest api?

Using Azure Pipelines, I have a yaml pipeline with a pipeline resource specified:
https://learn.microsoft.com/en-us/azure/devops/pipelines/process/resources?view=azure-devops&tabs=schema#define-a-pipelines-resource
resources:
pipelines:
- pipeline: Workload
source: Build Azure Function App
In the Run dialog you are then able to pick a run of this referenced pipeline as an input to the pipeline you are planning to run.
I would like to query previous pipeline runs through rest api in order to retrieve the particular run the pipeline has run against.
I tried the following rest api:
az rest -m get -u https://dev.azure.com/myorg/myproject/_apis/pipelines/20/runs/713?api-version=6.0-preview.1 --resource 499b84ac-1321-427f-aa17-267ca6975798 -o json
But this does not seem to yield the referenced pipeline run.
Any clues?
From the GET Run REST API, the response body does not contain the source pipeline: https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/runs/get?view=azure-devops-rest-6.0#pipelinereference
The Pipeline Reference part carries the below information:
Checking the sample response body:
Source is defined in the Pipeline resource, but the GET Run API does not recall .

In Azure DevOps YML pipeline, how to mandate template call?

I have a template to perform some post build(end of the pipeline) checks. This check must happen in a build, after few jobs successfully finished.
I want to ensure, template call must be the last job in main pipeline and no control to author to skip. How to mandate template usage ?
Because this template based checks not applicable to all pipelines with in Azure DevOps organization.
Is it possible to use template as a decorator backend and conditionally inject it into selected pipelines in a organization ?
Does decorator support parametrization ?

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.