diff --git a/jenkins_jobs/modules/builders.py b/jenkins_jobs/modules/builders.py index c7f61a95b..58d798e14 100644 --- a/jenkins_jobs/modules/builders.py +++ b/jenkins_jobs/modules/builders.py @@ -1249,13 +1249,15 @@ def system_groovy(registry, xml_parent, data): :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 + :arg str command: Groovy command to run. (Alternative: you can choose a script file instead) + :arg bool sandbox: Execute script inside of groovy sandbox (>=2.0) + (default false) :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) + :arg (list str) class-path: List of script class paths. + (optional) Examples: @@ -1264,12 +1266,25 @@ def system_groovy(registry, xml_parent, data): .. 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)) - - mapping = [("bindings", "bindings", ""), ("class-path", "classpath", "")] + if "file" in data: + scriptSource = _groovy_common_scriptSource(data) + if "command" in data: + scriptSource = XML.Element("source") + scriptSource.set("class", "hudson.plugins.groovy.StringSystemScriptSource") + script = XML.SubElement(scriptSource, "script") + mapping = [("command", "script", None), ("sandbox", "sandbox", False)] + helpers.convert_mapping_to_xml(script, data, mapping, fail_required=True) + classpath = XML.SubElement(script, "classpath") + classpaths = data.get("class-path", []) + if isinstance(classpaths, str): + classpaths = [classpaths] + for path in classpaths: + entry = XML.SubElement(classpath, "entry") + XML.SubElement(entry, "url").text = path + sysgroovy.append(scriptSource) + mapping = [("bindings", "bindings", "")] helpers.convert_mapping_to_xml(sysgroovy, data, mapping, fail_required=True) diff --git a/tests/builders/fixtures/system-groovy001.xml b/tests/builders/fixtures/system-groovy001.xml index 23bde23ba..4ea1ffcd2 100644 --- a/tests/builders/fixtures/system-groovy001.xml +++ b/tests/builders/fixtures/system-groovy001.xml @@ -6,7 +6,6 @@ test.groovy - diff --git a/tests/builders/fixtures/system-groovy002.xml b/tests/builders/fixtures/system-groovy002.xml index d1e89fa35..c181cde30 100644 --- a/tests/builders/fixtures/system-groovy002.xml +++ b/tests/builders/fixtures/system-groovy002.xml @@ -2,11 +2,18 @@ - - Some command - - Some bindings - Some classpath + + + true + + + file:/home/user/example.jar + + + + + EXAMPLE=foo-bar diff --git a/tests/builders/fixtures/system-groovy002.yaml b/tests/builders/fixtures/system-groovy002.yaml index 969e429e9..0626b3d58 100644 --- a/tests/builders/fixtures/system-groovy002.yaml +++ b/tests/builders/fixtures/system-groovy002.yaml @@ -1,5 +1,6 @@ builders: - system-groovy: - command: "Some command" - bindings: "Some bindings" - class-path: "Some classpath" + command: "println 'Hello'" + bindings: "EXAMPLE=foo-bar" + class-path: "file:/home/user/example.jar" + sandbox: true