git-commit-id-plugin


git-commit-id-plugin

需求:排查問題時發現測試版本和本地代碼不一致,導致排查問題困難,將發布的二進制文件和代碼進行關聯,方便排查問題。

節點下增加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拉取代碼進行問題排查。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM