项目中的代码放git上管理,jenkins的CICD的流水线打包发布下,经常容易忘记提交代码或者合并分支等。导致调试时和预期不一致,如果把代码的git提交记录在运行时展示出来,就可以快速确认是否是环境部署的问题导致的。
build.gradle中增加
plugins { id "com.gorylenko.gradle-git-properties" version "1.5.1" }
java代码:
@Slf4j @Component public class GitCommitInfoApplicationRunner implements ApplicationRunner { @Value("${spring.application.name:}") private String applicaitonName; @Override public void run(ApplicationArguments args) throws Exception { try { Resource resource = new ClassPathResource("git.properties"); Properties properties = new Properties(); properties.load(new InputStreamReader(resource.getInputStream())); log.info("{}打包日志: 分支[{}],代码版本[{}],构建时间[{}],提交时间[{}]", applicaitonName, properties.getProperty("git.closest.tag.name"), properties.getProperty("git.commit.id.abbrev"), properties.getProperty("git.build.time"), properties.getProperty("git.commit.time")); log.info("{}-GitCommitInfo: [{}]", applicaitonName, properties); } catch (FileNotFoundException e) { log.warn("git.properties文件不存在"); } catch (IOException e) { log.warn("git.properties文件读取失败"); } } }