Azure DevOps 中的合并自动化

问题描述 投票:0回答:2

首先,我是自动化的新手,所以请原谅我的任何错误。

我们目前使用 Azure DevOps 存储库(从 TFS 迁移)。在 DevOps 中,我们有一个“主要”存储库。典型的工作是,开发人员创建他们的分支,一旦准备就绪,他们将其分支推送到远程 DevOps 存储库(例如

git push -u origin itsp2187
)。 一旦进入 DevOps,开发人员将创建一个 PR,一旦获得批准,PR 就完成了(合并到远程主分支)。

我想做的是当 PR 合并到 DevOps 主分支时能够做一些事情。我需要使用合并 PR 中的信息更新外部数据库(例如,执行 PR 合并的用户、发生这种情况的日期和时间、PR 标题以及任何描述(如果可能))。

我应该使用什么样的软件来完成此任务?这可能吗?我听说 Github 中使用了 Actions,我听说 Azure 中使用了管道和 Webhook。这些是一样的吗?或者这些软件或其他软件中的任何一个最适合这种需求吗?

git azure-devops azure-pipelines webhooks
2个回答
1
投票

解决方案1:

合并 PR 时,提交历史记录中会有一条默认的 Commit 消息,如

Merged PR xx: The pull request title

注意:如果您使用该解决方案并想要自定义合并提交消息,请保留关键字

Merged PR xx:

基于此消息,我们可以创建一个管道,仅当 Commit 消息 (

$(Build.SourceVersionMessage)
) 包含关键字
Merged PR
时才会运行。在管道中,我们在 PowerShell 任务中使用 REST API Get Pull Request By Id

以下是我的测试步骤:

    创建具有代码权限的
  1. 个人访问令牌

    在主分支中使用以下 yaml 创建管道。
trigger: - main pool: vmImage: Windows-latest jobs: - job: A condition: eq(contains(variables['Build.SourceVersionMessage'], 'Merged PR'), True) steps: - task: PowerShell@2 inputs: targetType: 'inline' script: | # Set your Azure DevOps organization, project, repository information $organization = "your organization name" $project = "your project name" $repositoryId = "your repo name" # repo name or repo id $token="$(PAT)" $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "",$token))) # get pullRequestId from the $(Build.SourceVersionMessage) $string = "$(Build.SourceVersionMessage)" $splitString = $string.Split(" ") $pullRequestId = $splitString[2].Replace(":", "") Write-Host "pullRequestId is $pullRequestId" # Invoke the REST API to get the pull request $uri = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repositoryId/pullRequests/$pullRequestId"+"?api-version=7.1-preview.1" $uri try { $Response = Invoke-RestMethod -Uri $uri -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} $Response | ConvertTO-Json } catch { Write-Host "Error retrieving infornation of Pull Request $pullRequestId : $_" exit 1 }

  1. 使用管道中的个人访问令牌创建一个名为 PAT 的秘密变量。

  2. 保存管道。

  3. 合并 Pull 请求时,管道将在 PowerShell 任务中获取 Pull 请求信息。如果 Commit 消息不包含关键字

    Merged PR

    ,则作业将被跳过。

    现在,您可以添加其他数据库相关任务,以使用 PowerShell 任务中的拉取请求信息更新外部数据库。
解决方案2:

您可以将基于

webhook 的 YAML 管道触发器拉取请求更新事件一起使用。

详细步骤:

    创建传入 WebHook 服务连接

    使用拉取请求更新事件创建服务挂钩 Web 挂钩,然后选择状态已更改。

请求网址是

"https://dev.azure.com/<ADO Organization>/_apis/public/distributedtask/webhooks/<WebHook Name>?api-version=6.0-preview"

。 WebHook 名称是您在服务连接中设置的名称。

    使用资源类型 Webhooks 创建管道。当传入 Webhook 服务连接收到拉取请求更新的 Webhook 事件时,将触发管道,并且您可以在作业中以变量的形式使用有效负载数据。
示例 Yaml:

trigger: none resources: webhooks: - webhook: MyWebhookTrigger ### Webhook name connection: MyWebhookConnection ### Incoming webhook service connection name filters: - path: resource.status value: completed steps: - task: PowerShell@2 inputs: targetType: 'inline' ### JSON payload data is available in the form of ${{ parameters.<WebhookAlias>.<JSONPath>}} script: | Write-Host "user who executed the PR merge: ${{ parameters.MyWebhookTrigger.resource.createdBy.displayName}}" Write-Host "creationDate: ${{ parameters.MyWebhookTrigger.resource.creationDate}}" Write-Host "closedDate: ${{ parameters.MyWebhookTrigger.resource.closedDate}}" Write-Host "PR title: ${{ parameters.MyWebhookTrigger.resource.title}}" Write-Host "description: ${{ parameters.MyWebhookTrigger.resource.description}}"
测试结果:

    现在,您可以添加其他数据库相关任务,以使用 Web 挂钩中的拉取请求信息更新外部数据库。

0
投票
从我们的远程 azure 存储库中,我尝试创建一个管道(选择带有简单的“hello world”的“启动管道”),但我收到“TF402455:不允许推送到此分支;您必须使用拉取”请求更新此分支”。我不明白。当然,这个管道必须在主分支中,那就是它将被应用的地方,不是吗?

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.