How do I get the last commit programmatically in Java code? Jenkins / sbt

293 Views Asked by At

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.

1

There are 1 best solutions below

0
On

I will try to give solutions part by part.

Part 1 : Yes, you can run ShellScript(Shell Commands) from Jenkins Link.

Part 2 How to get the Email Id and other Stuff from GitCommit. For that Jenkins sever should have git command installed in build server. Create one conf file ex. /conf/reference which have

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. 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 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.

FYI: I haven't tested this code but as far as I remember working in Jenkins, we used to send email through this way.

#HappyCoding