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

标签 git bitbucket azure-pipelines delphi-xe

对于具有多存储库的 Azure Pipeline,如何从 checkout 的资源存储库中获取 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 Commit 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

作业 B 的结果:

enter image description here

来自 bitbucket.org 的 Commit Id:

enter image description here

希望这对您有所帮助。

关于git - Azure Pipelines 多存储库如何获取 Git 提交 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62156628/

相关文章:

docker - Bitbucket Pipelines - 如何在多个步骤中使用同一个 Docker 容器?

azure - 我可以使用 azure cli 触发发布管道吗

svn - git-svn merge 和提交细节

java - play框架路径只能访问两次

vi 丢失的 Git 提交消息

zend-studio - Zend Studio 9 - 从 Git 创建 PHP 项目 (BitBucket)

terraform - 错误: spawn terraform ENOENT during Azure Pipeline Terraform

azure-devops - 发布管道变量中哪个范围优先?

java - 移动到新包后,IntelliJ 将 GIT 存储库的文件标记为未跟踪

git - 如何显示本地分支历史?