2

I'm using the Scriptler plugin for Jenkins, and am having a hard time finding any information on how to share the scriptler scripts I'm writing between scripts. I've tried using the ScriptHelper from the Scriptler API, but have run into issues when passing in arguments to the script.

Anyone else come across this and solve it? Is there a standard way to do this (without calling the Jenkins REST API) to execute a script?

More Details We have a full build MultiJob that contains many phase jobs, each with their own artifacts, with a 3 day time to live on them. When a this full build job is promoted, a scriptler runs against it, pulling each of the phase jobs artifacts into the full build job. By doing so, we can keep the full build alive forever, without changing the lifetime on the artifacts for each phase job (essentially 'keep this build forever' on the full build, ignoring the lifetimes set in the phase jobs.

We also want to pull these artifacts into a deploy job. The idea is that we can point a deploy job to a full build, and it will pull out the artifacts we specify. If the full build is promoted, this script will pull the artifacts directly from the full build job, otherwise, it will pull them from the internal phase jobs. Since we have 2 scripts that work with MultiJobs, I would like to be able to share this code between them.

The script would take a MultiJob name and build number, and return the individual phase job's build numbers, build statuses, and artifact information.

  • Can you provide more information please? Are you using the plugin to configure steps in jobs and what are you trying to achieve with the scripts? – KeepCalmAndCarryOn Apr 14 '15 at 20:30
  • So in my case, I'm using the MultiJob plugin, and have written some utility scripts (get all phase jobs with their respective build numbers and statuses when given a multijob, aggregate all phase artifacts to the master multijob, etc). What I'd like to do, is reuse this functionality in other scripts. I'd like to be able to use the same code to retrieve the details about a MultiJob's run from other scripts. – SpikedPunchVictim Apr 14 '15 at 21:07

1 Answers1

1

This is possible using Groovy capabilities, though I don't know if Scripler supports it directly. If you are running on the master node, you can use Groovy evaluate. Scriptler scripts are stored as Groovy files on the file system of the master node in the $JENKINS_HOME/scriptler/scripts directory. The Scripter ID is the function name within that directory.

Here is a very simple example. It uses two files. The first is the parameterized function, findByScm.groovy, which finds jobs using a give source control type. The second script, findByGitScm.groovy will evaluate the first function for Git SCMs and print the results.

findByScm.groovy

import jenkins.model.*

jenkins = Jenkins.instance;

// Notice that myScmType is not defined in this function
scmJobs = jenkins.items
    .findAll { job -> job.scm != null && job.scm.type == myScmType }

findByGitScm.groovy

// This is supplying the argument to findByScm.groovy
myScmType = 'hudson.plugins.git.GitSCM'

// Now we are evaluating the script
evaluate(new File("${System.getProperty('JENKINS_HOME')}/scriptler/scripts/findByScm.groovy"))

// scmJobs is a variable which was introduced in findByScm.groovy
scmJobs.each { println it }
David V
  • 11,531
  • 5
  • 42
  • 66