从战中反弹:将Git提交信息作为JSON返回

在某些情况下,我们必须知道部署到远程服务器的Web应用程序的确切版本。 例如,客户可能想知道我们是否已经在服务器X上部署了错误修复程序。

当然,我们可以尝试使用“传统”方法找到该问题的答案。 问题是:

  • 没有人不记得是谁更新了服务器X或何时更新了服务器X。
  • 更新它的人不记得哪个是构建中包含的最后一次提交。

换句话说,我们被搞砸了。 我们可以尝试测试服务器X上是否仍然存在该错误,但这并不能真正帮助我们,因为我们的错误修复可能无法正常工作。

这篇博客文章描述了我们如何解决这个问题。 让我们从提取Git存储库的构建时状态开始。

如果使用Spring Boot,则应使用Spring Boot Actuator 。 它可以帮助您发布有关Git存储库状态的信息

如果您尚未阅读以下博客文章, 则应先阅读它们,然后再继续阅读此博客文章

提取我们的Git仓库的构建时间状态

我们可以使用Maven Git Commit Id插件提取Git存储库的构建时状态。 让我们了解如何配置Maven Git Commit Id插件并将提取的信息添加到属性文件中。

首先 ,我们需要配置资源目录的位置,并确保将从属性文件中找到的属性占位符替换为实际的属性值。 为此,我们可以将以下XML添加到pom.xml文件的build部分中:

<resources>
	<resource>
		<directory>src/main/resources</directory>
		<filtering>true</filtering>
		<includes>
			<include>**/*.properties</include>
		</includes>
	</resource>
</resources>

其次 ,我们需要配置Maven Git Commit Id插件。 我们可以按照以下步骤进行操作:

  1. 将Maven Git提交ID插件添加到我们的构建中。
  2. 确保在默认生命周期的初始化阶段调用了Maven Git Commit Id插件的修订目标。
  3. 配置.git目录的位置。

我们需要将以下XML添加到pom.xml文件的plugins部分:

<plugin>
	<groupId>pl.project13.maven</groupId>
	<artifactId>git-commit-id-plugin</artifactId>
	<version>2.1.13</version>
	<!--
		 Ensure that the revision goal is invoked during the initialize
		 phase.
	-->
	<executions>
		<execution>
			<goals>
				<goal>revision</goal>
			</goals>
		</execution>
	</executions>
	<configuration>
		<!--
			Configure the location of the .git directory.
		-->
		<dotGitDirectory>${project.basedir}/../.git</dotGitDirectory>
	</configuration>
</plugin>

如果您对Maven Git Commit Id插件的默认配置不满意,则应仔细阅读其自述文件:

  • 使用该插件提供了带注释的XML配置文件,该文件描述了Maven Git Commit Id插件的配置。
  • 深入的配置选项描述了Maven Git Commit Id插件的每个配置选项。

第三 ,我们需要创建属性文件,其中包含从Git存储库中提取的信息。 application.properties文件如下所示:

git.tags=${git.tags}
git.branch=${git.branch}
git.dirty=${git.dirty}
git.remote.origin.url=${git.remote.origin.url}

git.commit.id=${git.commit.id}
git.commit.id.abbrev=${git.commit.id.abbrev}
git.commit.id.describe=${git.commit.id.describe}
git.commit.id.describe-short=${git.commit.id.describe-short}
git.commit.user.name=${git.commit.user.name}
git.commit.user.email=${git.commit.user.email}
git.commit.message.full=${git.commit.message.full}
git.commit.message.short=${git.commit.message.short}
git.commit.time=${git.commit.time}

git.build.user.name=${git.build.user.name}
git.build.user.email=${git.build.user.email}
git.build.time=${git.build.time}

现在,我们已经配置了Maven Git Commit Id插件。 编译项目时,将从application.properties文件中找到的属性占位符替换为从Git存储库中提取的实际属性值。

target / classes目录中找到的application.properties文件如下所示:

git.tags=
git.branch=master
git.dirty=true
git.remote.origin.url=git@github.com:pkainulainen/spring-from-the-trenches.git

git.commit.id=1bdfe9cf22b550a3ebe170f60df165e5c26448f9
git.commit.id.abbrev=1bdfe9c
git.commit.id.describe=1bdfe9c-dirty
git.commit.id.describe-short=1bdfe9c-dirty
git.commit.user.name=Petri Kainu
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值