2

I'm trying to figure out how to use GitVersion to generate build id that is compatible with docker, by default gitversion is using something like this:

0.1.0-branchname.1+380

but + cannot be used in the image tag name. The only reasonable thing I've found under the GitVersion docs is NuGetVersionV2 variable, which does seem to replace + with -, but I'd like to know how to have more control over how gitversion is generating "offset" for the commits

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
  • the offset "+380" correspond to the number of commits since the last tag. So it is not unique and should not use it as your only "naming" parameter. Why not use the sha1 of the commit? There is a yaml file that you could use to configure GitVersion. – Philippe Jul 09 '19 at 10:52
  • i know about the yaml file, i was not able to come up with a reasonable configuration. commit sha i can use even without gitversion, also it would be a unreasonably long tag? – 4c74356b41 Jul 09 '19 at 10:55
  • 1
    you could use the short sha1 that is now available in GitVersion v4 (See my PR https://github.com/GitTools/GitVersion/pull/1405 ) – Philippe Jul 09 '19 at 12:45
  • do you know if that works in Azure Devops? – 4c74356b41 Jul 09 '19 at 16:08
  • Surely. Why, not!?! – Philippe Jul 09 '19 at 18:07

2 Answers2

3

For GitVersion task, based on the test result, it counts all commits of current branch instead of base on previous build or cache, regardless of using Microsoft-Hosted or Self-hosted agent.

The GitVersion task exports other variables, so I’d suggest that you can use other variables as docker image tag instead of BuildId.

##[debug]Processed: ##vso[task.setvariable variable=GitVersion.Major;]0
##[debug]Processed: ##vso[task.setvariable variable=GitVersion.Minor;]1
##[debug]Processed: ##vso[task.setvariable variable=GitVersion.Patch;]0
##[debug]Processed: ##vso[task.setvariable variable=GitVersion.PreReleaseTag;]
##[debug]Processed: ##vso[task.setvariable variable=GitVersion.PreReleaseTagWithDash;]
##[debug]Processed: ##vso[task.setvariable variable=GitVersion.PreReleaseLabel;]
##[debug]Processed: ##vso[task.setvariable variable=GitVersion.PreReleaseNumber;]
##[debug]Processed: ##vso[task.setvariable variable=GitVersion.BuildMetaData;]17
##[debug]Processed: ##vso[task.setvariable variable=GitVersion.BuildMetaDataPadded;]0017
##[debug]Processed: ##vso[task.setvariable variable=GitVersion.FullBuildMetaData;]17.Branch.master.Sha.4515221c0e10f3bf3fa4038b9b25e2379080ddf1
##[debug]Processed: ##vso[task.setvariable variable=GitVersion.MajorMinorPatch;]0.1.0
##[debug]Processed: ##vso[task.setvariable variable=GitVersion.SemVer;]0.1.0
##[debug]Processed: ##vso[task.setvariable variable=GitVersion.LegacySemVer;]0.1.0
##[debug]Processed: ##vso[task.setvariable variable=GitVersion.LegacySemVerPadded;]0.1.0
##[debug]Processed: ##vso[task.setvariable variable=GitVersion.AssemblySemVer;]0.1.0.0
##[debug]Processed: ##vso[task.setvariable variable=GitVersion.AssemblySemFileVer;]0.1.0.0
##[debug]Processed: ##vso[task.setvariable variable=GitVersion.FullSemVer;]0.1.0+17
##[debug]Processed: ##vso[task.setvariable variable=GitVersion.InformationalVersion;]0.1.0+17.Branch.master.Sha.4515221c0e10f3bf3fa4038b9b25e2379080ddf1
##[debug]Processed: ##vso[task.setvariable variable=GitVersion.BranchName;]master
##[debug]Processed: ##vso[task.setvariable variable=GitVersion.Sha;]4515221c0e10f3bf3fa4038b9b25e2379080ddf1
##[debug]Processed: ##vso[task.setvariable variable=GitVersion.ShortSha;]4515221
##[debug]Processed: ##vso[task.setvariable variable=GitVersion.NuGetVersionV2;]0.1.0
##[debug]Processed: ##vso[task.setvariable variable=GitVersion.NuGetVersion;]0.1.0
##[debug]Processed: ##vso[task.setvariable variable=GitVersion.NuGetPreReleaseTagV2;]
##[debug]Processed: ##vso[task.setvariable variable=GitVersion.NuGetPreReleaseTag;]
##[debug]Processed: ##vso[task.setvariable variable=GitVersion.CommitsSinceVersionSource;]17
##[debug]Processed: ##vso[task.setvariable variable=GitVersion.CommitsSinceVersionSourcePadded;]0017
##[debug]Processed: ##vso[task.setvariable variable=GitVersion.CommitDate;]2019-07-10

You also can custom format in GitVersion.yml. For example:

mode: ContinuousDelivery
assembly-informational-format: '{SemVer}-{ShortSha}'
branches: {}
ignore:
  sha: []

Then, use $(Gitversion.Informationalversion) (sample data:0.1.0-0322edb) in Docker task (Tags input box)

enter image description here

starian chen-MSFT
  • 33,174
  • 2
  • 29
  • 53
  • funny enough `short sha` is just first 7 characters of the `long sha`, doh. https://stackoverflow.com/questions/43665836/in-git-what-is-the-difference-between-long-and-short-hashes – 4c74356b41 Jul 10 '19 at 15:56
  • @4c74356b41 You could use long sha too. For example: assembly-informational-format: '{SemVer}-{Sha}' – starian chen-MSFT Jul 11 '19 at 01:45
  • like i said in the comments, i can use sha even without gitversion. but this proposed configuration doesnt really work for me. I still get the same tag (so not 0.1.0-shortsha, but 0.1.0-branchname.patch). any ideas? – 4c74356b41 Jul 11 '19 at 06:10
  • @4c74356b41 Set system.debug variable to true, then queue a new build and share this build log. – starian chen-MSFT Jul 11 '19 at 07:17
1

here's what I ended up doing:

mode: ContinuousDeployment
assembly-informational-format: '{Major}.{Minor}.{Patch}-{PreReleaseLabel}-{ShortSha}'
branches: {}
ignore:
  sha: []

in combination with this:

- pwsh: Write-Host "##vso[build.updatebuildnumber]${env:GITVERSION_INFORMATIONALVERSION}"
4c74356b41
  • 69,186
  • 6
  • 100
  • 141