Jenkins Groovy Postbuild uses a static file instead of a script

Is it possible to load an external groovy script into the post w build groovy plugin instead of pasting the contents of the script into each job? We have about 200 tasks, so updating them is rather laborious. I know that I could write a script to directly update the configuration files (as in this post: Add Jenkins groovy Postbuild step for all jobs ), but these jobs work 24x7, so finding a window when I can restart Jenkins or reload the configuration is problematic .

Thank!

+3
source share
8 answers

: , : https://issues.jenkins-ci.org/browse/JENKINS-21480

" , : script " Groovy script ":"

// Delegate to an external script
// The filename must match the class name
import JenkinsPostBuild
def postBuild = new JenkinsPostBuild(manager)
postBuild.run()

" " groovy classpath " .

+3

"Groovy script:":

( ( "... groovy script ..." ));

, . , script? Template plugin, "", groovy script ( ) , , post-build " ", .

+2

.

c:\somepath\MyScriptLibClass.groovy ( Jenkins), groovy MyScriptLibClass. , , ( ).

, sytem groovy postbuild groovy:

[ // include lib scripts
    'MyScriptLibClass'
].each{ this.metaClass.mixin(new GroovyScriptEngine('c:\\somepath').loadScriptByName(it+'.groovy')) }

, script. script, .

, script. , :

class MyScriptLibClass {
    def setBuildName( String str ){
        binding?.variables['manager'].build.displayName = str
    }
}

groovy Postbuild :

[ // include lib scripts
    'MyScriptLibClass'
].each{ this.metaClass.mixin(new GroovyScriptEngine('c:\\somepath').loadScriptByName(it+'.groovy')) }

setBuildName( 'My Greatest Build' )

.

groovy, . , groovy Java ?

+1

:

$JENKINS_HOME/scripts/PostbuildActions.groovy :

public class PostbuildActions {
  void setBuildName(Object manager, String str ){
    binding?.variables['manager'].build.displayName = str
  }
}

Groovy Postbuild :

File script = new File("${manager.envVars['JENKINS_HOME']}/scripts/PostbuildActions.groovy")
Object actions = new GroovyClassLoader(getClass().getClassLoader()).parseClass(script).newInstance();

actions.setBuildName(manager, 'My Greatest Build');
+1

Groovy script Build/Test Slave, , Groovy Postbuild .

- Unix-, / - Windows . script FilePath .

:

// Get an Instance of the Build object, and from there
// the channel from the Master to the Workspace
build = Thread.currentThread().executable
channel = build.workspace.channel;

// Open a FilePath to the script
fp = new FilePath(channel, build.workspace.toString() + "<relative path to the script in Unix notation>")

// Some have suggested that the "Not NULL" check is redundant
// I've kept it for completeness
if(fp != null)
{
    // 'Evaluate' requires a string, so read the file contents to a String
    script = fp.readToString();
    // Execute the script
    evaluate(script);
} 
+1

.

Jenkins Job DSL plugin, jenkins DSL Groovy post build Groovy wiki

Groovy Postbuild

Groovy .

groovyPostBuild (String script, = Behavior.DoNothing) :

script Groovy script . , . . scriptfail, , . , : DoNothing, MarkUnstable MarkFailed.

:

Groovy script, , , :

groovyPostBuild('println "hello, world"') This example will run a 
        groovy script, and if that fails will mark the build as failed:

groovyPostBuild('// some groovy script', Behavior.MarkFailed) This example 
        will run a groovy script, and if that fails will mark the

:

groovyPostBuild('// some groovy script', Behavior.MarkUnstable) (Since 1.19)

( , ), , -. , .

, script , . .

0

( jira).

postbuild

this.class.classLoader.parseClass("/home/jenkins/GitlabPostbuildReporter.groovy")
GitlabPostbuildReporter.newInstance(manager).report()

/home/jenkins/GitlabPostbuildReporter.groovy

class GitlabPostbuildReporter {
  def manager
  public GitlabPostbuildReporter(manager){
    if(manager == null) {
      throw new RuntimeException("Manager object musn't be null")
    }
    this.manager = manager
  }
  public def report() {
    // do work with manager object
  }
}
0

I ran into the same problem and tried using the @Blaskovicz approach. Unfortunately, this did not work for me, but I found the updated code here (Zach Auclair)

Publication here with minor changes:

task after creation

//imports
import hudson.model.*
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import java.io.File;

// define git file
def postBuildFile = manager.build.getEnvVars()["WORKSPACE"] + "/Jenkins/SimpleTaskPostBuildReporter.GROOVY"
def file = new File(postBuildFile)
// load custom class from file
Class groovy = this.class.classLoader.parseClass(file);
// create custom object
GroovyObject groovyObj = (GroovyObject) groovy.newInstance(manager);
// do report
groovyObj.report();

postbuild class file in git repo (SimpleTaskPostBuildReporter.GROOVY)

class SimpleTaskPostBuildReporter {
  def manager

  public SimpleTaskPostBuildReporter(Object manager){
    if(manager == null) {
      throw new RuntimeException("Manager object can't be null")
    }
    this.manager = manager
  }

  public def report() {
    // do work with manager object
  }
}
0
source

All Articles