git-commit-id-plugin
需求:排查问题时发现测试版本和本地代码不一致,导致排查问题困难,将发布的二进制文件和代码进行关联,方便排查问题。
<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>
</configuration>
</plugin>
代码中增加git读取接口
@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) {
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();
}
}
访问接口,返回:
{
"git.branch": "dev",
"git.build.host": "DESKTOP-F8DISTL",
"git.build.time": "2022-03-28T10:07:59+0800",
"git.build.user.email": "xxx",
"git.build.user.name": "xxx",
"git.build.version": "1.0.0-SNAPSHOT",
"git.closest.tag.commit.count": "1470",
"git.closest.tag.name": "v2.1.2-210331",
"git.commit.id": "ea919121fb5ec62ef8e5034ad9501d5dee45c3ea",
"git.commit.id.abbrev": "ea91912",
"git.commit.id.describe": "v2.1.2-210331-1470-gea91912-dirty",
"git.commit.id.describe-short": "v2.1.2-210331-1470-dirty",
"git.commit.message.full": "fix(YTHBUG-xxx):xxxxx",
"git.commit.message.short": "fix(YTHBUG-xxx):xxxxx",
"git.commit.time": "2022-03-28T10:05:38+0800",
"git.commit.user.email": "xxx",
"git.commit.user.name": "xxx",
"git.dirty": "true",
"git.remote.origin.url": "xxx",
"git.tags": "",
"git.total.commit.count": "4003"
}
获取git.commit.id,根据commitId拉取代码进行问题排查。