使用git插件标记当前发布版本

使用git插件标记当前发布版本

需求:排查问题时发现测试版本和本地代码不一致,导致排查问题困难,将发布的二进制文件和代码进行关联,方便排查问题。

节点下增加git-commit-id-plugin插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<build>
<plugins>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>2.2.6</version>
<executions>
<execution>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<verbose>true</verbose>
<dateFormat>yyyy-MM-dd'T'HH:mm:ssZ</dateFormat>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<failOnNoGitDirectory>false</failOnNoGitDirectory>
<format>json</format>
<excludeProperties>
<excludeProperty>git.remote.origin.url</excludeProperty>
</excludeProperties>
</configuration>
</plugin>

</plugins>
</build>

代码中增加git读取接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@Slf4j
@RestController
@RequestMapping("/version")
public class VersionController {
private final String gitProperties = "git.properties";

@GetMapping(value = "/info", produces = {"application/json;charset=UTF-8"})
@ResponseBody
public String getVersionInfo() {
return readGitProperties();
}

private String readGitProperties() {
ClassLoader classLoader = getClass().getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream(gitProperties);
try {
return readFromInputStream(inputStream);
} catch (IOException e) {
// 注意:这里的返回值需要能被解析成json,放到details中
return "Git information could not be retrieved";
}
}

private String readFromInputStream(InputStream inputStream) throws IOException {
StringBuilder resultStringBuilder = new StringBuilder();
try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = br.readLine()) != null) {
resultStringBuilder.append(line);
}
}
return resultStringBuilder.toString();
}

}

访问接口,返回:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
"git.branch" : "master",
"git.build.host" : "DESKTOP-EE7G6UA",
"git.build.time" : "2024-01-03T16:23:23+0800",
"git.build.user.email" : "zzz",
"git.build.user.name" : "zzz",
"git.build.version" : "1.0.0",
"git.closest.tag.commit.count" : "",
"git.closest.tag.name" : "",
"git.commit.id" : "f22e7eb55f8d5a46860bf2d1e61e5b9a2d6bc62a",
"git.commit.id.abbrev" : "f22e7eb",
"git.commit.id.describe" : "f22e7eb-dirty",
"git.commit.id.describe-short" : "f22e7eb-dirty",
"git.commit.message.full" : "fix the permission.",
"git.commit.message.short" : "fix the permission.",
"git.commit.time" : "2023-12-25T11:53:25+0800",
"git.commit.user.email" : "zzz",
"git.commit.user.name" : "zzz",
"git.dirty" : "true",
"git.tags" : "",
"git.total.commit.count" : "304"
}

获取git.commit.id,根据commitId拉取代码进行问题排查。

也可以加入到健康检查中:

定义健康检查接口:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@Component
public class PackageInfoHealthIndicator implements HealthIndicator {

@Override
public Health health() {
String s = readGitProperties();
JSONObject entries = JSONUtil.parseObj(s);
// 检查应用程序的状态,返回一个 Health 对象
return Health.up().withDetails(entries).build();
}
private String readGitProperties() {
ClassLoader classLoader = getClass().getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("git.Properties");
try {
return readFromInputStream(inputStream);
} catch (IOException e) {
return "Git information could not be retrieved";
}
}

private String readFromInputStream(InputStream inputStream) throws IOException {
StringBuilder resultStringBuilder = new StringBuilder();
try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = br.readLine()) != null) {
resultStringBuilder.append(line);
}
}
return resultStringBuilder.toString();
}
}

访问健康检查接口:

http://localhost:8086/actuator/health

返回值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
{
"status": "UP",
"components": {
"db": {
"status": "UP",
"details": {
"database": "MySQL",
"validationQuery": "isValid()"
}
},
"discoveryComposite": {
"status": "UP",
"components": {
"discoveryClient": {
"status": "UP",
"details": {
"services": [
"xxxx"
]
}
}
}
},
"diskSpace": {
"status": "UP",
"details": {
"total": 488554622976,
"free": 480372101120,
"threshold": 10485760,
"exists": true
}
},
"mongo": {
"status": "UP",
"details": {
"version": "7.0.4"
}
},
"nacosConfig": {
"status": "UP"
},
"nacosDiscovery": {
"status": "UP"
},
"packageInfo": {
"status": "UP",
"details": {
"git.branch": "dev",
"git.build.host": "xxxx",
"git.build.time": "2024-01-04T10:11:03+0800",
"git.build.user.email": "xxxx",
"git.build.user.name": "xxxx",
"git.build.version": "1.0-SNAPSHOT",
"git.closest.tag.commit.count": "",
"git.closest.tag.name": "",
"git.commit.id": "b637312b1e686c250f8995abec81b335e22d18c8",
"git.commit.id.abbrev": "b637312",
"git.commit.id.describe": "b637312-dirty",
"git.commit.id.describe-short": "b637312-dirty",
"git.commit.message.full": "xxxxmaster",
"git.commit.message.short": "xxxxmaster",
"git.commit.time": "2024-01-04T09:17:46+0800",
"git.commit.user.email": "xxxx",
"git.commit.user.name": "xxxx",
"git.dirty": "true",
"git.tags": "",
"git.total.commit.count": "1478"
}
},
"ping": {
"status": "UP"
},
"redis": {
"status": "UP",
"details": {
"version": "7.0.11"
}
},
"refreshScope": {
"status": "UP"
}
}
}

参考链接

https://www.cnblogs.com/iiot/p/16077597.html

https://github.com/git-commit-id/git-commit-id-maven-plugin/issues/315