Search code examples
gitazure-devopsazure-pipelinesazure-pipelines-yamlgitversion

Azure DevOps Yaml pipeline - GitVersionfor additional repository


I have Azure DevOps Yaml pipeline with 2 repositories used:

  • Main repo: the one with .yml files (in my example branch used: feature/abc-123)
  • additional: the one with backend code - it's used in whole pipeline to build app and produce artifacts (in my example branch used: master)

my .yml file (partial):

resources:
  repositories:
    - repository: contoso
      type: git
      ref: master
      name: contosoProject/contoso

steps:
  - checkout: contoso
    fetchDepth: 0
    fetchTags: true
    clean: true
  - task: UseDotNet@2
    displayName: Use .NET Core sdk 3.1.102
    inputs:
      version: 3.1.102
  - task: UseDotNet@2
    displayName: Use .NET Core sdk 8.x
    inputs:
      version: 8.x
  - task: GitVersion@5
    displayName: GitVersion
    inputs:
      updateAssemblyInfo: true
  ...

Partial execution log from GitVersion:

Starting: GitVersion
==============================================================================
Task         : GitVersion Task
Description  : Easy Semantic Versioning (http://semver.org) for projects using Git
Version      : 5.0.1
Author       : GitVersion Contributors
Help         : See the [documentation](http://gitversion.readthedocs.org/en/latest/) for help
==============================================================================
C:\agent\_work\_tool\dotnet\dotnet.exe C:\agent\_work\_tasks\GitVersion_xxx\5.0.1\core\GitVersion.dll C:\agent\_work\12\s /output buildserver /nofetch /config C:\agent\_work\12\s /updateassemblyinfo true
INFO [01/16/25 9:21:35:42] Working directory: C:\agent\_work\12\s
INFO [01/16/25 9:21:35:45] IsDynamicGitRepository: False
INFO [01/16/25 9:21:35:60] Returning Project Root from DotGitDirectory: C:\agent\_work\12\s\.git - C:\agent\_work\12\s
INFO [01/16/25 9:21:35:60] Running on Windows.
INFO [01/16/25 9:21:35:61] Applicable build agent found: 'VsoAgent'.
INFO [01/16/25 9:21:35:61] Branch from build environment: refs/heads/feature/abc-123
INFO [01/16/25 9:21:35:61] Begin: Normalizing git directory for branch 'refs/heads/feature/abc-123'
INFO [01/16/25 9:21:35:67] One remote found (origin -> 'https://[email protected]/contoso/contosoProject/_git/contoso').
INFO [01/16/25 9:21:35:68] Skipping fetching, if GitVersion does not calculate your version as expected you might need to allow fetching or use dynamic repositories

As you can see in line INFO [01/16/25 9:21:35:61] Branch from build environment: refs/heads/feature/abc-123 task GitVersion by default checks environment variable BUILD_SOURCEBRANCH which points out to the repo/branch assigned to pipeline. I want to use additional repo/branch that is checked out with checkout step. How to achieve this?

I can also use GitTools or dotnet-gitversion in PowerShell task - doesn't matter to me.


Solution

  • I can reproduce the same situation when using the Gitversion tool version 5.x(From your log, the current version : 5.0.1).

    When we use GitTool version 5.x, the task will use the BUILD.SOURCEBRANCH variable by default to set the branch of Gitversion Tool.

    Refer to this ticket: BUILD_SOURCEBRANCH is assumed to be relevant

    To solve this issue, we need to upgrade the GitVersion tool to 6.x.

    In this case, you can set the variable: GIT.BRANCH in Pipeline to force the GitVersion task to use the required git branch.

    The GitVersion Tool(6.x) will find the variable: GIT.BRANCH first. If not found, it will use the variable: BUILD.SOURCEBRANCH.

    Here is an example:

    resources:
      repositories:
        - repository: contoso
          type: git
          ref: master
          name: contosoProject/contoso
    variables:
        GIT.BRANCH: $[ resources.repositories['contoso'].ref ]
    steps:
      - checkout: contoso
        fetchDepth: 0
        fetchTags: true
        clean: true
      - task: UseDotNet@2
        displayName: Use .NET Core sdk 3.1.102
        inputs:
          version: 3.1.102
      - task: UseDotNet@2
        displayName: Use .NET Core sdk 8.x
        inputs:
          version: 8.x
      - task: GitVersion@5
        displayName: GitVersion
        inputs:
          versionSpec: '6.0.x'
          updateAssemblyInfo: true