-2

I am using https://github.com/sbt/sbt-git and https://sbt-native-packager.readthedocs.io/en/stable/index.html and would like to assign the value of git.gitTagToVersionNumber to my docker container.

I tried:

git.useGitDescribe := true

Docker / dockerAlias := DockerAlias(None, Some("zerocoder"), (packageName in Docker).value, git.gitTagToVersionNumber.value)  

it does not work, because the type of the last argument of DockerAlias does not match.

How to get the value git.gitTagToVersionNumber?

I also tried:

git.useGitDescribe := true

git.formattedShaVersion := git.gitTagToVersionNumber.value("tag")
Docker / dockerAlias := DockerAlias(None, Some("zerocoder"), (packageName in Docker).value, git.gitDescribedVersion.value) 

But it does not supply the desired value. I would like to tag my docker container for example with:

v0.2.0-2-g8d5cd83
softshipper
  • 32,463
  • 51
  • 192
  • 400

1 Answers1

3

This is an example of using GitTagToVersionNumber:

git.useGitDescribe := true
git.gitDescribePatterns := Seq("v*.*")
git.gitTagToVersionNumber := { tag :String =>

val branchTag = if (git.gitCurrentBranch.value == "master") "" else "-" + git.gitCurrentBranch.value
val uncommit = if (git.gitUncommittedChanges.value) "-U" else ""

tag match {
    case v if v.matches("v\\d+.\\d+") => Some(s"$v.0${branchTag}${uncommit}".drop(1))
    case v if v.matches("v\\d+.\\d+-.*") => Some(s"${v.replaceFirst("-",".")}${branchTag}${uncommit}".drop(1))
    case _ => None
}}

Don't forget you need to create a tag like v1.0 (git tag v1.0 on your current branch) to see the above code work, since it is reading that from git. If you're testing this remotely don't forget to do git push --tags.

The example above provides:

  • semver compatible natural versioning e.g 1.0.0 with minor/major sections.
  • The Git SHA for an accurate understanding of what the code is doing. (excluding where the actual tag applies to the current SHA)
  • The major part v1.0 comes from a manually create tag you need to make and the last part (e.g .3 in 1.0.3-xxx) comes from automatically counting the number of commits you are from the tag v1.0. Therefore it is less susceptible to human error than tagging every single minor release.
  • It adds -U if the repository has modifications and therefore the code is not necessarily recreatable.
  • It adds the branch name if you're not on the master branch.

This is a complete working example but maybe you haven't enabled the GitVersioning plugin in the project part of build.sbt`.

Michael Mior
  • 28,107
  • 9
  • 89
  • 113
Philluminati
  • 2,649
  • 2
  • 25
  • 32
  • I've done as follows: `git.gitTagToVersionNumber := { tag: String => if (tag.isEmpty) None else Some(tag.split("-").toList.head) }` and I've got `v0.2.0-SNAPSHOT`. How to remove SNAPSHOT part? It should only show `v0.2.0`. – softshipper Jun 14 '19 at 11:23
  • 1
    To remove snapshot add this line: `git.uncommittedSignifier := None` – Philluminati Jun 14 '19 at 20:10
  • 1
    @michael-mior: Please don't remove the link to the working example. The configuration requires files in project/ to fully understand the solution, so copying and pasting the linked contents into stackoverflow will leave many people without a full working example, and therefore unable to get the solution working. – Philluminati Aug 12 '21 at 11:28
  • @Philluminati The link is somewhat deceptive then since it says it leads to a complete working example. I've redone my edit to include the other minor fixes. – Michael Mior Aug 12 '21 at 23:09