关于git:聊聊如何验证线上的版本是符合预期的版本

2次阅读

共计 2902 个字符,预计需要花费 8 分钟才能阅读完成。

前言

当咱们想晓得部署我的项目的哪个版本有问题?当咱们想晓得线上运行的版本是否是咱们预期的版本?当咱们想把部署的版本与代码进行关联?如果是你用 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/gitDe…

总结

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

demo 链接

https://github.com/lyb-geek/springboot-learning/tree/master/springboot-git-commit

正文完
 0