Expose Git information with Spring Boot Actuator

Last Updated:  June 13, 2020 | Published: June 2, 2018

In today's #HOWTO blog post I want to show you a way to expose Git information (like commit hash, commit timestamp, branch) of the deployed application using Spring Boot Actuator. If you are not versioning your Maven application with the <version>X.Y.Z</version> tag and you want to know which state of your application is deployed to production/development, this blog post can be quite helpful. In addition, you will also get information about the Maven build (version, the timestamp of the build, etc.). You could use this information for displaying the version of your API backend service in your frontend application.

For this tutorial, we'll make use of Spring Boot 2.3.0, Java 11, and Maven as the build tool.  You can find the whole example on Github.

Add Spring Boot Maven project setup

The required Maven dependencies are the following:

To extract the information about your git repository your application has to reside in a folder with a) a .git directory in one of the parent folders or b) the .git directory directly in the working folder.

The pl.project13.maven:git-commit-id-plugin will pick up the information during the build and store it per default to target/classes/git.propertiesIn addition, you have to configure an execution goal for your spring-boot-maven-plugin (this maven plugin is always present if you generated your Spring Boot app with the Spring Initializer). Your <build> section in your pom.xml should look like the following:

Access the Git information with a Spring Boot Actuator endpoint

The collected data will be available at /actuator/info which is enabled per default and you don't have to configure the Actuator endpoints with your application.properties file. You can now build your Spring Boot application with mvn clean package and start it from your IDEA or with java -jar target/your-application-name.jar on the console. If you use the default port you can access the endpoint with your browser on http://localhost:8080/actuator/info. The output should contain the following information:

If you don't want to expose this information directly to your clients you can secure it with Spring Security. Another solution is to use a different port for your Actuator endpoints and disallow public access to it.

With Spring Boot you just have to set the property management.server.port to a different port than server.port property in your application.properties or application.yml file and the Actuator endpoints won't be accessible on port 8080.

Please note: If /actuator/info does not return any information about your git repository, try to run the application with mvn spring-boot:run to ensure the git-commit-id-plugin is executed before the application starts.

You can find the whole example on Github and more useful guides for Spring Boot on my blog.

git commit -m “see you at the next blog post”

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Sign up for Our Mailing List And Get

the Testing Java Applications ($9) Cheat Sheet for Free

Testing Java Applications Cheat Sheet Cover