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:
1 2 3 4 5 6 7 8 9 10 | <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | a) my-enterprise-applications/ ├── .git/ │ ├── billing-application/ │ ├── src/ │ └── pom.xml │ └── product-application/ ├── src/ └── pom.xml b) billing-application/ ├── .git/ ├── src/ └── pom.xml |
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.properties
. In 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>build-info</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>pl.project13.maven</groupId> <artifactId>git-commit-id-plugin</artifactId> </plugin> </plugins> </build> |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | { "git":{ "commit": { "time": "2018-06-02T07:31:06Z", "id": "91c16c2" }, "branch": "master" }, "build": { "version": "0.0.1-SNAPSHOT", "artifact": "expose-git-information-actuator", "name": "expose-git-information-actuator", "group": "de.rieckpil.blog", "time": "2018-06-02T07:52:27.522Z" } } |
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.
1 2 3 4 5 6 7 8 9 10 | # application.properties management.server.port=8081 server.port=8080 # application.yml management: server: port: 8081 server: 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”