Created
December 21, 2018 19:34
-
-
Save mhpaler/0c7d6bbe7cbf4cf842ccc822f8b2e0ae to your computer and use it in GitHub Desktop.
Groovy script to add PR title as a environment variable to Jenkins build environment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I had the need to access the PR title in a build triggered | |
// by a merge of that PR into the targeted repository. In our case, | |
// we use the GitHub hook trigger for GITScm polling setting provided | |
// by the Github plugin. This fires a webhook at your jenkins instance | |
// (must be configured in Github) when changes are pushed into the | |
// target repo. The code below is put into the Groovy script window | |
// provded by the Environment Injector Plugin. | |
// Get the current build environment variables. | |
def thr = Thread.currentThread() | |
def build = thr?.executable | |
def envVarsMap = build.parent.builds[0].properties.get("envVars") | |
// Build the command to extract the commit message using git log. | |
// Note PR title gets inserted into commit message when PR is merged. | |
def cmd = "git --git-dir=$envVarsMap.WORKSPACE/.git log --format=%b -1 $envVarsMap.GIT_COMMIT"; | |
// Execute the shell command. | |
def sout = new StringBuilder(), serr = new StringBuilder() | |
def proc = cmd.execute() | |
proc.consumeProcessOutput(sout, serr) | |
proc.waitForOrKill(1000) | |
// Inject the new environment variable. | |
return ['ghprbPullTitle': sout] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment