Skip to content

Instantly share code, notes, and snippets.

@mkutz
Created April 20, 2018 18:02
Show Gist options
  • Save mkutz/2b3960063d323c77afbbf1cdf497278c to your computer and use it in GitHub Desktop.
Save mkutz/2b3960063d323c77afbbf1cdf497278c to your computer and use it in GitHub Desktop.
Simple Groovy script to open the Jenkins job right from a code repository, assuming that the job has the same name as the repository.
#!/usr/bin/env groovy
final String JENKINS_URL = "<YOUR JEKINS URL>" // TODO add your Jenkins URL
final String JOB_NAME = new File(".").canonicalFile.name}
/*
* define CLI
*/
def cli = new CliBuilder(usage: "open-jenkins [-b <build-number> [-c OR -t]]")
cli.b(longOpt: "build", args: 1, argName: "build",
"Build number or to open, \"last\", \"successful\", \"failed\", \"unstable\", \"unsuccessful\" or \"stable\" for last build with that result.")
cli.c(longOpt: "console", "Go directly to console output.")
cli.t(longOpt: "test-report", "Go directly to test report.")
cli.h(longOpt: "help", "Print this message.")
/*
* parse arguments
*/
OptionAccessor options = cli.parse(args)
if (options.h) {
cli.usage()
System.exit(0)
}
if (options.t && options.c) {
cli.usage()
System.exit(1)
}
String build = null
if (options.b) {
if ("last" == options.b) {
build = "lastBuild"
} else {
build = options.b ? "last${options.b.capitalize()}Build" : ""
}
}
final Boolean console = options.c
final Boolean testReport = options.t
final String url = "${JENKINS_URL}/job/${JOB_NAME}/" +
"${build ? "${build}/" : ""}" +
"${console ? "consoleFull" : ""}" +
"${testReport ? "testReport/" : ""}"
/*
* open URL
*/
String command = [
"windows": "start",
"linux": "xdg-open",
"darwin": "open"
].find { key, value -> System.properties["os.name"].toLowerCase().contains(key) }.value
Process process = "${command} ${url}".execute()
process.consumeProcessOutput(System.out, System.err)
process.waitFor()
System.exit(process.exitValue())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment