Search code examples
azure-devopsazure-pipelinesgitversionazure-releases

Azure DevOps release pipeline throws error on Gitversion updatebuildnumber


I am using Gitversion tasks in Azure Release pipeline to find the next tag sequence. I have added update-build-number=false in the execute gitversion task.

Screenshot of the task:

Screenshot of the task

However, the release pipeline fails with the error:

2025-02-06T11:57:18.5806353Z Command: /opt/hostedtoolcache/GitVersion.Tool/6.0.5/dotnet-gitversion --roll-forward Major /home/vsts/work/r1/a/_temp/ /output json /l console /overrideconfig update-build-number=false
2025-02-06T11:57:21.7120908Z ##[error]Unable to process command '##vso[build.updatebuildnumber]0.3.0-alpha.0' successfully. Please reference documentation (http://go.microsoft.com/fwlink/?LinkId=817296)
2025-02-06T11:57:21.7137378Z ##[error]Value cannot be null. (Parameter 'buildId')

Here is the version details of the task that I am using in the release pipeline

steps:

- task: gittools.gittools.setup-gitversion-task.gitversion/setup@3

  displayName: 'Setup gitversion'

  inputs:

    versionSpec: 6.0.5

    preferLatestVersion: true

How can I prevent the step update build number from running in the release pipeline of Azure


Solution

  • I looked at the source code to understand how the task operates. It looks like you've properly configured the extension as it's passing the /overrideconfig <prop>=<value> to the gitversion.exe on the command-line. It appears that the command-line respects the configuration value you provided.

    However, the build-task appears to update the build number after it receives a successful output.

    Here's the reverse-engineered bits:

      writeGitVersionToAgent(output) {
        for (const property of keysOf(output)) {
          const name = this.toCamelCase(property);
          try {
            let value = output[property]?.toString();
            if (value === "0") {
              value = "0";
            }
            this.buildAgent.setOutput(name, value);
            this.buildAgent.setOutput(`GitVersion_${property}`, value);
            this.buildAgent.setOutput(`GitVersion.${property}`, value);
            this.buildAgent.setVariable(name, value);
            this.buildAgent.setVariable(`GitVersion_${property}`, value);
            this.buildAgent.setVariable(`GitVersion.${property}`, value);
          } catch (_error) {
            this.buildAgent.error(`Unable to set output/variable for ${property}`);
          }
        }
        if (output.FullSemVer.endsWith("+0")) {
          output.FullSemVer = output.FullSemVer.slice(0, -2);
        }
        this.buildAgent.updateBuildNumber(output.FullSemVer);
      }
    
    • The runner has an execute() method that is called when you run the execute build-task. The execute function calls processGitVersionOutput which in turn calls the writeGitVersionToAgent mentioned above.
      async execute() {
        return this.safeExecute(async () => {
          const result = await this.tool.executeJson();
          this.buildAgent.debug("Parsing GitVersion output");
          return this.processGitVersionOutput(result);
        }, "GitVersion executed successfully");
      }
    
      processGitVersionOutput(result) {
        const stdout = result.stdout;
        if (stdout.lastIndexOf("{") === -1 || stdout.lastIndexOf("}") === -1) {
          this.buildAgent.debug("GitVersion output is not valid JSON");
          this.buildAgent.setFailed("GitVersion output is not valid JSON", true);
          return {
            code: -1,
            error: new Error("GitVersion output is not valid JSON")
          };
        } else {
          const jsonOutput = stdout.substring(stdout.lastIndexOf("{"), stdout.lastIndexOf("}") + 1);
          const gitVersionOutput = JSON.parse(jsonOutput);
          this.tool.writeGitVersionToAgent(gitVersionOutput);
          this.buildAgent.setSucceeded("GitVersion executed successfully", true);
          return result;
        }
      }
    

    Release pipelines don't have a build number you can modify. Not sure if running this task in a Release pipeline is a scenario that the GitTools team will support, but you could log a bug here.