如何在 Azure CI 管道上进行 git diff

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

我面临着让

git diff
在 Azure DevOps 管道环境中工作的挑战。这是管道脚本,用于将 git HEAD 与之前的提交进行比较,并仅复制已更改的文件。 git 命令在本地运行良好,但在 Azure 管道中会失败,因为 CI 运行中没有
HEAD^
。寻求建议,这样的比较在 Azure 中是否可行,以及我如何实现这一目标。

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      $targetfolder = "$(Build.StagingDirectory)" + "/"
      
      function CopyFiles{
          param( [string]$source )
      
          $target = $targetfolder + $source
      
          New-Item -Force $target
          copy-item $source $target -Force
      }
      
      $changes = git diff --name-only --relative --diff-filter AMR HEAD^ HEAD .
      
      if ($changes -is [string]){ CopyFiles $changes }
      else
      {
          if ($changes -is [array])
          {       
              foreach ($change in $changes){ CopyFiles $change }
          }
      }

错误消息如下所示:

我认为在Azure管道设置中,当它检查重新定位时,它只提取最新的提交,而没有完整的提交历史记录。这就是为什么当 git 尝试获取之前的提交(如

HEAD^
)时,它找不到任何内容。

提前非常感谢您的帮助!

git azure continuous-integration azure-pipelines git-diff
2个回答
6
投票

我刚刚找到了解决这个问题的方法。在这里为可能有类似问题的任何人发布答案。

默认情况下,构建代理在管道构建期间从存储库执行“浅层获取”。这仅获取最新的提交,没有任何其他提交历史记录。要获取更多提交或禁用浅层获取,请参阅 Microsoft 的此doc。有一个选项可以指定提取深度或禁用浅层提取。

希望有帮助!


0
投票

从 2022 年 9 月开始,Azure Devops 默认启用深度为 1 的浅层提取。 因此我们需要将 fetchDepth 设置为 0 来禁用它,这样我们就可以 在 azure 管道中运行 git diff 命令。 [步骤:结账] :https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema/steps-checkout?view=azure-pipelines#shallow-fetch

steps:
      - checkout: self
        fetchDepth: 0 
© www.soinside.com 2019 - 2024. All rights reserved.