简体   繁体   English

如何在 Java 代码中以编程方式获取最后一次提交? 詹金斯 / sbt

[英]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 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例如,我希望它执行以下操作: Git:查看我的最后一次提交

Where the email basically says:电子邮件基本上说:

Subject: Test Results主题:测试结果

Message: All your tests passed in dev for commit 0e39756383662573.消息:您的所有测试都在 dev 中通过了提交 0e39756383662573。

Does Jenkins provide this functionality already? Jenkins 是否已经提供此功能? 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 )?另外,有没有办法以编程方式获取最近提交的作者的电子邮件(例如,可能使用http://www.eclipse.org/jgit/http://javagit.sourceforge.net )?

I don't really care how I get email notifications - I just want them and I can't use TravisCI.我真的不在乎我是如何收到电子邮件通知的——我只是想要它们,我不能使用 TravisCI。

I will try to give solutions part by part.我将尝试部分地给出解决方案。

Part 1 : Yes, you can run ShellScript(Shell Commands) from Jenkins Link .第 1 部分:是的,您可以从Jenkins Link运行 ShellScript(Shell Commands)。

Part 2 How to get the Email Id and other Stuff from GitCommit.第 2 部分如何从 GitCommit 获取电子邮件 ID 和其他内容。 For that Jenkins sever should have git command installed in build server.为此, Jenkins服务器应该在构建服务器中安装 git 命令。 Create one conf file ex.创建一个 conf 文件 ex. /conf/reference which have /conf/reference其中有

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

When making your build run the command在构建时运行命令

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

above code will put the values in reference.conf .上面的代码会将值放在reference.conf Now you can use to get the info and send the mail.现在您可以使用来获取信息并发送邮件。 As far as I know, Jenkins gives the capability to send the Email.据我所知,Jenkins 提供了发送电子邮件的功能。 Jenkins work on the environment variables rather than putting this into reference.conf you can put this in Environment variable and use the environment variables to send the mail. Jenkins 处理环境变量,而不是将其放入reference.conf您可以将其放入环境变量并使用环境变量发送邮件。

FYI: I haven't tested this code but as far as I remember working in Jenkins, we used to send email through this way.仅供参考:我还没有测试过这段代码,但据我记得在 Jenkins 工作,我们曾经通过这种方式发送电子邮件。

#HappyCoding #HappyCoding

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM