繁体   English   中英

CI/CD 管道 Azure devops 部署发布后自动合并

[英]CI/CD pipelines Azure devops automatic merge after deploy release

我有一个经典的环境。 设置如下:

我有 2 个分支: DevelopMaster

Azure DevOps 中是否有任何方法可以设置以下规则:

  1. 在开发环境中部署成功时(在 azure devops 的发布管道中定义) ------>自动创建pull request以将开发合并到Master中。

  2. 或另一个:如果开发分支Build成功------->自动创建pull request以将开发合并到 Master

任何帮助将不胜感激。

编辑:

我刚刚上传了执行此操作的扩展程序: https : //marketplace.visualstudio.com/items?itemName=ShaykiAbramczyk.CreatePullRequest


您可以使用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

照片3

您需要允许脚本访问OAuth令牌(选中“代理作业”选项中的复选框):

照片1

结果:

在此处输入图片说明

我将基本参数放在正文中(从分支到分支,标题),但是您可以添加更多参数,例如评论者,请在此处检查文档。

  1. 没有内置任务,但是您可以使用oauth令牌或使用自己的auth对api发出请求,自己编写脚本。
  2. 几乎可以在此处使用相同的方法,也可以使用分支策略在合并请求到主服务器之前强制对拉取请求进行验证(在我看来,这是更好的方法,因为在每次提交时从开发合并到主服务器都是毫无意义的)。

使用 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.

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