Build 使用Azure DevOps和git有条件地添加标记以合并提交

Build 使用Azure DevOps和git有条件地添加标记以合并提交,build,azure-devops,tags,pull-request,Build,Azure Devops,Tags,Pull Request,我想基本上实现与您在使用内置Azure DevOps的内置“标记功能”时可以实现的完全相同的功能,但有一个例外。。我需要有条件地添加一个标记。。 基本思想是,如果我的PR的SourceBranch以refs/heads/hotfix开始,并且目标分支是refs/heads/master,那么我想用标记“hotfix”标记最终合并的提交 我是这样开始的: name: PR-$(Rev:r) trigger: none pool: vmImage: 'windows-latest'

我想基本上实现与您在使用内置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
然而。。我很快意识到分支主节点并不存在,因为在PR Azure期间,DevOps创建了一个临时分支,它在该分支上运行所有的“检查”。 即使上述方法有效。。我假设另一个问题是,标记将在实际合并提交之前的提交上结束(或者我错了吗?)。。
知道如何有条件地向合并提交添加标记吗?

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

你可以参考这份文件

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

由于变量
System.PullRequest.TargetBranch
System.PullRequest.SourceBranch
仅存在于Pull-Request触发器生成中,因此可以尝试以下设置来设置管道。创建两个管道:管道1和管道2

在管道1中,您可以设置条件并使用来触发管道2

在管道2中,您可以在管道2中添加一个环境,该环境包含check:invokerestapi to。如果API响应满足要求(Pull Reuqest状态为completed),它将运行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
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
注意:您需要创建一个变量组来保存Pull请求ID。此ID可用于
调用Rest API检查

管道2:

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
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

变量组:

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
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

工作流


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

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