git验证线上的版本是否符合预期

 更新时间:2022年07月23日 11:58:15   作者:Linyb极客之路  
当我们想知道部署项目的哪个版本有问题?当我们想知道线上运行的版本是否是我们预期的版本?当我们想把部署的版本与代码进行关联?如果是你用git来做版本管理,那就可以使用git-commit-id-maven-plugin插件来实现上述功能

正文

git-commit-id-maven-plugin插件,会根据当前分支的版本号生成一个git.properties文件。

git.properties内容形如下

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里面已经内嵌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

总结

git-commit-id-maven-plugin在分布式或者微服务项目中,用来验证项目版本还是挺有用的,推荐大家有机会用一下

demo链接

以上就是git验证线上的版本是否符合预期的详细内容,更多关于git验证线上版本的资料请关注脚本之家其它相关文章!

相关文章

  • 详解git基本操作和指令

    详解git基本操作和指令

    这篇文章主要介绍了git基本操作和指令的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2020-11-11
  • String与string的区别(注意大小写)

    String与string的区别(注意大小写)

    String与string的区别(注意大小写)
    2010-06-06
  • 各类常见语言清除网页缓存方法汇总

    各类常见语言清除网页缓存方法汇总

    这篇文章主要介绍了各类常见语言清除网页缓存方法汇总,包括了常见的html、asp、php与java,非常具有实用价值,需要的朋友可以参考下
    2014-10-10
  • Git本地仓库基本操作及技巧

    Git本地仓库基本操作及技巧

    这篇文章主要介绍了Git本地仓库基本操作及一些小技巧,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • 目标检测mAP的概念及公式详解

    目标检测mAP的概念及公式详解

    这篇文章主要为大家介绍了我们在进行目标检测时需要用到的mAP概念及公式详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • 遇到不能复制的网站怎么办?

    遇到不能复制的网站怎么办?

    有时我们看到喜欢的网页内容时定会产生复制下来为我所用的冲动,不过当你点击鼠标时它却没有任何反应,选择的内容没有任何变化,不禁有点扫兴。不要紧,办法总比困难多!
    2009-06-06
  • Unity开发VR项目问题总结分析

    Unity开发VR项目问题总结分析

    本篇文章主要对Unity开发VR项目会遇到的一些问题总结,针对这些问题进行分析解决,有需要的朋友可以借鉴参考下,希望对大家有所帮助
    2021-09-09
  • 自定义 Github Action 库实战详解

    自定义 Github Action 库实战详解

    这篇文章主要为大家介绍了自定义 Github Action 库实战详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • kali添加开机自启的方法

    kali添加开机自启的方法

    采用systemd的方法,kali默认是没有rc.local的,需要自己创建。本方法也适用于ubuntu 18.04 64bit,对kali添加开机自启知识感兴趣的朋友一起看看吧
    2022-09-09
  • 文章中优酷视频全屏及去除广告在线转换

    文章中优酷视频全屏及去除广告在线转换

    很多网站发表了引用优酷视频不能全屏,或一点全屏又跳到官方网了,结果又要重新缓冲。用户体验特别不好。
    2010-09-09

最新评论