Azure DevOps 中的自动化

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

首先我要说的是,我是自动化方面的新手,所以如有任何错误,请原谅。 我们目前使用 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
1个回答
0
投票

合并 PR 时,提交历史记录中会有类似

Merged PR xx: The pull request title
的 Commit 消息。

基于此消息,我们可以创建一个管道,仅当 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 任务中的拉取请求信息更新外部数据库。
© www.soinside.com 2019 - 2024. All rights reserved.