My primary repo "MainApp" has 3 different .NET projects and the master branch is called develop
. Each project has its own pipeline therefore I've setup build validations for the branch to ensure each projects pipeline is included as a PR check.
I have another repo "SecondApp", with a master
branch and its own pipeline SecondApp-Build
.
One of the MainApp's pipeline triggers a run of SecondApp-Build
using Azure REST API (open to alternatives).
How can I include this pipeline as a PR check for MainApp's branch?
Based on your description, the Pipeline: SecondApp-Build is using different repos and triggered by MainApp's pipeline. In this case, the Pipeline status can not directly be used in the Pull Request.
How can I include this pipeline as a PR check for MainApp's branch?
You can use Pull Request Status check and Azure DevOps Rest API: Pull Request Statuses - Create to achieve it.
Here are the steps:
Step1: Add the Status check to MainApp repo Branch Ploicy. And set the Genre and Name value.
For example:
Genre: vsts-samples
Name: status-check-SecondApp-Build
Step2: You need to set the parameters in SecondApp-Build Pipeline to collect the Pull Request ID. Then when you use MainApp's pipeline to trigger pipeline, you need to pass Pull Reuqest ID value via Rest API.
For example:
MainApp's Pipeline Powershell:
$token = "$(pat)"
$url="https://dev.azure.com/{organizationname}/{ProjectName}/_apis/pipelines/{PipelineID}/runs?api-version=5.1-preview"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$JSON = "
{
`"resources`": {
`"repositories`": {
`"self`": {
`"ref`": `"refs/heads/main`"
}
}
},
`"templateParameters`": {
`"PullRequestID`":`"$(System.PullRequest.PullRequestId)`"
},
}"
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON -ContentType application/json
SecondApp-Build Pipeline:
parameters:
- name: PullRequestID
type: string
default: 1
Step3: You can use script to run Rest API to pass the Pipeline status to Pull Request status check.
Here is an Powershell example:
$token = "$(PAT)"
$url="https://dev.azure.com/{org}/{project}/_apis/git/repositories/databricks/pullRequests/${{parameters.PullRequestID}}/statuses?api-version=7.2-preview.2"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$JSON = "
{
`"state`": `"succeeded`",
`"description`": `"Sample status succeeded`",
`"context`": {
`"name`": `"status-check-SecondApp-Build`",
`"genre`": `"vsts-samples`"
},
`"targetUrl`": `"https://dev.azure.com/{org}/{project}/_build/results?buildId=$(build.buildid)&view=results`"
}
"
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON -ContentType application/json
You can set the Status check Result based on the condition.
Full YAML sample:
parameters:
- name: PullRequestID
type: string
default: 1
pool:
vmImage: windows-latest
steps:
.....
- task: PowerShell@2
condition: failed()
inputs:
targetType: 'inline'
script: |
env | sort
$token = "$(PAT)"
$url="https://dev.azure.com/{org}/{project}/_apis/git/repositories/databricks/pullRequests/${{parameters.PullRequestID}}/statuses?api-version=7.2-preview.2"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$JSON = "
{
`"state`": `"failed`",
`"description`": `"Sample status failed`",
`"context`": {
`"name`": `"status-check-SecondApp-Build`",
`"genre`": `"vsts-samples`"
},
`"targetUrl`": `"https://dev.azure.com/{org}/{project}/_build/results?buildId=$(build.buildid)&view=results`"
}
"
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON -ContentType application/json
- task: PowerShell@2
condition: succeeded()
inputs:
targetType: 'inline'
script: |
env | sort
$token = "$(PAT)"
$url="https://dev.azure.com/{org}/{project}/_apis/git/repositories/databricks/pullRequests/${{parameters.PullRequestID}}/statuses?api-version=7.2-preview.2"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$JSON = "
{
`"state`": `"succeeded`",
`"description`": `"Sample status succeeded`",
`"context`": {
`"name`": `"status-check-SecondApp-Build`",
`"genre`": `"vsts-samples`"
},
`"targetUrl`": `"https://dev.azure.com/{org}/{project}/_build/results?buildId=$(build.buildid)&view=results`"
}
"
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON -ContentType application/json
Result:
When the SecondApp-Build is failed/successed, it will send result to Pull Request status check.
At the same time, you can click the link in status check to navigate to the target Pipeline Run.