GitVersion: [ISSUE]: GitVersion is undefined in Azure DevOps on PR builds using GitTools 3.1.1

Prerequisites

  • I have written a descriptive issue title
  • I have searched issues to ensure it has not already been reported

GitVersion package

AzureDevops task

GitVersion version

6.0.5

Operating system

Linux

What are you seeing?

I recently updated to the new GitTools tasks from UseGitVersion@5 along with new config and have had no issues building code for approximately a week. When I opened a pull request today, the automatic build triggered by our PR requirement failed stating that GitVersion is undefined. Looking at the logs, I can see that the checkout process is working as expected with all our most recent commits to the repository and we automatically tag every commit into master/main branches so history shouldn’t be an issue. Any suggestions/workarounds would be appreciated.

What is expected?

Pull request builds should behave the same way when using GitTools in Azure DevOps.

Steps to Reproduce

# azure-pipelines.yaml job template
parameters:
- name: DependsOn
  type: object
  default: []

jobs:
- job: AdoBuildNumber
  dependsOn: ${{ parameters.DependsOn }}
  pool:
    name: 'ubuntu-latest'
  steps:  
  - checkout: self
    fetchDepth: 0

  - task: gitversion/setup@3.1.1
    displayName: Install GitVersion
    inputs:
      versionSpec: '6.0.x'

  - task: gitversion/execute@3.1.1
    displayName: Determine Version
    name: git_version  # variable to use in subsequent steps
    inputs:
      overrideConfig: |
        update-build-number=false
  
  - pwsh: |
      $build_number = "$(git_version.majorMinorPatch)-$(git_version.preReleaseLabel)-$(git_version.buildMetaData)"
      echo "##vso[build.updatebuildnumber]$($build_number)"
    displayName: 'Set Build Number using GitVersion'
# GitVersion.yaml
workflow: GitHubFlow/v1
strategies:
  - Mainline
assembly-versioning-format: '{MajorMinorPatch}-{PreReleaseLabel}-{BuildMetaData}'

RepositoryFixture Test

No response

Output log or link to your CI build (if appropriate).

