diff --git a/jenkins_jobs/modules/builders.py b/jenkins_jobs/modules/builders.py index e7b3a8b57..dd58eddf3 100644 --- a/jenkins_jobs/modules/builders.py +++ b/jenkins_jobs/modules/builders.py @@ -494,6 +494,104 @@ def gradle(parser, xml_parent, data): 'use-root-dir', False)).lower() +def _groovy_common_scriptSource(data): + """Helper function to generate the XML element common to groovy builders + """ + + scriptSource = XML.Element("scriptSource") + if 'command' in data and 'file' in data: + raise JenkinsJobsException("Use just one of 'command' or 'file'") + + if 'command' in data: + command = XML.SubElement(scriptSource, 'command') + command.text = str(data['command']) + scriptSource.set('class', 'hudson.plugins.groovy.StringScriptSource') + elif 'file' in data: + scriptFile = XML.SubElement(scriptSource, 'scriptFile') + scriptFile.text = str(data['file']) + scriptSource.set('class', 'hudson.plugins.groovy.FileScriptSource') + else: + raise JenkinsJobsException("A groovy command or file is required") + + return scriptSource + + +def groovy(parser, xml_parent, data): + """yaml: groovy + Execute a groovy script or command. + Requires the Jenkins `Groovy Plugin + `_ + + :arg str file: Groovy file to run. + (Alternative: you can chose a command instead) + :arg str command: Groovy command to run. + (Alternative: you can chose a script file instead) + :arg str version: Groovy version to use. (default '(Default)') + :arg str parameters: Parameters for the Groovy executable. (optional) + :arg str script-parameters: These parameters will be passed to the script. + (optional) + :arg str properties: Instead of passing properties using the -D parameter + you can define them here. (optional) + :arg str java-opts: Direct access to JAVA_OPTS. Properties allows only + -D properties, while sometimes also other properties like -XX need to + be setup. It can be done here. This line is appended at the end of + JAVA_OPTS string. (optional) + :arg str class-path: Specify script classpath here. Each line is one + class path item. (optional) + + Examples: + + .. literalinclude:: ../../tests/builders/fixtures/groovy001.yaml + :language: yaml + .. literalinclude:: ../../tests/builders/fixtures/groovy002.yaml + :language: yaml + """ + + root_tag = 'hudson.plugins.groovy.Groovy' + groovy = XML.SubElement(xml_parent, root_tag) + + groovy.append(_groovy_common_scriptSource(data)) + XML.SubElement(groovy, 'groovyName').text = \ + str(data.get('version', "(Default)")) + XML.SubElement(groovy, 'parameters').text = str(data.get('parameters', "")) + XML.SubElement(groovy, 'scriptParameters').text = \ + str(data.get('script-parameters', "")) + XML.SubElement(groovy, 'properties').text = str(data.get('properties', "")) + XML.SubElement(groovy, 'javaOpts').text = str(data.get('java-opts', "")) + XML.SubElement(groovy, 'classPath').text = str(data.get('class-path', "")) + + +def system_groovy(parser, xml_parent, data): + """yaml: system-groovy + Execute a system groovy script or command. + Requires the Jenkins `Groovy Plugin + `_ + + :arg str file: Groovy file to run. + (Alternative: you can chose a command instead) + :arg str command: Groovy command to run. + (Alternative: you can chose a script file instead) + :arg str bindings: Define variable bindings (in the properties file + format). Specified variables can be addressed from the script. (optional) + :arg str class-path: Specify script classpath here. Each line is one class + path item. (optional) + + Examples: + + .. literalinclude:: ../../tests/builders/fixtures/system-groovy001.yaml + :language: yaml + .. literalinclude:: ../../tests/builders/fixtures/system-groovy002.yaml + :language: yaml + """ + + root_tag = 'hudson.plugins.groovy.SystemGroovy' + sysgroovy = XML.SubElement(xml_parent, root_tag) + sysgroovy.append(_groovy_common_scriptSource(data)) + XML.SubElement(sysgroovy, 'bindings').text = str(data.get('bindings', "")) + XML.SubElement(sysgroovy, 'classpath').text = \ + str(data.get('class-path', "")) + + def batch(parser, xml_parent, data): """yaml: batch Execute a batch command. diff --git a/setup.cfg b/setup.cfg index 6bda4b43b..ad44d6f9f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -50,6 +50,7 @@ jenkins_jobs.builders = critical-block-end=jenkins_jobs.modules.builders:critical_block_end gradle=jenkins_jobs.modules.builders:gradle grails=jenkins_jobs.modules.builders:grails + groovy=jenkins_jobs.modules.builders:groovy inject=jenkins_jobs.modules.builders:inject maven-target=jenkins_jobs.modules.builders:maven_target msbuild=jenkins_jobs.modules.builders:msbuild @@ -58,6 +59,7 @@ jenkins_jobs.builders = sbt=jenkins_jobs.modules.builders:sbt shell=jenkins_jobs.modules.builders:shell shining-panda=jenkins_jobs.modules.builders:shining_panda + system-groovy=jenkins_jobs.modules.builders:system_groovy trigger-builds=jenkins_jobs.modules.builders:trigger_builds jenkins_jobs.reporters = email=jenkins_jobs.modules.reporters:email diff --git a/tests/builders/fixtures/groovy001.xml b/tests/builders/fixtures/groovy001.xml new file mode 100644 index 000000000..6682fa3e2 --- /dev/null +++ b/tests/builders/fixtures/groovy001.xml @@ -0,0 +1,16 @@ + + + + + + test.groovy + + (Default) + + + + + + + + diff --git a/tests/builders/fixtures/groovy001.yaml b/tests/builders/fixtures/groovy001.yaml new file mode 100644 index 000000000..e12716e26 --- /dev/null +++ b/tests/builders/fixtures/groovy001.yaml @@ -0,0 +1,3 @@ +builders: + - groovy: + file: "test.groovy" diff --git a/tests/builders/fixtures/groovy002.xml b/tests/builders/fixtures/groovy002.xml new file mode 100644 index 000000000..dae670e29 --- /dev/null +++ b/tests/builders/fixtures/groovy002.xml @@ -0,0 +1,16 @@ + + + + + + Some command + + Groovy 1.2 + parameters + script parameters + properties + java opts + + + + diff --git a/tests/builders/fixtures/groovy002.yaml b/tests/builders/fixtures/groovy002.yaml new file mode 100644 index 000000000..30c7be247 --- /dev/null +++ b/tests/builders/fixtures/groovy002.yaml @@ -0,0 +1,8 @@ +builders: + - groovy: + command: "Some command" + version: "Groovy 1.2" + parameters: "parameters" + script-parameters: "script parameters" + properties: "properties" + java-opts: "java opts" diff --git a/tests/builders/fixtures/system-groovy001.xml b/tests/builders/fixtures/system-groovy001.xml new file mode 100644 index 000000000..23bde23ba --- /dev/null +++ b/tests/builders/fixtures/system-groovy001.xml @@ -0,0 +1,12 @@ + + + + + + test.groovy + + + + + + diff --git a/tests/builders/fixtures/system-groovy001.yaml b/tests/builders/fixtures/system-groovy001.yaml new file mode 100644 index 000000000..553f69bde --- /dev/null +++ b/tests/builders/fixtures/system-groovy001.yaml @@ -0,0 +1,3 @@ +builders: + - system-groovy: + file: "test.groovy" diff --git a/tests/builders/fixtures/system-groovy002.xml b/tests/builders/fixtures/system-groovy002.xml new file mode 100644 index 000000000..d1e89fa35 --- /dev/null +++ b/tests/builders/fixtures/system-groovy002.xml @@ -0,0 +1,12 @@ + + + + + + Some command + + Some bindings + Some classpath + + + diff --git a/tests/builders/fixtures/system-groovy002.yaml b/tests/builders/fixtures/system-groovy002.yaml new file mode 100644 index 000000000..969e429e9 --- /dev/null +++ b/tests/builders/fixtures/system-groovy002.yaml @@ -0,0 +1,5 @@ +builders: + - system-groovy: + command: "Some command" + bindings: "Some bindings" + class-path: "Some classpath"