从 Azure DevOps YAML 管道中的所有提交中获取更改

Get changes from all commits in an Azure DevOps YAML pipeline

我正在编写一个 ADO YAML 管道,每当将文件推送到存储库时它都会进行一些修改。到目前为止一切正常,除了我无法一次处理多个提交。 我需要获取自上次推送到推送(触发)最初来自的分支以来所有更改文件的列表。

Build.SourceVersion 只有 returns 最新的提交,我找不到任何其他内置变量

没有 PushId 可用于 REST API (https://docs.microsoft.com/en-us/rest/api/azure/devops/git/commits/get-push-commits?view=azure-devops-rest-6.0)

在 Github 操作中,前一个和最新的提交 ID 作为变量公开,然后我可以使用这些变量来获取自上次推送以来的更改,但我在 ADO 中找不到任何类似的东西

还有其他方法可以获取这些信息吗?

类似于

git log --pretty=format:"" --name-only <oldCommitHash>..<newCommitHash> | sort -u

它将找到上次推送和当前推送之间的所有提交,输出每个更改文件的列表(--pretty=format:"" 在这里防止提交元数据输出,--name-only 将diff 只是更改文件的列表),sort -u 将有助于重新排序和摆脱双打/空白。

DevOps API 中有一种方法可以 get all the changes associated with a specific build

这是我用于类似目的的脚本片段:

$changes = Invoke-RestMethod -Method Get -Headers $headers "https://dev.azure.com/$organization/$project/_apis/build/builds/$buildId/changes?api-version=5.1" -verbose

$changedPaths = @()
if ($changes.count -gt 0) {
    $commitId = $changes.value[$changes.value.length - 1].id
    write-verbose "first change: $commitId" -Verbose

    $commitId = $changes.value[$changes.value.length - 1].id
    $head = $changes.value[0].id
    
    $changedPaths = git diff "$($commitId)~1" $head --name-only
}

感谢 qbik 的输入,我提出了以下 YAML 任务,它在推送前后获取 GIT 提交 ID,并将这些值写入环境变量以在以下脚本中使用:

- task: PowerShell@2
  displayName: Get GIT Commit-IDs before and after Push
  inputs:
    targetType: 'inline'
    script: |
      # print Information stream
      $InformationPreference = "Continue"

      $headers = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" }
      $uri = "$env:SYSTEM_TEAMFOUNDATIONSERVERURI$env:SYSTEM_TEAMPROJECT/_apis/build/builds/$($env:BUILD_BUILDID)/changes?api-version=5.1"
      $changes = Invoke-RestMethod -Method Get -Headers $headers -Uri $uri -Verbose

      if ($changes.count -gt 0) {
          $commit_id_before = $changes.value[$changes.count - 1].id
          $commit_id_after = $changes.value[0].id

          Write-Information "Commit ID before push: $commit_id_before"
          Write-Information "Commit ID after push:  $commit_id_after"

          Write-Host "##vso[task.setvariable variable=GIT_EVENT_BEFORE]$commit_id_before"
          Write-Host "##vso[task.setvariable variable=GIT_EVENT_AFTER]$commit_id_after"
      }
      else {
        Write-Warning "No changes found in Build $buildId"
      }
    pwsh: true
  env:
    SYSTEM_ACCESSTOKEN: $(System.AccessToken)