absolute-version / commit-and-tag-version

Fork of the excellent standard-version. Automate versioning and CHANGELOG generation, with semver.org and conventionalcommits.org :trophy:
ISC License
385 stars 36 forks source link

Version bump insisting on versioning to 1.0.1 in azure-pipeline #75

Closed rowley-pete closed 1 year ago

rowley-pete commented 1 year ago

Describe the bug I have a gradle project, a build.gradle file, a version.json file, a .versionrc.json file, an azure-pipeline.yml and a commit-and-tag-updater.js file (detailed below). When running the commit-and-tag-version in the azure pipeline it insists on setting the version to 1.0.1 no matter what is in the version.json or tagged on the branch.

Current behavior Using the --dry-run flag locally works as expected but in the pipeline it insists on setting version to 1.0.1 no matter what it was previously, this causes downstream errors when publishing artifiacts.

azure pipeline task output { updated: '1.0.1' } { updated: '1.0.2' } ✔ bumping version in ./lib/version.json from 1.0.2 to 1.0.1 ✔ outputting changes to CHANGELOG.md ✔ committing ./lib/version.json and CHANGELOG.md ✔ tagging release v1.0.1 fatal: tag 'v1.0.1' already exists

Expected behavior I would expect it to bump the version from 1.0.2 to 1.0.3 if the commit message is prefixed with fix:

Environment

Possible Solution

Additional context

build.gradle fragment

import groovy.json.JsonSlurper plugins { id 'java-library' id 'maven-publish' } task readVersionNumber { def json = new JsonSlurper().parseText(file('./version.json').text) project.version = json.version } jar { dependsOn readVersionNumber } version.json

{ "version": "1.0.2" }

versionrc.json

{ "releaseCommitMessageFormat": "chore(release): {{currentTag}} [skip ci]", "bumpFiles": [ { "filename": "./lib/version.json", "updater": "./commit-and-tag-updater.js" } ] }

azure-pipeline script

- script: | sudo npm install -g commit-and-tag-version git config --global user.email "azure@bot.com" git config --global user.name "azurebot" if [ "$(Build.SourceBranch)" == "refs/heads/main" ]; then commit-and-tag-version else commit-and-tag-version --prerelease alpha fi git push --follow-tags origin HEAD:$(Build.SourceBranchName) displayName: 'Bump version and commit'

commit-and-tag-updater.js

module.exports.readVersion = (contents) => { const updated = JSON.parse(contents).version; console.log({updated}); return updated; } module.exports.writeVersion = (contents, version) => { const json = JSON.parse(contents); json.version = version; return JSON.stringify(json); }

TimothyJones commented 1 year ago

Are you doing a full checkout in the pipeline, or is it just a single commit?

rowley-pete commented 1 year ago

It is a full checkout

drod3763 commented 1 year ago

Is the changelog coming up empty? I have a similar issue right now. Here's the gist:

This triggers future failures because we upload the builds to aws under the version number. Future PRs keep staging and prod version numbers out of sync.

The worst part is if I pull the branch locally and run the command, the changelog generates normally.

I don't understand why this doesn't work. The only thing I can think of is that it somehow is unable to find the commits in the pipeline, and since conventional-changelog uses this to come up with the version bump it messes this up somehow. The thing is I don't know why. I am wondering if it's possibly due to me cloning locally using ssh and the pipeline cloning using https? This shouldn't be an issue but I do notice the links are different when I run it locally vs running in the pipeline.

rowley-pete commented 1 year ago

After leaving it for a few weeks and coming back to it prompted by the previous comment I fixed the issue I was having by adding packageFiles property to my .versionrc.json;

{
  "releaseCommitMessageFormat": "chore(release): {{currentTag}} [skip ci]",
  "bumpFiles": [
    {
      "filename": "./lib/version.json",
      "updater": "./commit-and-tag-updater.js"
    }
  ],
  "packageFiles": [
    {
      "filename": "./lib/version.json",
      "updater": "./commit-and-tag-updater.js"
    }
  ]
}

This fixed the issue for me.

TimothyJones commented 1 year ago

Awesome! I'll close this one, please feel free to reopen if there are further problems.

kaotika commented 11 months ago

I had the same issue on gitlab and the solution was simple: adding GIT_DEPTH=0.

By default the runner clones just the last 20 commit messages and if you have more than this, the calculated SemVer wasn't correct. Most likely Azure uses something similar.