Skip to content

Instantly share code, notes, and snippets.

@ahonor
Created September 3, 2015 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ahonor/337dc28b55b57c8a540c to your computer and use it in GitHub Desktop.
Save ahonor/337dc28b55b57c8a540c to your computer and use it in GitHub Desktop.
example rundeck notification plugin that triggers a jenkins job via a curl command
import com.dtolabs.rundeck.plugins.notification.NotificationPlugin;
import groovy.text.SimpleTemplateEngine
/**
* This plugin executes a curl command with some arguments as defined
* by the user in the GUI. It wraps an invocation like shown below
*
* curl --user <your_jenkins_username>:<your_jenkins_API_key> http://<jenkins_job_url>
*
*/
rundeckPlugin(NotificationPlugin){
//plugin title shown in GUI
title="Trigger a Jenkins Job"
//plugin description shown in GUI
description="Invokes the Jenkins job URL."
//define configuration options for the plugin
configuration{
jenkinsUser title:"Jenkins User", required:true
jenkinsApiKey title:"Jenkins API Key", required:true
jenkinsUrl title:"Jenkins Job URL (eg, http://jenkins/job/myjob/build)", required:true
}
/**
* common closure used for all the notification triggers
*/
def handleTrigger = {
//with no closure args, there is a "config" and an "execution" variable in the context
//create a new list starting with the base shell command
def shellCommand=['curl', '-sf', '--user', "${configuration.jenkinsUser}:${configuration.jenkinsApiKey}", configuration.jenkinsUrl]
def command=new ArrayList(shellCommand)
println "INFO: jenkins-notification-curl: triggering job: ${configuration.jenkinsUrl}"
//execute the command as a process and copy the output to the System streams
def proc = command.execute()
proc.waitForProcessOutput(System.out,System.err)
//finally return true if the process exited with 0 exitcode
proc.exitValue()==0
}
//define handlers for the notification events
onsuccess handleTrigger
onfailure handleTrigger
onstart handleTrigger
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment