简体   繁体   English

我们可以从 Azure Devops 管道提出并合并拉取请求吗?

[英]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.我有一个发布分支,通过它我正在执行 CI/CD yaml 管道并在 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.一旦我的 UAT 部署阶段完成,我想创建一个拉取请求并在没有冲突的情况下合并分支,从发布分支到主分支。 And on completion of Pull Request only, the stage for PROD deployment should start.并且仅在完成拉取请求后,应该开始 PROD 部署阶段。

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.我已经检查了Create Pull Request扩展,但目前它只支持 Windows 机器。

I also read about Azure DevOps REST APIs to create a Pull Request, but it was mentioned that the API supports 2 commits only.我还阅读了有关 Azure DevOps REST API 以创建拉取请求,但有人提到 API 仅支持 2 次提交。

Any help here would be appreciated.这里的任何帮助将不胜感激。

In Linux you can use PowerShell core and rest API to create the PR:在 Linux 中,您可以使用PowerShell 内核rest ZDB974238714CA8DE634A7CE1D083A1来创建:

$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.您可以使用拉请求- 创建Rest Api 创建拉请求,如 Shayki Abramczyk 所述。 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.但是,有一种更简单的方法是在附加代理作业中使用创建拉取请求扩展并在 windows 代理上运行此作业。 See below steps:请参见以下步骤:

1, Add another agent job in your UAT deployment stage. 1,在您的 UAT 部署阶段添加另一个代理作业。

在此处输入图像描述

2, Add Create Pull Request task to create the Pull request. 2、添加Create Pull Request任务来创建Pull request。

If you want the stage for PROD deployment to be started automatically on the completion of the Pull Request.如果您希望在拉取请求完成时自动启动 PROD 部署阶段。 You can consider using the Invoke rest api Gate for the PROD deployment stage.您可以考虑在 PROD 部署阶段使用 Invoke rest api Gate。 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. 1,创建通用服务连接以连接到您的 azure devops 项目。

Navigate to project settings-->Service connections-->New service connection-->Generic导航到项目设置-->服务连接-->新服务连接-->通用在此处输入图像描述

在此处输入图像描述

Server URL: https://dev.azure.com/OrgName/ProjName服务器 URL: https://dev.azure.com/OrgName/ProjName

Password: Personal Access Token密码:个人访问令牌

2, Add a invoke rest api Gate for PROD deployment stage. 2、为PROD部署阶段添加一个invoke rest api Gate。

在此处输入图像描述

invoke rest api Gate will invoke the Pull Requests - Get Pull Requests rest api to get the Pull request created in UAT 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.并且只有在Pull 请求完成后才能通过gate。

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 URL 后缀和参数:/_apis/git/repositories/{RepoId}/pullrequests?searchCriteria.sourceRefName=refs/heads/{sourceBranchName}&searchCriteria.targetRefName=refs/heads/{TargetBranchName}&$top=1&api-version=6.1-预览.1

Success criteria: not(root['value'][0])成功标准:not(root['value'][0])

Update:更新:

To use above invoke rest api in yaml pipeline.要使用上述调用 yaml 管道中的 rest api。 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请参阅此线程在 Azure YAML 管道中实现门

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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