Starting: Determine Version
==============================================================================
Task         : Execute GitVersion Task
Description  : Easy Semantic Versioning (https://semver.org) for projects using Git
Version      : 3.1.1
Author       : GitTools Contributors
Help         : See the [documentation](https://gitversion.net/docs/) for help
==============================================================================
Running on: 'Azure Pipelines'
Disable Telemetry
Command: git -C /agent/_work/15/s rev-parse --is-shallow-repository
Command: /opt/hostedtoolcache/GitVersion.Tool/6.0.5/dotnet-gitversion /agent/_work/15/s /output json /l console /overrideconfig update-build-number=false
##[error]GitVersion output is not valid JSON
dotnet-gitversion Output:
-------------------
undefined
-------------------
undefined
Finishing: Determine Version

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 6
  • Comments: 26 (7 by maintainers)

Commits related to this issue

Most upvoted comments

I’ve also encountered the same issue using the gitversion@3 tasks, however none of above solutions worked for me. Since some of the builds succeeded, I’ve assumed it has to be something with the installation itself. After digging into the logs, it seemed that gitversion/setup didn’t run as expected and installed the tool into another directory.

Running ls $env:GITVERSION_PATH resulted in:

x64
x64.complete

which I’ve found is an Azure DevOps specification for installing tools (check following doc).

In order to handle this, I’ve configured my pipeline as follows:

  - task: gitversion/setup@3
    displayName: gitversion/setup
    inputs:
      versionSpec: '6.0.*'
  
  - pwsh: |
      $gitVersion = Get-ChildItem "${env:GITVERSION_PATH}" -Filter "dotnet-gitversion"
      if (!$gitVersion) {
        $gitVersion = Get-ChildItem "${env:GITVERSION_PATH}" -Recurse -Filter "dotnet-gitversion"
        Write-Host "Setting 'GITVERSION_PATH' to '$($gitVersion.DirectoryName)'"
        Write-Output "##vso[task.setvariable variable=GITVERSION_PATH]$($gitVersion.DirectoryName)"
      }
    displayName: gitversion/configure
  
  - task: gitversion/execute@3
    displayName: gitversion/execute

The second step checks the installation path and adjusts it accordingly. As this is rather an ugly workaround, I’d assume it can be easily fixed.

Whenever a build previously failed, I’ve now get a log like: Setting 'GITVERSION_PATH' to '/agent/_work/_tool/GitVersion.Tool/6.0.5/x64'

Noticed the undefined as well in azure devops pipelines when using workflow: TrunkBased/preview1 Problem might be that “merge from master” feature is used to get latest from the trunk into the current feature, and potentially not using feature branch but accidental a different mistyped variant. (Thus hitting the ‘unknown’ configuration)

However when it happens a rebase seems not succeed either so a full new branch is normally created with cherry picks from the one that experiences it. It might be most likely due to the following:

Please try the TrunkBased/preview1 workflow:

* [Mainline in 6.0.0 with default configuration values throws #4236 (reply in thread)](https://github.com/GitTools/GitVersion/discussions/4236#discussioncomment-10785593)

If you have a merge from main to your feature or hotfix branch then this can be a problem as well. You should know currently the feature to support multi mainline branches in the Mainline version strategy (#4057) is not implemented.

Hopefully https://github.com/GitTools/GitVersion/issues/4057 will get some attention for such weird cases, as trunkbased is really something simpler to use in most cases but the undefined is really a heavy blow when encountered

Please try the TrunkBased/preview1 workflow:

If you have a merge from main to your feature or hotfix branch then this can be a problem as well. You should know currently the feature to support multi mainline branches in the Mainline version strategy (#4057) is not implemented.

Thanks for the info! Changing the workflow did fix my issue.

I had a similar issue in my DevOps pipeline where this stopped working today (2025-01-13 - was working yesterday). In my case the issue was building a pipeline on a bugfix branch.

Running on: 'Azure Pipelines'
Disable Telemetry
Command: git -C /home/vsts/work/1/s rev-parse --is-shallow-repository
Command: /opt/hostedtoolcache/GitVersion.Tool/6.0.5/dotnet-gitversion /home/vsts/work/1/s /output json /l console /config /home/vsts/work/1/s/GitVersion.yml
##[warning]Free memory is lower than 5%; Currently used: 95.69%
##[warning]Free memory is lower than 5%; Currently used: 95.69%
##[warning]Free memory is lower than 5%; Currently used: 95.69%
##[warning]Free memory is lower than 5%; Currently used: 95.69%
##[warning]Free memory is lower than 5%; Currently used: 95.69%
##[warning]Free memory is lower than 5%; Currently used: 95.69%
##[warning]Free memory is lower than 5%; Currently used: 95.69%
##[warning]Free memory is lower than 5%; Currently used: 95.69%
##[warning]Free memory is lower than 5%; Currently used: 95.69%
##[error]GitVersion output is not valid JSON
dotnet-gitversion Output:
-------------------
undefined
-------------------
undefined
Finishing: Create Version

Here is the devops git version config that I am using:

  - task: gitversion/setup@3
    inputs:
      versionSpec: "6.0.5"
      includePrerelease: false

  - task: gitversion/execute@3
    displayName: "Create Version"
    inputs:
      useConfigFile: true
      configFilePath: "GitVersion.yml"

I can confirm that changing the workflow fixed my issue, changing the GitVersion.yml as follows:

workflow: GitHubFlow/v1

to

workflow: TrunkBased/preview1

This thread has saved me some time, so thanks to all!

Please try the TrunkBased/preview1 workflow:

If you have a merge from main to your feature or hotfix branch then this can be a problem as well. You should know currently the feature to support multi mainline branches in the Mainline version strategy (#4057) is not implemented.

Here’s ^^ a bit of a repro, but generally it’s working with my use case, despite the persistent UNDEFINED runner console output.

@DaveSkender I’m seeing undefined on the setup step everytime either way; don’t know whether the additional info is useful.


On a different note, it appears that the build only works when using the old GitVersion task. When I change back to the new way of doing things, I’m getting undefined as mentioned above. It’s possible that @DaveSkender has already determined the root issue to that though.

I guess we have mixed up thinks here. @arturcic do you have an idea what the problem might be? Are all informationen present in this ticket to reproduce it? Because it seem to me a infrastrucutre problem, I cannot help.

@HHobeck From my end, exactly the same configuration will return different results in ADO depending on whether I use UseGitVersion@5 with the version set to v6.0.x or gitversion/execute@v3.1.1. The latter works when building locally or building a branch directly, but does not work when auto-building pull request branches; the former always works.

# my entire GitVersion.yaml configuration for a .NET code repository
workflow: TrunkBased/preview1
strategies:
  - Mainline
assembly-versioning-format: '{MajorMinorPatch}-{PreReleaseLabel}-{CommitsSinceVersionSource}'

It might be worth mentioning that ADO (and I believe GitHub Actions as well) automatically create a new branch including the current target branch and the changes in the source branch in the pull request before running any subsequent build steps. I’m not confident on whether that branch is of the target with the source merged in or of the source with the target merged in, but I suspect that this has something to do with GitVersion being unable to determine versioning.

Considering that this works as expected in v5 as well as the deprecated UseGitVersion@5 task, but not the newer task, I would expect this to be a bug on GitVerison’s side at this point.