Search code examples
gitbitbucketazure-pipelinesdelphi-xe

Azure Pipelines multi-repo how to get Git Commit ID


For an Azure Pipeline with multi-repositories, how can you get the GIT commit id from a checked out resource repository? Is it supported?

I'm using an Azure repo to store the pipeline yaml file, and checking out the build source on an agent to build there. We're using Delphi so we have to use an agent.

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

Solution

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

    I am afraid this is not supported by the Azure devops at this moment.

    So, we could not use the predefined variables like $(Build.SourceVersion) to get the Git Commit ID from the multi-repositories directly.

    To resolve this issue, we could use git command line to get the Commit ID and branch name:

     git rev-parse HEAD
     git branch -r
    

    You could check my test YAML file for some details:

    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
    

    The result for job B:

    enter image description here

    Commit Id from bitbucket.org:

    enter image description here

    Hope this helps.