Adding Commit Info to your Micronaut Application

Expose the exact version of code that your application is running.

Authors: Sergio del Amo

Micronaut Version: 4.6.3

In this guide, we will 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. Getting Started

In this guide, we will create a Micronaut application written in Groovy.

2. What you will need

To complete this guide, you will need the following:

3. Solution

We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.

Before running the downloaded project, follow the steps described in the Initialize Git Repository section below.

4. Writing the Application

Create an application using the Micronaut Command Line Interface or with Micronaut Launch.

mn create-app example.micronaut.micronautguide --build=maven --lang=groovy
If you don’t specify the --build argument, Gradle with the Kotlin DSL is used as the build tool.
If you don’t specify the --lang argument, Java is used as the language.
If you don’t specify the --test argument, JUnit is used for Java and Kotlin, and Spock is used for Groovy.

The previous command creates a Micronaut application with the default package example.micronaut in a directory named micronautguide.

5. Initialize Git Repository

The project aims to demonstrate how to provide Git commit information to the /info endpoint and in order for that to work the project needs to be in a Git repository. After creating the project, initialize a Git repository from the root of the newly created project:

cd micronautguide
git init
git add .
git commit -am "Initial project"

6. 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.

pom.xml
<dependency>
    <groupId>io.micronaut</groupId>
    <artifactId>micronaut-management</artifactId>
    <scope>compile</scope>
</dependency>

7. 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:

src/main/resources/application.yml
endpoints:
  info:
    enabled: true
    sensitive: false

8. Maven Git Commit Id Plugin

If a git.properties file is available on the classpath, the GitInfoSource will expose the values in that file under the git key. Generation of a git.properties file will need to be configured as part of your build.

For example, you may choose to use the Maven Git Commit Id Plugin plugin. The plugin provides a goal named git-commit-id:revision responsible for the git.properties file generation. It is automatically invoked upon the execution of the classes goal. You can find the generated file in the directory target/classes.

Modify pom.xml file to add the plugin:

pom.xml
<properties>
   ...
   <gitCommitIdPlugin.version>4.0.5</gitCommitIdPlugin.version>
</properties>

...

<plugin>
    <groupId>pl.project13.maven</groupId>
    <artifactId>git-commit-id-plugin</artifactId>
    <version>${gitCommitIdPlugin.version}</version>
    <executions>
        <execution>
            <id>get-the-git-infos</id>
            <goals>
                <goal>revision</goal>
            </goals>
            <phase>compile</phase>
        </execution>
    </executions>
    <configuration>
        <generateGitPropertiesFile>true</generateGitPropertiesFile>
    </configuration>
</plugin>

9. Test

Create a JUnit test to verify that when you make 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 JUnit test to verify the behaviour:

src/test/groovy/example/micronaut/InfoSpec.groovy
package example.micronaut

import io.micronaut.http.HttpRequest
import io.micronaut.http.HttpResponse
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import spock.lang.Shared
import spock.lang.Specification
import jakarta.inject.Inject

@MicronautTest (1)
class InfoSpec extends Specification {

    @Shared
    @Client("/")
    @Inject
    HttpClient 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 @MicronautTest so the Micronaut framework will initialize the application context and the embedded server. More info.
2 Inject the HttpClient bean and point it to the embedded server.
3 Creating HTTP Requests is easy thanks to the Micronaut framework fluid API.
4 Use .body() to retrieve the parsed payload.

10. Testing the Application

To run the tests:

./mvnw test

11. Running the Application

To run the application, use the ./mvnw mn:run command, which starts the application on port 8080.

12. Next Steps

Explore more features with Micronaut Guides.

13. Help with the Micronaut Framework

The Micronaut Foundation sponsored the creation of this Guide. A variety of consulting and support services are available.

14. License

All guides are released with an Apache license 2.0 license for the code and a Creative Commons Attribution 4.0 license for the writing and media (images…​).