Adding Commit Info to your Micronaut Application
Expose the exact version of code that your application is running.
Authors: Sergio del Amo
Micronaut Version: 2.0.0.RC1
1 Getting Started
This guide uses Micronaut 2.x. You can read this tutorial for Micronaut 1.x. |
In this guide we are going to add git commit info to your Micronaut build artifacts and running application. There are many benefits of keeping your commit info handy:
-
Commit info is encapsulated within the built artifacts
-
Fast authoritative means of identifying what specific code is running in an environment
-
This solution doesn’t rely on external tracking mechanisms
-
Transparency and reproducibility when investigating issues
1.1 What you will need
To complete this guide, you will need the following:
-
Some time on your hands
-
A decent text editor or IDE
-
JDK 1.8 or greater installed with
JAVA_HOME
configured appropriately
1.2 Solution
We recommend you to follow the instructions in the next sections and create the app step by step. However, you can go right to the completed example.
-
Download and unzip the source
or
-
Clone the Git repository:
git clone https://github.com/micronaut-guides/adding-commit-info-groovy.git
Then, cd
into the complete
folder which you will find in the root project of the downloaded/cloned project.
2 Writing the App
Create a Groovy Micronaut app using the Micronaut Command Line Interface.
mn create-app example.micronaut.complete --lang=groovy
The previous command creates a micronaut app with the default package example.micronaut
in a folder named complete
.
Due to the --lang=groovy
flag, it generates a Groovy Micronaut app that uses the Gradle build system. However, you could use
other build tools such as Maven
or other programming languages such as Java
or Kotlin
.
3 Management
Inspired by Spring Boot and Grails, the Micronaut management dependency adds support for monitoring of your application via endpoints: special URIs that return details about the health and state of your application.
To use the management features described in this section, add the dependency on your classpath. For example, in build.gradle
dependencies {
...
..
implementation("io.micronaut:micronaut-management")
}
4 Info endpoint
The info endpoint returns static information from the state of the application. The info exposed can be provided by any number of "info sources".
Enable the info endpoint:
endpoints:
info:
enabled: true
sensitive: false
5 Gradle Git Properties Plugin
If a git.properties
file is available on the classpath, the GitInfoSource
will expose the values in that file under the git
key. Generating of a git.properties
file will need to be configured
as part of your build; for example, you may choose to use the Gradle Git Properties plugin. The plugin provides a task named generateGitProperties
responsible for the git.properties
file generation. It is automatically invoked upon the execution of the classes
task. You can find the generated file in the directory build/resources/main
.
Modify build.gradle
file to add the Gradle plugin:
plugins {
..
.
id "com.gorylenko.gradle-git-properties" version "2.2.2"
}
6 Test Info endpoint
Create a test which verifies that when you do a GET request to /info
you get a payload such as:
{
"git": {
"dirty": "true",
"commit": {
"id": "7368906193527fbf2b45f1ed5b08c56631f5b155",
"describe": "7368906-dirty",
"time": "1527429126",
"message": {
"short": "Initial version",
"full": "Initial version"
},
"user": {
"name": "sdelamo",
"email": "sergio.delamo@softamo.com"
}
},
"branch": "master"
}
}
Create a Spock feature method to verify the behaviour:
package example.micronaut
import io.micronaut.http.HttpRequest
import io.micronaut.http.HttpResponse
import io.micronaut.http.client.RxHttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.annotation.MicronautTest
import spock.lang.Shared
import spock.lang.Specification
import javax.inject.Inject
@MicronautTest (1)
class InfoSpec extends Specification {
@Shared
@Client("/")
@Inject
RxHttpClient client (2)
void 'test git commit info appears in JSON'() {
given:
HttpRequest request = HttpRequest.GET('/info') (3)
when:
HttpResponse<Map> rsp = client.toBlocking().exchange(request, Map)
then:
rsp.status().code == 200
when:
Map json = rsp.body() (4)
then:
json.git
json.git.commit
json.git.commit.message
json.git.commit.time
json.git.commit.id
json.git.commit.user
json.git.branch
}
}
1 | Annotate the class with @MicronatTest to let Micronaut starts the embedded server and inject the beans. More info: https://micronaut-projects.github.io/micronaut-test/latest/guide/index.html. |
2 | Inject the HttpClient bean in the application context. |
3 | Creating HTTP Requests is easy thanks to Micronaut’s fluid API. |
4 | Use .body() to retrieve the parsed payload. |
7 Testing the Application
To run the tests:
$ ./gradlew test
$ open build/reports/tests/test/index.html
8 Running the Application
To run the application use the ./gradlew run
command which will start the application on port 8080.