Skip to content
Permalink
Browse files

Merge pull request #1023 from daspilker/JENKINS-42299

[JENKINS-42299] added a switch to the command line runner
  • Loading branch information
daspilker committed May 1, 2017
2 parents f8b5191 + b75d2f0 commit 0df6e080960703df15891164d01ea472c50806fb
Showing with 20 additions and 3 deletions.
  1. +3 −0 docs/Home.md
  2. +6 −0 docs/User-Power-Moves.md
  3. +11 −3 job-dsl-core/src/main/groovy/javaposse/jobdsl/Run.groovy
@@ -37,6 +37,9 @@ Browse the Jenkins issue tracker to see any [open issues](https://issues.jenkins
* Enhanced support for the [Parameterized Remote Trigger
Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Remote+Trigger+Plugin)
([JENKINS-43025](https://issues.jenkins-ci.org/browse/JENKINS-43025))
* Added a switch to the command line runner to put the script's directory on the classpath, see
[User-Power-Moves](User-Power-Moves#run-a-dsl-script-locally)
([JENKINS-42299](https://issues.jenkins-ci.org/browse/JENKINS-42299))
* Support for the older versions of the [Parameterized Remote Trigger
Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Remote+Trigger+Plugin) is deprecated, see
[Migration](Migration#migrating-to-162)
@@ -13,6 +13,12 @@ If you already have the source code checked out then you can ignore step 1.
What's going on here is that there's a static main method that can run the DSL, you just have to give it a filename. It'll output all the jobs' XML to the current directory. Likewise, if you use "using" (the templates-like feature) it'll look in the current directory for a file with the name of the job appended with ".xml" at the end of it.
By default the current directory is added to the classpath to be able to import classes. When using sub-directories for
scripts, the classpath differs compared to running in Jenkins where the DSL script's directory is added to the
classpath. Add the `-j` command line option to use the same behavior as when running in Jenkins:
java -jar $DSL_JAR -j sample.dsl.groovy
# Generate a Job config.xml without having to fire up Jenkins
1. Add some job dsl content to a file, say job.dsl
1. Run the gradle command: ./gradlew run -Pargs=job.dsl
@@ -17,7 +17,14 @@ class Run {

@SuppressWarnings('NoDef')
static void main(String[] args) throws Exception {
if (args.length == 0) {
boolean scriptClasspath
String[] files = args
if (files.length > 0 && files[0] == '-j') {
files = files[1..-1]
scriptClasspath = true
}

if (files.length == 0) {
LOG.severe('Script name is required')
return
}
@@ -31,10 +38,11 @@ class Run {
jm.parameters.put(key.toString(), value.toString())
}

args.each { String scriptName ->
files.each { String scriptName ->
File scriptFile = new File(scriptName)
String scriptBody = scriptFile.getText('UTF-8')
ScriptRequest request = new ScriptRequest(scriptBody, cwdURL, false, scriptFile.absolutePath)
URL urlRoot = scriptClasspath ? scriptFile.absoluteFile.parentFile.toURI().toURL() : cwdURL
ScriptRequest request = new ScriptRequest(scriptBody, urlRoot, false, scriptFile.absolutePath)
GeneratedItems generatedItems = new DslScriptLoader(jm).runScripts([request])

for (GeneratedJob job : generatedItems.jobs) {

0 comments on commit 0df6e08

Please sign in to comment.
You can’t perform that action at this time.