首页 > 解决方案 > How do I get the last commit programmatically in Java code? Jenkins / sbt

问题描述

I started writing a little tool that basically can do something (ex. compile or test code) and then send an email if it fails.

https://github.com/JohnReedLOL/EmailTestingBot

I want to add a feature where this tool can programmatically look at the last commit in the working directory, look at the author of the commit, extract their email, and then email that person whether their commit resulted in a pass or a failure.

For example, I want it to do something like: Git: See my last commit

Where the email basically says:

Subject: Test Results

Message: All your tests passed in dev for commit 0e39756383662573.

Does Jenkins provide this functionality already? I want to make my setup email the person who put in the most recent commit.

Also, is there a way I can obtain the email of the author of the most recent commit programmatically (ex. perhaps with http://www.eclipse.org/jgit/ or http://javagit.sourceforge.net )?

I don't really care how I get email notifications - I just want them and I can't use TravisCI.

标签: scalajenkinssbtdevopssbt-0.13

解决方案


我将尝试部分给出解决方案。

第 1 部分:是的,您可以从Jenkins Link 运行 ShellScript(Shell Commands) 。

第 2 部分 如何从 GitCommit 获取电子邮件 ID 和其他东西。对于该Jenkins服务器应该在构建服务器中安装 git 命令。创建一个conf文件ex。/conf/reference其中有

app {
  git {
     commit = "CURRENT_COMMIT"
     repo = "CURRENT_REPO"
     timestamp = "CURRENT_TIMESTAMP"
     emailId = "EMAIL_ID"
     }
}

当你的构建运行命令时

sed -i'' "s/CURRENT_COMMIT/$(git rev-parse HEAD)/g" conf/reference.conf
sed -i'' "s^CURRENT_REPO^$(git config --get remote.origin.url)^g" conf/reference.conf
sed -i'' "s/CURRENT_TIMESTAMP/$(git show -s --format=%ci HEAD)/g" conf/reference.conf
sed -i'' "s/EMAIL_ID/git --no-pager show -s --format='%an <%ae>' CURRENT_COMMIT/g" conf/reference.conf

上面的代码会将值放入reference.conf. 现在您可以使用它来获取信息并发送邮件。据我所知,Jenkins 提供了发送电子邮件的能力。詹金斯处理环境变量而不是将其放入reference.conf您可以将其放入环境变量并使用环境变量发送邮件。

仅供参考:我没有测试过这段代码,但据我记得在 Jenkins 工作,我们曾经通过这种方式发送电子邮件。

#快乐编码


推荐阅读