Sunday, March 18, 2018

Application versioning with Git and Gradle

Please follow Rest API Server to see the big picture and GitHub repo details.

Application versioning in Git is tricky compared to subversion. Subversion commits are in the increasing order of number so it's easy to put them as part of the application version. Being numbers they are easy to comprehend. However, Git commit id's are hash which would frighten any faint-hearted. A typical git commit id may look like

8ed7418ca02f239c712f2ac964b83cc391d68de6

Using revision list count

One can find the number of commits that have gone into a branch by doing the following

git rev-list --count HEAD 

However, beware that the numbers will be monotonically increasing in this case. One way to handle that is to add extra metadata in terms of commit id and branch name. This will help in finding the source code from where the revision was generated. As commit id's and branch name are big strings, some conventions can be put in terms of having the last five digits of commit id and branch name. Even the full string of commit id and branch name can be captured and put in some metadata with the build.

Using Tags 

Git provides tag functionality which can be used to something similar with respect to subversion. For the same, tag your branch with '-a' option. This creates an annotated tag.

git tag -a v0.1 -m "0.1 version"

Now doing a git tag command will show all the tags

git tag
v0.1

Now do a describe

git describe
v0.1

If you do any commit over this, the describe will increment the version and append last letters of commit id

git describe
v0.1-1-gcc67a65

Integrating into Gradle

To integrate into Gradle, one way is to add an implementation version in MANIFEST.MF file. This will allow the jar or bootJar task to use the version in creating the artifacts. (In Spring boot environment, I am seeing that jar task is not picked up. Not sure if that is intended or a bug. In this example, that's why you will see the version handling as part of bootJar task.)

bootJar {
    baseName='rms'
    version= 'git describe'.execute().text.trim()
    manifest {
        attributes('Implementation-Title':baseName,
                   'Implementation-Version':version)
    }
}

The application jar, in this case, will be generated with rms-<version>.jar. Also, the details are available in MANIFEST.MF file which can be accessed programmatically. Also for this, you will have to use the build task. bootRun task does not generate MANIFEST.MF file.

No comments:

Post a Comment