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 .
Related
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.
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:
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:
I would put my questions through following points, hope it's make clear now:
The application source code is in application_code repo.
The pipeline code(YAMLs) are in pipeline_code repo. Because I'd like to version it and don't like to keep in application_code repo. Just to avoid giving control to Dev team to manage it.
Problem statement:
The pipeline YAML won't be triggered unless it's in the source code repository based on the events pr, commit etc.
Can we trigger or execute YAML file which is in pipeline_repo whenever there's event triggered in application_code repo?
I've tried achieving above using Classic pipeline and YAML template but this don't work together. As I can execute a YAML template from a YAML pipeline only not from a classic pipeline like below:
#azure-pipeline.yaml
jobs:
- job: NewJob
- template: job-template-bd1.yaml
Any ideas or better solution than above?
The feature Multi-repository support for YAML pipelines will be available soon for azure devops service. This feature will support for triggering pipelines based on changes made in one of multiple repositories. Please check Azure DevOps Feature Timeline or here. This feature is expected to be rolled out in 2020 Q1 for azure devops service.
Currently you can follow below workaround to achieve above using Build Completion(the pipeline will be triggered on the completion of another build).
1, Setup the triggering pipeline
Create an empty classic pipeline for application_code repo as the triggering pipeline, which will always succeed and do nothing.
And check Enable continuous integration under Triggers tab and setup Bracnh filters
2, setup the triggered pipeline
In the pipeline_code repo using Checkout to Check out multiple repositories in your pipeline. You can specifically checkout the source code of application_code repo to build. Please refer below example:
steps:
- checkout: git://MyProject/application_code_repo#refs/heads/master # Azure Repos Git repository in the same organization
- task: TaskName
...
Then in the yaml pipeline edit page, click the 3dots on the top right corner and click Triggers. Then click +Add beside Build Completion and select above triggering pipeline created in step 1 as the triggering build.
After finishing above two steps, when changes made to application_code repo, the triggering pipeline will be executed and completed with success. Then the triggered pipeline will be triggered to run the real build job.
Update:
Show Azure DevOps Build Pipeline Status in Bitbucket.
you can add a python script task at the end of the yaml pipeline to update the Bitbucket build status. You need to set a condtion: always() to always run this task even if other tasks are failed.
You can get the build status with env variable Agent.JobStatus. For below example:
For more information, please refer to document Integrate your build system with Bitbucket Cloud, and also this thread.
- task: PythonScript#0
condition: always()
inputs:
scriptSource: inline
script: |
import os
import requests
# Use environment variables that your CI server provides to the key, name,
# and url parameters, as well as commit hash. (The values below are used by
# Jenkins.)
data = {
'key': os.getenv('BUILD_ID'),
'state': os.getenv('Agent.JobStatus'),
'name': os.getenv('JOB_NAME'),
'url': os.getenv('BUILD_URL'),
'description': 'The build passed.'
}
# Construct the URL with the API endpoint where the commit status should be
# posted (provide the appropriate owner and slug for your repo).
api_url = ('https://api.bitbucket.org/2.0/repositories/'
'%(owner)s/%(repo_slug)s/commit/%(revision)s/statuses/build'
% {'owner': 'emmap1',
'repo_slug': 'MyRepo',
'revision': os.getenv('GIT_COMMIT')})
# Post the status to Bitbucket. (Include valid credentials here for basic auth.
# You could also use team name and API key.)
requests.post(api_url, auth=('auth_user', 'auth_password'), json=data)
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.