git驗證線上的版本是否符合預期
正文
git-commit-id-maven-plugin插件,會根據(jù)當前分支的版本號生成一個git.properties文件。
git.properties內(nèi)容形如下
git.branch=master git.build.host=xxx git.build.time=2022-03-01T20\:33\:43+0800 git.build.user.email=aaa@qq.com git.build.user.name=aaa git.build.version=1.0-SNAPSHOT git.closest.tag.commit.count= git.closest.tag.name= git.commit.id=6dab4430864e3e4a9fc1ba6f6b93f278100d4e2e git.commit.id.abbrev=6dab443 git.commit.id.describe=6dab443-dirty git.commit.id.describe-short=6dab443-dirty git.commit.message.full=Add README.md git.commit.message.short=Add README.md git.commit.time=2022-03-01T16\:24\:48+0800 git.commit.user.email=aa@example git.commit.user.name=aa git.dirty=true git.remote.origin.url=http://hello git.tags= git.total.commit.count=1
如何使用
本文以springboot項目為例,springboot項目的parent pom里面已經(jīng)內(nèi)嵌git-commit-id-maven-plugin插件管理依賴。如下
<pluginManagement>
?<plugins>
????<plugin>
??????????<groupId>pl.project13.maven</groupId>
??????????<artifactId>git-commit-id-plugin</artifactId>
??????????<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>
????????????<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
??????????</configuration>
????????</plugin>
???</plugins>
</pluginManagement>項目中做如下配置
1、在我們的項目中顯式引入git-commit-id-plugin插件
<build> ????????<plugins> ????????????<plugin> ????????????????<groupId>pl.project13.maven</groupId> ????????????????<artifactId>git-commit-id-plugin</artifactId> ????????????</plugin> ????????</plugins> ?</build>
2、通過和actuator集成,顯示git信息
a、在項目中引入actuator GAV
<dependency> ????????????<groupId>org.springframework.boot</groupId> ????????????<artifactId>spring-boot-starter-actuator</artifactId> ????????</dependency>
b、瀏覽器訪問http://ip : port/actuator/info

如果覺得上面的信息不夠多,我們可以通過自定義端點或者自己寫一個controller把信息展示出來
詳細的信息可以通過org.springframework.boot.info.GitProperties展示
本示例以自定義端點為例。示例如下
@Endpoint(id =?"git")
@Component
public?class?GitEndpoint?{
????@Autowired(required =?false)
????private?GitProperties gitProperties;
????@ReadOperation
????public?Object?info()?throws?IOException?{
????????if(ObjectUtils.isEmpty(gitProperties)){
????????????return?new?HashMap<>();
????????}
????????return?gitProperties;
????}
}在application.yml中開放自定義端點
management: ??endpoints: ????web: ??????exposure: ????????include:?'git'
瀏覽器訪問http://ip : port/actuator/git

如果仍然覺得上述的信息還是不夠詳細,那可以通過解析git.properties文件顯示。示例
@Endpoint(id =?"gitDetail")
@Slf4j
@Component
public?class?GitDetailEndPoint?{
????@ReadOperation
????public?Object?detail()?throws?IOException?{
????????Properties props =?null;
????????try?{
????????????props = PropertiesLoaderUtils.loadAllProperties("git.properties");
????????????return?props;
????????}?catch?(IOException e) {
????????????log.error("git.properties not found");
????????}?finally?{
????????}
????????return?new?HashMap<>();
????}
}在application.yml中開放自定義端點
management: ??endpoints: ????web: ??????exposure: ????????include:?'gitDetail'
瀏覽器訪問http://ip:port/actuator/gitDetail

總結(jié)
git-commit-id-maven-plugin在分布式或者微服務項目中,用來驗證項目版本還是挺有用的,推薦大家有機會用一下
以上就是git驗證線上的版本是否符合預期的詳細內(nèi)容,更多關于git驗證線上版本的資料請關注腳本之家其它相關文章!
相關文章
詳解git使用小結(jié)(本地分支與遠程分支、git命令)
這篇文章主要介紹了git使用小結(jié)(本地分支與遠程分支、git命令),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
git提交報錯pre -commit hook failed (add 
這篇文章主要介紹了git提交報錯pre -commit hook failed (add --no-verify)問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11

