简体   繁体   中英

Can we raise and merge a pull request from Azure Devops pipelines?

I have a release branch through which I am executing the CI/CD yaml Pipelines and deploying the application in different environments on AKS.

Once my UAT deployment stage completes, I want to create a Pull Request and merge the branches if no conflicts, from release branch to master branch. And on completion of Pull Request only, the stage for PROD deployment should start.

Is there any task/script which will help me achieve my goal?

I have checked Create Pull Request extension, but currently it supports only Windows machines.

I also read about Azure DevOps REST APIs to create a Pull Request, but it was mentioned that the API supports 2 commits only.

Any help here would be appreciated.

In Linux you can use PowerShell core and rest API to create the PR:

$body =  @{
             sourceRefName= "refs/heads/feature"
             targetRefName = "refs/heads/master"
             title = "PR from Pipeline"
     }

$head = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"  }
$json = ConvertTo-Json $body
$url = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/git/repositories/$(Build.Repository.Name)/pullrequests?api-version=5.0"
Invoke-RestMethod -Uri $url -Method Post -Headers $head -Body $json -ContentType application/json

You can use the P ull Requests - Create Rest Api to create a Pull request as Shayki Abramczyk mentioned. See example .

However, there is an easier way which is using the Create Pull Request extension in an additional agent job and run this job on a windows agent. See below steps:

1, Add another agent job in your UAT deployment stage.

在此处输入图像描述

2, Add Create Pull Request task to create the Pull request.

If you want the stage for PROD deployment to be started automatically on the completion of the Pull Request. You can consider using the Invoke rest api Gate for the PROD deployment stage. See here for more information about Gate. You can refer to below steps:

1, Create a generic service connection to connect to your azure devops project.

Navigate to project settings-->Service connections-->New service connection-->Generic在此处输入图像描述

在此处输入图像描述

Server URL: https://dev.azure.com/OrgName/ProjName

Password: Personal Access Token

2, Add a invoke rest api Gate for PROD deployment stage.

在此处输入图像描述

invoke rest api Gate will invoke the Pull Requests - Get Pull Requests rest api to get the Pull request created in UAT stage. And the gate can only be passed when the Pull request is completed.

URL suffix and parameters: /_apis/git/repositories/{RepoId}/pullrequests?searchCriteria.sourceRefName=refs/heads/{sourceBranchName}&searchCriteria.targetRefName=refs/heads/{TargetBranchName}&$top=1&api-version=6.1-preview.1

Success criteria: not(root['value'][0])

Update:

To use above invoke rest api in yaml pipeline. You need to use deployment job and define approvals and checks for your environment.

See approvals and checks for more information.

Check here for more information about environment,

See this thread Implementing Gates in Azure YAML Pipelines

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM