Search code examples
spring

Spring - show git commit ID on custom health endpoint


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


Solution

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