简体   繁体   English

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

[英]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 个分支: DevelopMaster

Is there any way in Azure DevOps to setup the following rule: Azure DevOps 中是否有任何方法可以设置以下规则:

  1. 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中。

  2. 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.任何帮助将不胜感激。

Edit: 编辑:

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

照片3

You need to Allow scripts to access the OAuth token (check the checbox in the Agent Job options): 您需要允许脚本访问OAuth令牌(选中“代理作业”选项中的复选框):

照片1

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

  1. there is no build-in tasks for that, but you can script this yourself using the oauth token, or using your own auth to issue a request against the api. 没有内置任务,但是您可以使用oauth令牌或使用自己的auth对api发出请求,自己编写脚本。
  2. pretty much the same approach can be used here, or you can use Branch policies to force pull requests to be validated before merging them to master (which in my mind is better, because merging from develop to master on every commit is pretty pointless). 几乎可以在此处使用相同的方法,也可以使用分支策略在合并请求到主服务器之前强制对拉取请求进行验证(在我看来,这是更好的方法,因为在每次提交时从开发合并到主服务器都是毫无意义的)。

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.

相关问题 Azure 数据工厂 CI/CD - 合并到主分支后自动发布 - Azure Data Factory CI/CD - automatic publish after merge to master branch Azure DevOps 中的多个 CI 管道? - Multiple CI pipelines in Azure DevOps? Jenkins - 发布了多少个CI / CD管道? - Jenkins - How many CI/CD pipelines for a release? Azure DevOps 多个发布管道,用于单个 CI 构建管道,用于多个功能分支; ) - Azure DevOps multiple Release Pipelines for single CI Build Pipelie for multiple Feature Branches ; ) Azure DevOps:1 个解决方案多项目 CI/CD - Azure DevOps: 1 Solution Multiple Projects CI/CD Azure DevOps CI/CD 和从源代码管理中分离连接字符串 - Azure DevOps CI/CD and Separating Connection Strings from Source Control kubernetes 和 azure Devops 在 CI/CD 方面的区别 - What it the difference between kubernetes and azure Devops in terms of CI/CD Azure DevOps 中 git 发布分支的级联合并 - Cascading merge of git release branches in Azure DevOps 我们可以从 Azure Devops 管道提出并合并拉取请求吗? - Can we raise and merge a pull request from Azure Devops pipelines? 合并提交后在git ci / cd上触发脚本 - Trigger scripts on git ci/cd after merge committed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM