温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么使用git验证线上的版本是否符合预期

发布时间:2022-07-22 13:40:24 来源:亿速云 阅读:104 作者:iii 栏目:开发技术

这篇文章主要介绍了怎么使用git验证线上的版本是否符合预期的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇怎么使用git验证线上的版本是否符合预期文章都会有所收获,下面我们一起来看看吧。

正文

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

怎么使用git验证线上的版本是否符合预期


如果觉得上面的信息不够多,我们可以通过自定义端点或者自己写一个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验证线上的版本是否符合预期


如果仍然觉得上述的信息还是不够详细,那可以通过解析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验证线上的版本是否符合预期

关于“怎么使用git验证线上的版本是否符合预期”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“怎么使用git验证线上的版本是否符合预期”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

git
AI