Versioning Docker images with Gradle and Git

Radek Grębski
May 3, 2019 · 3 min read
Image for post
Image for post

In this blog post I’m going to show you how to configure Gradle to version Docker images using Git information. You can use similar approach for versioning different artifacts (jar, war, etc.). We will be using:

If you prefer reading code than text you can skip reading and go straight to GitHub:
https://github.com/rgrebski/gradle-git-docker-versioning

Gradle configuration

Most of the work needs to be done in build.gradle file:

Dependencies and plugins

Let’s define all the dependencies, plugins and their repositories (build.gradle):

buildscript {
ext {
springBootVersion = '2.0.0.M1'
}
repositories {
mavenLocal()
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
classpath "se.transmode.gradle:gradle-docker:1.2"
}
}

[UPDATE]
If you are using gradle.build.kts (Kotlin), you should define version variable as :

val gitVersion: groovy.lang.Closure<String> by extra
version
= gitVersion(mapOf ("prefix" to "v-"))

Versioning

I am using com.palantir.git-version plugin to get version info from git (the plugin uses git describe to determine version info), it must be defined in plugins block (incubating!) and it adds gitVersion() and versionDetails() methods to the build script. You can use gitVersion() to obtain app version, or you can create your own method if you want something more sophisticated. You can use versionDetails() which provides you all the git information needed to generate version, here is a simple gradle task that prints version details:

task showVersion {
doLast {
println "\nCurrent version: ${gitVersion()}\n"

[UPDATE]
If you are using build.gradle.kts (Kotlin) you need to define versionDetails() function as following:

fun getVersionDetails(): VersionDetails = (extra["versionDetails"] as groovy.lang.Closure<*>)() as VersionDetails

Seting version to static files (using ReplaceTokens)

In my sample application I’m having application.properties file with following content:

application.version=@appVersion@

@appVersion@ is going to be replaced by Gradle during processResources phase, all you need is to add following code to your build.gradle:

processResources {
filter(ReplaceTokens, tokens:[appVersion: gitVersion()])
}

Having that you can easily read the version from properties in Spring and expose it in Spring Boot Actuator info resource.
To read current version in Spring you can use @ConfigurationProperties:

@Configuration
@ConfigurationProperties(prefix = "application")
public class VersionConfig {

And now you can append application version to /application/info (notice that Spring Boot 2 uses /application to group actuator resources) using InfoContributor:

@Bean
public InfoContributor versionInfoContributor(VersionConfig versionConfig) {
return builder -> {
builder.withDetail("version", versionConfig.getVersion());
};
}

Building Docker image

I am using Gradle-Docker plugin to build Docker image, it allows you to define “Dockerfile” in Gradle (or mix Dockerfile with Gradle groovy code):

task buildDocker(type: Docker, dependsOn: build) {
push = project.findProperty(‘push’)?.toBoolean() ?: false

Now you can run either docker image or VersionApplication from your IDE and go to:
http://localhost:8080/application/info (notice that Spring Boot 2 uses /application to group actuator endpoints)

You should see something like:

{
"version": "v.0.1.0-1-g1e46e49"
}

Version v.0.1.0–1-g1e46e49 means:
v.0.1.0 -> last tag
1 -> number of commits since the last tag
g1e46e49 -> hash of the last commit

If we moved back to the commit tagged with ‘v.0.1.0’ the generated version would be ‘v.0.1.0’

Stepwise

Digital Transformation empowered by Cloud & ML

Radek Grębski

Written by

Stepwise

Stepwise

Stepwise specializes in Digital Transformation for Mid-size businesses that would like to scale up their products and become data driven companies. We start with understanding your business in the first place. Technology is at your service.

Radek Grębski

Written by

Stepwise

Stepwise

Stepwise specializes in Digital Transformation for Mid-size businesses that would like to scale up their products and become data driven companies. We start with understanding your business in the first place. Technology is at your service.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store