如何在具有多个存储库的 Azure DevOps CI 管道上进行 git diff

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

网上有一些有用的[问题与解答][1],

[1]:如何在 Azure CI 管道上进行 git diff,但它们似乎在单存储库环境中工作。我使用一个 git 存储库作为项目源,另一个用于涵盖多个项目的部门范围的 YAML 代码存储库。

我当前的挑战是获取由于产品存储库的

PullRequest
IndividualCI
BatchCI
Build.Reason
而更改的文件列表。 CI 静态代码分析需要该列表。

使用

git diff --name-only HEAD^ HEAD
(或
HEAD~ HEAD
@~ @
)都给我
fatal: not a git repository (or any of the parent directories): .git

对于两个存储库,

Build.SourceDirectory
会附加存储库的名称,但我无法找到在 git diff 调用中或之前使用该信息(例如
\agent\_work\1\s\foo
)。

有人遇到过这个问题,或者有解决方案的建议吗?

git azure-pipelines azure-pipelines-yaml git-diff
1个回答
0
投票

假设您在 YAML 管道中定义了额外的存储库资源,以便与

self
存储库一起使用,并根据 文档

除非在

path
步骤中指定了
checkout
,否则源代码将放置在默认目录中。该目录会有所不同,具体取决于您要签出单个存储库还是多个存储库。

多个存储库:如果您的工作中有多个

checkout
步骤,您的源代码将被检出到以存储库命名的目录中,作为
s
(Agent.BuildDirectory)
的子文件夹。如果
(Agent.BuildDirectory)
C:\agent\_work\1
并且您的存储库名为
tools
code
,您的代码将签出到
C:\agent\_work\1\s\tools
C:\agent\_work\1\s\code
,

因此,您可以尝试下面的示例 YAML 管道在该代理计算机上的不同存储库路径中运行

git diff

resources:
 repositories:
   - repository: RepoA
     name: RepoA
     type: git

variables:
  RepoA.name: $[ resources.repositories.RepoA.name ]
  RepoA.ref: $[ resources.repositories.RepoA.ref ]
  RepoA.version: $[ resources.repositories.RepoA.version ]

steps:
# Set fetchDepth as 0 to disable the dault shallow fetch 1
- checkout: Self
  fetchDepth: 0
- checkout: RepoA
  fetchDepth: 0
- script: |
    echo "================ 1. Show file structure in Pipeline.Workspace ================"
    tree $(Pipeline.Workspace)
    
    echo "================ 2. Show self repo info and run git diff ================"
    cd $(Agent.BuildDirectory)/s/$(Build.Repository.Name)
    echo "self repo name: $(Build.Repository.Name)"
    echo "self repo ref: $(Build.SourceBranch)"
    echo "self repo commit version: $(Build.SourceVersion)"
    git diff --name-only $(Build.SourceVersion)^ $(Build.SourceVersion)
    
    echo "================ 3. Show $(RepoA.name) info and run git diff ================"
    cd $(Agent.BuildDirectory)/s/$(RepoA.name)
    echo "RepoA name: $(RepoA.name)"
    echo "RepoA ref: $(RepoA.ref)"
    echo "RepoA commit version: $(RepoA.version)"
    git diff --name-only $(RepoA.version)^ $(RepoA.version)
  displayName: Run git diff in multiple repo paths

Image

© www.soinside.com 2019 - 2024. All rights reserved.