Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save virtualtam/5e17c8866ec876b370d0974cbeff17dc to your computer and use it in GitHub Desktop.
Save virtualtam/5e17c8866ec876b370d0974cbeff17dc to your computer and use it in GitHub Desktop.
Jenkins Groovy script to add Groovy postbuild script to many projects
import hudson.model.FreeStyleProject
import hudson.model.Hudson
import hudson.tasks.Publisher
import org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildRecorder
import org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript;
import org.jenkinsci.plugins.scriptsecurity.scripts.ClasspathEntry;
// Exclusion list for job names. Job names listed here will NOT have the groovy postbuild entry added.
def exclusion_list = ["some_job_that_doesnt_need_groovy_postbuild"]
// Excluded list for jobs in a particular view. Handy if you don't want to manage exclusion list
// in the script.
def excluded_views = ["tests"]
// If you have JARs or classes to add to the class path in Jenkins, change this path to point to them.
def jar_location = "/var/lib/jenkins/scripts/jars/"
// This is the postbuild script that will be added to all scripts. Note that it needs to reside
// on the Jenkins Master.
groovyScriptPath = "/var/lib/jenkins/scripts/create_jira_issue.groovy"
// Classes or JARs you want to include.
classPaths = [
"$jar_location/http-builder-0.7.1.jar",
"$jar_location/httpclient-4.3.6.jar",
"$jar_location/httpcore-4.3.3.jar",
"$jar_location/xml-resolver-1.2.jar"
]
// Set this to true when updating the script. It will override the existing
// script in Jenkins.
force_update = true
report = false
// This will do all jobs minus exclusions. Change this to another view name
// if you wish to be specific.
str_view = "All"
hudson_inst = Hudson.instance
newScript = createScript()
def view = hudson_inst.getView( str_view )
excluded_views.each { view_name ->
def excluded_view = hudson_inst.getView( view_name )
excluded_view.items.each { project ->
exclusion_list << project.displayName
}
}
def createScript()
{
String loaderScript = new File( groovyScriptPath ).text
List<ClasspathEntry> classPathEntries = new ArrayList<ClasspathEntry>()
for( String path in classPaths )
{
ClasspathEntry classpath = new ClasspathEntry( path )
classPathEntries.add( classpath )
}
SecureGroovyScript script = new SecureGroovyScript( loaderScript, false, classPathEntries )
// String script, behaviour (0 is don't fail build if a script fails), and false for Matrix builds
GroovyPostbuildRecorder newGroovyPostBuild = new GroovyPostbuildRecorder(script, 0, false)
return newGroovyPostBuild
}
for( item in view.getItems() )
{
if ( ! exclusion_list.contains( item.displayName ) && hudson_inst.getJob( item.displayName ).getClass() == FreeStyleProject )
{
println( "Project Name: " + item.displayName )
FreeStyleProject project = hudson_inst.getJob( item.displayName )
def update_idx = -1
def exists = false
List<Publisher> publishers = project.getPublishersList()
def counter = 0
def pubScript
for( Publisher pub in publishers )
{
println pub.class.name
if( pub instanceof GroovyPostbuildRecorder)
{
if( pub.script.script =~ /^\/\/jira_integration_script/ )
{
pubScript = pub.script.script
exists = true
if ( force_update )
{
update_idx = counter
}
break
}
}
counter += 1
}
if ( report )
{
if ( pubScript ) {
if ( newScript.script.script == pubScript )
{
println( item.displayName + " : OK" )
}
else
{
println( item.displayName + " : DIFFERENT" )
}
}
else
{
println( item.displayName + " : MISSING" )
}
}
else
{
println( "Update index for " + item.displayName + update_idx.toString() )
if( ! exists )
{
publishers.add( createScript() )
}
else if ( update_idx > -1 )
{
println("Replacing publisher")
publishers.replace( publishers.get(update_idx), newScript )
}
project.save()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment