# 【SpingBoot】取得git branch

出處 : [https://blog.elliot.tw/?p=658](https://blog.elliot.tw/?p=658)

Spring Boot可以提供的Application資訊，參考以下連結

[https://docs.spring.io/spring-boot/reference/actuator/endpoints.html#actuator.endpoints.info](https://docs.spring.io/spring-boot/reference/actuator/endpoints.html#actuator.endpoints.info)

<table class=" table table-hover" id="bkmrk-id-name-description-"><thead><tr><th>ID</th><th>Name</th><th>Description</th><th>Prerequisites</th></tr></thead><tbody><tr><td>`build`</td><td>[`BuildInfoContributor`](https://github.com/spring-projects/spring-boot/tree/v3.1.0/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/BuildInfoContributor.java)</td><td>Exposes build information.</td><td>A `META-INF/build-info.properties` resource.</td></tr><tr><td>`env`</td><td>[`EnvironmentInfoContributor`](https://github.com/spring-projects/spring-boot/tree/v3.1.0/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/EnvironmentInfoContributor.java)</td><td>Exposes any property from the `Environment` whose name starts with `info.`.</td><td>None.</td></tr><tr><td>`git`</td><td>[`GitInfoContributor`](https://github.com/spring-projects/spring-boot/tree/v3.1.0/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/GitInfoContributor.java)</td><td>Exposes git information.</td><td>A `git.properties` resource.</td></tr><tr><td>`java`</td><td>[`JavaInfoContributor`](https://github.com/spring-projects/spring-boot/tree/v3.1.0/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/JavaInfoContributor.java)</td><td>Exposes Java runtime information.</td><td>None.</td></tr><tr><td>`os`</td><td>[`OsInfoContributor`](https://github.com/spring-projects/spring-boot/tree/v3.1.0/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/OsInfoContributor.java)</td><td>Exposes Operating System information.</td><td>None.</td></tr></tbody></table>

裡面提到了`git`，主要就是讀取`git.properties`

所以只要能產生`git.properties`

在此有兩種方式，一個是由Maven Plugin產生，另一個則由Gradle Plugin產生

[Generate Git Information](https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.build.generate-git-info)

## Maven – git-commit-id-maven-plugin

```xml
      <plugin>
        <groupId>io.github.git-commit-id</groupId>
        <artifactId>git-commit-id-maven-plugin</artifactId>
        <version>6.0.0</version>
        <executions>
          <execution>
            <id>get-the-git-infos</id>
            <goals>
              <goal>revision</goal>
            </goals>
            <phase>initialize</phase>
          </execution>
        </executions>
        <configuration>
          <generateGitPropertiesFile>true</generateGitPropertiesFile>         <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
          <commitIdGenerationMode>full</commitIdGenerationMode>
        </configuration>
      </plugin>
```

## Gradle – gradle-git-properties

```
    id("com.gorylenko.gradle-git-properties") version "2.4.1"
```

## Spring Boot – Application Yaml

再來只要在`managment`裡加入`info.git.enabled=true` 即可

```yaml
management:
  endpoints:
    web:
      exposure:
        include: '*'
  info:
    git:
      enabled: true
    java:
      enabled: true
```

## Actuator Info

簡單透過actuator 提供的endpoint  
[http://localhost:8080/actuator/info](http://localhost:8080/actuator/info)

```YAML
{
    "git": {
        "branch": "test/performance",
        "commit": {
            "id": "f95f360",
            "time": "2024-10-25T02:09:37Z"
        }
    },
    "build": {
        "artifact": "live-show",
        "name": "live-show",
        "time": "2024-10-25T02:54:21.087Z",
        "version": "2411.2.0",
        "group": "com.momo.app"
    },
    "java": {
        "version": "17.0.11",
        "vendor": {
            "name": "Amazon.com Inc.",
            "version": "Corretto-17.0.11.9.1"
        },
        "runtime": {
            "name": "OpenJDK Runtime Environment",
            "version": "17.0.11+9-LTS"
        },
        "jvm": {
            "name": "OpenJDK 64-Bit Server VM",
            "vendor": "Amazon.com Inc.",
            "version": "17.0.11+9-LTS"
        }
    }
}
```