【问题标题】:Conditionally add a tag to merge-commit using Azure DevOps and git使用 Azure DevOps 和 git 有条件地将标记添加到合并提交
【发布时间】:2020-11-14 17:02:47
【问题描述】:

我想基本上实现与在 Azure DevOps 中使用内置“标签功能”构建时可以实现的完全相同的目标,但有一个例外。我需要有条件地添加一个标签。 基本思想是评估我的 PR 的 SourceBranch 是否以 refs/heads/hotfix 开头,并且目标分支是 refs/heads/master,然后我想用标签“hotfix”标记最终合并提交。

我是这样开始的:

    name: PR-$(Rev:r)

trigger: none

pool:
  vmImage: 'windows-latest'

jobs:
  - job: Say_hello_to_my_little_friend
    steps:
      - pwsh: Write-Host "Hello little friend!" 
  - job: Tag_As_Hotfix
    condition: and( eq(variables['System.PullRequest.TargetBranch'],'refs/heads/master'), startsWith(variables['System.PullRequest.SourceBranch'], 'refs/heads/hotfix'))
    steps:
      - pwsh: |
          git tag hotfix
          git push origin master
        displayName: Tag as hotfix

但是.. 我很快意识到分支 master 不存在,因为在 PR Azure DevOps 创建了一个临时分支,它运行所有“检查”。 即使上述方法可行..我认为另一个问题是标签最终会出现在实际合并提交之前的提交上(或者我错了吗?).. 知道如何有条件地向合并提交添加标签吗?

【问题讨论】:

  • 嗨@lnx51。这张票有更新吗?如果答案能给你一些帮助,请随时告诉我。只是提醒this。

标签: build azure-devops tags pull-request


【解决方案1】:

当您将构建设置为拉取请求中的构建验证时,构建将在完成拉取请求之前执行。所以它不会满足你的要求。

关于Branch Policy,你可以参考这个文档。

知道如何有条件地向合并提交添加标签吗?

由于变量System.PullRequest.TargetBranch和System.PullRequest.SourceBranch只存在于Pull Request Trigger Build中,您可以尝试以下设置来设置管道。创建两个管道:管道 1 和管道 2。

在 Pipeline 1 中,您可以设置条件并使用Rest API 来触发 pipeline 2。

在 Pipeline 2 中,您可以在 Pipeline 2 中添加一个 Environment,该 Environment 包含检查:Invoke Rest API to check the status of the Pull Request。如果 API 响应满足要求(Pull Reuqest Status 完成),就会运行 git 命令添加标签。

这是我的示例:

管道 1:

trigger: none

pool:
  vmImage: 'windows-latest'

jobs:
  - job: Tag_As_Hotfix
    condition: and( eq(variables['System.PullRequest.TargetBranch'],'refs/heads/master'), startsWith(variables['System.PullRequest.SourceBranch'], 'refs/heads/test'))
    steps:
    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          $token = "PAT"
          
          $url="https://dev.azure.com/{Org}/{Project}/_apis/distributedtask/variablegroups/{VariableGroup ID}?api-version=5.0-preview.1"
          
          $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
          
          $JSON = @'
          {
             "variables": {
                 "id": {
                    "value": "$(SYSTEM.PULLREQUEST.PULLREQUESTID)"
               }
            },
            "type": "Vsts",
            "name": "New variable group 16-Nov"
          }
          '@
          
          $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method PUT -Body $JSON -ContentType application/json
    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          $token = "PAT"
          
          $url="https://dev.azure.com/{ORG}/{Project}/_apis/build/builds?api-version=5.0"
          
          $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
          
          $JSON = @'
          {
              "definition": {
                  "id": BuilddefinitionID
              }
          }
          '@
          
          $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON -ContentType application/json

注意:您需要创建一个变量组来保存拉取请求 ID。此 id 可用于Invoke Rest API check。

管道2:

trigger:
- none

pool:
  vmImage: 'windows-latest'

stages:
- stage: deploy
  jobs:
  - deployment: DeployWeb
    displayName: deploy Web App
    environment: 'API Test'
    strategy:
      runOnce:
        deploy:
          steps:
          - script: |
              git tag hotfix
              git push origin master

环境: 管道 -> 环境 -> 批准和检查 -> 调用 Rest API

变量组:

工作流程

创建拉取请求->触发管道1->触发管道2并更新变量组中的ID->管道2检查拉取请求状态->运行命令添加标签。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 2023-02-09
    • 2020-11-25
    • 2020-01-24
    • 2016-12-30
    • 2016-08-12
    相关资源
    最近更新 更多