Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring - show git commit ID on custom health endpoint

Tags:

spring

I am trying to show the Git version info (branch, commit etc) on my custom health endpoint.

I tried using management.info.git.mode=full + git-commit-id-plugin but there is no direct way to extract the git info into a Java class. If there is, this will be the ideal way.

I also tried the same git-commit-id-plugin with Value annotations in my Java class like so @Value("${git.commit.id}") but Spring can't find the property values. I see the git.properties file created in the target dir.

What am I missing here? thanks in advance

like image 976
john Avatar asked Feb 05 '19 09:02

john


People also ask

How do you get the ID of a commit?

To find a git commit id (or hash), you can simply use the git log command. This would show you the commit history, listing the commits in chronological order, with the latest commit first.

Is commit ID unique?

Commit IDs are unique SHA-1 hashes that are created whenever a new commit is recorded.

What does git commit ID plugin do?

The git commit-id plugin is very useful to depict the state of the git repository when a binary has been created. Imagine the case of multiple deployments in a shared staging environment using the same version.

How do you expose endpoints in spring boot?

In a Spring Boot application, we expose a REST API endpoint by using the @RequestMapping annotation in the controller class. For getting these endpoints, there are three options: an event listener, Spring Boot Actuator, or the Swagger library.


2 Answers

We have to configure PropertyPlaceHolderConfigurer bean so that we can able to access the property file generated by the plugin, Please use the below code for your reference,

@Bean
    public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
        PropertySourcesPlaceholderConfigurer propsConfig 
          = new PropertySourcesPlaceholderConfigurer();
        propsConfig.setLocation(new ClassPathResource("git.properties"));
        propsConfig.setIgnoreResourceNotFound(true);
        propsConfig.setIgnoreUnresolvablePlaceholders(true);
        return propsConfig;
    }

then in your custom health check class, you can use,

@Value("${git.commit.id}") private String commitId;

I hope this will resolve your problem.

like image 141
VelNaga Avatar answered Sep 21 '22 01:09

VelNaga


With Spring Boot 2 you can get this information using git-commit-id-plugin in info endpoint. This is how you can configure it POM File

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

Sample Response http://localhost:8080/actuator/info

{ 
"git":{ 
"branch":"some-name",
"commit":{ 
"id":"ef569c2",
"time":1579000598.000000000
}
},
"build":{ 
"artifact":"xxx",
"name":"xxxx",
"time":1579020527.139000000,
"version":"0.0.1-SNAPSHOT",
"group":"xxxx"
}
} 
like image 38
Niraj Sonawane Avatar answered Sep 18 '22 01:09

Niraj Sonawane