#HOWTO: Expose git information with Spring Boot’s Actuator

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 via Spring Boot’s Actuator dependency. 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 build information of the Maven build (version, the timestamp of the build …). You could use this information for displaying the version of your API backend service in your frontend application.

For this tutorial, I will use the Spring Boot version 2.0.2.RELEASE and Java 1.8 with Maven as the build tool.  You can find the whole example on Github.

The required Maven dependencies are the following (you could omit the spring-boot-starter-test if you want):

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

     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
     </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.

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:

<build>
    <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>

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 and the output should contain the following information:

{  
   "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 could either use Spring Security to secure your Actuator endpoints and allow only a  defined collection of users to view this data or use a different port for your Actuator endpoints and use a firewall restrict the public access only to our “production” port. 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.

# application.properties
management.server.port=8081
server.port=8080

# application.yml

management:
  server:
     port: 8081
server:
  port: 8080

You can find the whole example on Github.

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

 

 

Leave a comment

Your email address will not be published. Required fields are marked *