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验证线上版本的资料请关注脚本之家其它相关文章!

相关文章

  • 关于rpc长连接与短连接的思考记录

    关于rpc长连接与短连接的思考记录

    文章总结了RPC项目中长连接和短连接的处理方式,包括RPC和HTTP的长连接与短连接的区别、TCP的保活机制、客户端与服务器的连接模式及其利弊分析,文章强调了在实际应用中需要根据具体情况选择长连接还是短连接,并讨论了负载均衡器在RPC中的作用
    2025-01-01
  • 详解cron表达式

    详解cron表达式

    Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义。接下来通过本文给大家详细介绍cron表达式内容,感兴趣的朋友一起看看吧
    2018-04-04
  • Postman 使用指南及小技巧

    Postman 使用指南及小技巧

    Postman 简化了构建 API 的每个步骤,并简化了协作,这样就可以更快地创建 API。接下来通过本文给大家介绍Postman 使用指南及小技巧,感兴趣的朋友跟随小编一起看看吧
    2021-12-12
  • Visual Studio自定义项目模版

    Visual Studio自定义项目模版

    这篇文章介绍了Visual Studio自定义项目模版的方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • 如何解决git revert后再次merge代码丢失问题

    如何解决git revert后再次merge代码丢失问题

    这篇文章主要介绍了如何解决git revert后再次merge代码丢失问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-04-04
  • CLion中编译ROS工程的配置详细教程

    CLion中编译ROS工程的配置详细教程

    这篇文章主要介绍了CLion中编译ROS工程的配置,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • 详解git使用小结(本地分支与远程分支、git命令)

    详解git使用小结(本地分支与远程分支、git命令)

    这篇文章主要介绍了git使用小结(本地分支与远程分支、git命令),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • UTF-8 BOM 可能导致样式错乱的解决方法

    UTF-8 BOM 可能导致样式错乱的解决方法

    utf-8 是一种在web应用中经常使用的一种 unicode 字符的编码方式,使用 utf-8 的好处在于它是一种变长的编码方式,对于 ANSII 码编码长度为1个字节,这样的话在传输大量 ASCII 字符集的网页时,可以大量节约网络带宽。
    2009-06-06
  • 怎样写commit log记录及如何提交有哪些约定

    怎样写commit log记录及如何提交有哪些约定

    这篇文章主要为大家介绍了怎样写commit log记录以及及如何提交有哪些约定,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • git提交报错pre -commit hook failed (add --no-verify)问题及解决

    git提交报错pre -commit hook failed (add 

    这篇文章主要介绍了git提交报错pre -commit hook failed (add --no-verify)问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11

最新评论