Skip to content

Simple app for including git-commit informations into SpringBoot actuator /info endpoint and also customize an endpoint/api to show git-info directly.

Notifications You must be signed in to change notification settings

gary258796/git-info-actuator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

git-info-actuator

Overall

  • From doc from Spring.io, it tells what plugin we should use to get git information for us, including Maven and Gradle.
  • Also, from doc from Spring.io
    1. Plugin will generate git.properties file automatically.
    2. A GitProperties bean is autoconfigured if a git.properties file is available at the root of the classpath
    3. If a GitProperties bean is available, you can use the info endpoint to expose these properties.
  • So, we will first expose git infos into /info endpoint provided by SpringBoot actuator.
  • And if this isn't enough, we customize an /endpoint specific for showing git-infos.
  • Secure our endpoint if it contains some info we wouldn't want anybody to see.
  • Only expose endpoint within specific environment

Quick Go Through

  1. Download and launch SpringBoot application with 8080 port.
  2. All endpoints we offer now: http://localhost:8080/actuator
  3. /info endpoint: http://localhost:8080/actuator/info
  4. Custom /git-info endpoint
  5. Custom /defined-info endpoint
  6. Custom /custom-info endpoint

Add Dependencies into pom.xml

    <!-- Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Starter for using Spring Boot's Actuator which provides production ready features to help us monitor and manage our application -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

Add Plugin into pom.xml

<build>
      <plugins>
          <plugin>
              <groupId>pl.project13.maven</groupId>
              <artifactId>git-commit-id-plugin</artifactId>
          </plugin>
      </plugins>
  </build>

Run maven command

mvn clean compile

We should be able to see there is a 'git.properties' file under target folder like this. git-properties.png

And the content will be like this git-properties-content.png

⭐ We can decide what information to be included into git.properties by setting configuration of git plugin.
For more details of configuration, please refer to plugin_docs

Say we don't want infos that prefix with 'git.build', we set configuration as

<plugin>
    <groupId>pl.project13.maven</groupId>
    <artifactId>git-commit-id-plugin</artifactId>
    <configuration>
        <excludeProperties>
             <excludeProperty>git.build.*</excludeProperty>
        </excludeProperties>
    </configuration>
</plugin>

Expose using SpringBoot actuator

  1. Edit application.properties and add some configuration for actuator
# Disable all endpoints that SpringBoot Actuator provide by default
# For this project, we only enable /info endpoint
management.endpoints.enabled-by-default=false
management.endpoint.info.enabled=true

# Include only /info endpoint
management.endpoints.web.exposure.include=info

# full   -> show all info about git commit
# simple -> show only 'branch, 'commit-id' and 'commit-time'
management.info.git.mode=full

# Enable git info in order to show in /actuator/info endpoint
management.info.git.enabled=true
  1. Now, start our SpringBoot application, we should be able to access to http://localhost:8080/actuator/info and saw git infos.

Expose using Custom EndPoint

  • Add a class annotated with WebEndPoint and inject GitProperties like below

    😋 @ReadOperation is for GET method for our custom endpoint. For further details, please reference to Spring-docs

    @Component
    @WebEndpoint(id = "git-info")
    public class GitEndPoint {
    
        private final GitProperties gitProperties;
    
        @Autowired
        public GitEndPoint(@Autowired(required = false) GitProperties gitProperties) {
            this.gitProperties = gitProperties;
        }
    
        @ReadOperation
        public GitProperties getGitInfo() {
    
            if (ObjectUtils.isEmpty(gitProperties)) {
                return null;
            }
    
            return this.gitProperties;
        }
    }
    
  • Update our application.properties file, include also our custom endpoint

    # Include only /info endpoint
    management.endpoints.web.exposure.include=info, git-info
    
  • Start our SpringBoot application again, we should be able to get response like below by accessing to http://localhost:8080/actuator/git-info custom-endpoint-git-info.png

    ⚠️
    If the info from GitProperties are not sufficient, we could directly return the whole content inside the Property file like below, or select needed info ourselves and return.
    We should be able to access by http://localhost:8080/actuator/git-info/true

    /**
    * Return detail git info if ${showDetail} is true.
    * @return detail git information
    */
    @ReadOperation
    public Object getGitInfo(@Selector boolean showDetail) {
    
      if (ObjectUtils.isEmpty(gitProperties) || !showDetail) {
          return null;
      }
    
      return getGitDetail();
    }
    
    /**
    * Get all properties from git.properties file
    */
    private Object getGitDetail() {
    
      Properties props;
    
      try {
          props = PropertiesLoaderUtils.loadAllProperties("git.properties");
      } catch (IOException e) {
          throw new RuntimeException(e);
      }
    
      return props;
    }
    

Expose using Custom RestControllerEndPoint

Add a class annotate with @RestControllerEndPoint and define endpoints.
I personally prefer using RestControllerEndPoint because I can set up endpoints with all familiar annotation when we're developing apis, such as @GetMapping, @PostMapping, @RequestBody...etc.
Access this endpoint by:

Expose using Custom RestControllerEndPoint with self-selected properties

Add a class annotate with @RestControllerEndPoint and inject all properties we want using @Value annotation.

⚠️
Remember to let SpringBoot know git.properties in order to inject value by @Value.
No matter we choose to achieve by using @PropertySource, config PropertySourcesPlaceholderConfigurer or any ways mentioned in this article.

Access this endpoint by:

Notice

  1. Tracking issue for personal use of chinese commit message.

Reference

  1. 聊聊如何验证线上的版本是符合预期的版本
  2. Custom Endpoint in Spring Boot Actuator
  3. Enhancing Spring Boot Actuator with Custom Endpoints
  4. Injecting Git Information Into Spring

About

Simple app for including git-commit informations into SpringBoot actuator /info endpoint and also customize an endpoint/api to show git-info directly.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages