Azure Pipelines 多存储库如何获取 Git 提交 ID

Azure Pipelines multi-repo how to get Git Commit ID

对于具有多存储库的 Azure Pipeline,如何从检出的资源存储库中获取 GIT 提交 ID?是否支持?

我正在使用 Azure 存储库来存储管道 yaml 文件,并检查代理上的构建源以在那里构建。我们正在使用 Delphi 所以我们必须使用代理。

resources:
  repositories:
  - repository: MyBitBucketRepo
    type: bitbucket
    endpoint: MyBitBucketServiceConnection
    name: MyBitBucketOrgOrUser/MyBitBucketRepo

trigger:
- pilot

pool:
  name: MyAgent
  demands: RADSTUDIO

variables:
  GIT_COMMIT: $(Build.SourceVersion) # <- How can I get the checked out Commit ID for the MyBitBucketRepo?
  GIT_BRANCH: $(Build.SourceBranchName) # And the branch name?

steps:
- checkout: MyBitBucketRepo

- script: dir $(Build.SourcesDirectory)
- script: echo $(GIT_COMMIT)
- script: echo $(GIT_BRANCH)
# todo set environment vars on agent with the Commit and Branch names required by msbuild script on agent
# todo run msbuild script on agent

how can you get the GIT commit id from a checked out resource repository? Is it supported?

恐怕目前 Azure devops 不支持此功能。

因此,我们无法使用 $(Build.SourceVersion) 等预定义变量直接从多存储库获取 Git 提交 ID。

要解决此问题,我们可以使用 git 命令行来获取 Commit ID 和分支名称:

 git rev-parse HEAD
 git branch -r

您可以查看我的测试 YAML 文件了解一些细节:

resources:
  repositories:
  - repository: TestProject
    type: github
    name: xxxx/TestProject
    endpoint: GitHub connection 1
  - repository: leotest
    type: bitbucket
    name: Lsgqazwsx/leotest
    endpoint: Lsgqazwsx

variables:
  GIT_COMMIT: $(Build.SourceVersion)
  GIT_BRANCH: $(Build.SourceBranchName)


stages:
- stage:
  jobs:
   - job: A
     pool:
       name: MyPrivateAgent
     steps:
     - checkout: TestProject
     - script: echo $(GIT_COMMIT)

- stage:
  jobs:
   - job: B
     pool:
      name: MyPrivateAgent
     steps:
     - checkout: leotest
     - script: git rev-parse HEAD
     - script: git branch -r

job B的结果:

来自 bitbucket.org 的提交 ID:

希望对您有所帮助。