[英]CI/CD pipelines Azure devops automatic merge after deploy release
I have a classic env.我有一个经典的环境。 setup like following:
设置如下:
I have 2 branches: Develop
and Master
.我有 2 个分支:
Develop
和Master
。
Is there any way in Azure DevOps to setup the following rule: Azure DevOps 中是否有任何方法可以设置以下规则:
When a deploy is succeeded on dev environment (defined in the release pipeline of azure devops) ------> create automatically a pull request
to merge develop into Master .在开发环境中部署成功时(在 azure devops 的发布管道中定义) ------>自动创建
pull request
以将开发合并到Master中。
or the other one: if a Build
of develop branch is succeded -------> create automatically a pull request
to merge develop into Master .或另一个:如果开发分支的
Build
成功------->自动创建pull request
以将开发合并到 Master 。
Any help will be appreciated.任何帮助将不胜感激。
I just uploaded an extension that does it: https://marketplace.visualstudio.com/items?itemName=ShaykiAbramczyk.CreatePullRequest 我刚刚上传了执行此操作的扩展程序: https : //marketplace.visualstudio.com/items?itemName=ShaykiAbramczyk.CreatePullRequest
You can use Azure DevOps Rest API to create a Pull Request, so in the end of the Build / Release add a PowerShell task that do it, for example: 您可以使用Azure DevOps Rest API创建请求请求,因此在Build / Release的最后添加执行此任务的PowerShell任务,例如:
$body = @{
sourceRefName= "$(Build.SourceBranch)"
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 need to Allow scripts to access the OAuth token (check the checbox in the Agent Job options): 您需要允许脚本访问OAuth令牌(选中“代理作业”选项中的复选框):
Results: 结果:
I put the basic parameters in the body (from branch, to branch, title) but you can add more parameters like reviewers, check the docs here . 我将基本参数放在正文中(从分支到分支,标题),但是您可以添加更多参数,例如评论者,请在此处检查文档。
Using python and the devops rest api people already mentioned you could do something like this.使用 python 和devops rest api人们已经提到你可以做这样的事情。
# Tested in python 3.10
# pip install requests
import base64
import requests
# Fill the following variables with real values
personal_access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxx' # https://learn.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&viewFallbackFrom=vsts&tabs=Windows
organization = "myorganization"
project_id = "00000000-0000-0000-0000-000000000000"
repository_id = "00000000-0000-0000-0000-000000000000"
authorization = str(base64.b64encode(bytes(f":{personal_access_token}", "ascii")), "ascii")
headers = {"Content-type": "application/json", "Authorization": f"Basic {authorization}"}
prs_url = f"https://dev.azure.com/{organization}/{project_id}/_apis/git/repositories/{repository_id}/pullrequests?api-version=5.1"
# create PR
response = requests.post(
f"{prs_url}",
headers=headers,
data=json.dumps({
"sourceRefName": "refs/heads/release",
"targetRefName": "refs/heads/master",
"title": "release to master",
}),
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.