diff --git a/jenkins_jobs/modules/publishers.py b/jenkins_jobs/modules/publishers.py index 7e6d489d3..00a8c066c 100644 --- a/jenkins_jobs/modules/publishers.py +++ b/jenkins_jobs/modules/publishers.py @@ -1,5 +1,7 @@ # Copyright 2012 Hewlett-Packard Development Company, L.P. # Copyright 2012 Varnish Software AS +# Copyright 2013-2014 Antoine "hashar" Musso +# Copyright 2013-2014 Wikimedia Foundation Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -2381,6 +2383,86 @@ def post_tasks(parser, xml_parent, data): task.get('script', '')) +def postbuildscript(parser, xml_parent, data): + """yaml: postbuildscript + Executes additional builders, script or Groovy after the build is + complete. + + Requires the Jenkins `Post Build Script plugin. + `_ + + :arg list generic-script: Paths to Batch/Shell scripts + :arg list groovy-script: Paths to Groovy scripts + :arg list groovy: Inline Groovy + :arg bool onsuccess: Builders are run if the build succeed + (default False) + :arg bool onfailure: Builders are run if the build fail + (default True) + + The `onsuccess` and `onfailure` options are confusing. If you want + the post build to always run regardless of the build status, you + should set them both to `false`. + + Example: + + .. literalinclude:: /../../tests/publishers/fixtures/\ +postbuildscript001.yaml + + You can also execute :doc:`builders `: + + .. literalinclude:: /../../tests/publishers/fixtures/\ +postbuildscript002.yaml + """ + + pbs_xml = XML.SubElement( + xml_parent, + 'org.jenkinsci.plugins.postbuildscript.PostBuildScript') + + # Shell/Groovy in a file + script_types = { + 'generic': 'GenericScript', + 'groovy': 'GroovyScriptFile', + } + for script_type in script_types.keys(): + if script_type + '-script' not in data: + continue + + scripts_xml = XML.SubElement(pbs_xml, script_type + 'ScriptFileList') + for shell_scripts in [data.get(script_type + '-script', [])]: + for shell_script in shell_scripts: + script_xml = XML.SubElement( + scripts_xml, + 'org.jenkinsci.plugins.postbuildscript.' + + script_types[script_type]) + file_path_xml = XML.SubElement(script_xml, 'filePath') + file_path_xml.text = shell_script + + # Inlined Groovy + if 'groovy' in data: + groovy_inline_xml = XML.SubElement(pbs_xml, 'groovyScriptContentList') + for groovy in data.get('groovy', []): + groovy_xml = XML.SubElement( + groovy_inline_xml, + 'org.jenkinsci.plugins.postbuildscript.GroovyScriptContent' + ) + groovy_content = XML.SubElement(groovy_xml, 'content') + groovy_content.text = groovy + + # Inject builders + if 'builders' in data: + build_steps_xml = XML.SubElement(pbs_xml, 'buildSteps') + for builder in data.get('builders', []): + parser.registry.dispatch('builder', parser, build_steps_xml, + builder) + + # When to run the build? Note the plugin let one specify both options + # although they are antinomic + success_xml = XML.SubElement(pbs_xml, 'scriptOnlyIfSuccess') + success_xml.text = str(data.get('onsuccess', True)).lower() + failure_xml = XML.SubElement(pbs_xml, 'scriptOnlyIfFailure') + failure_xml.text = str(data.get('onfailure', False)).lower() + + def xml_summary(parser, xml_parent, data): """yaml: xml-summary Adds support for the Summary Display Plugin diff --git a/setup.cfg b/setup.cfg index a28083c2b..02c4fb866 100644 --- a/setup.cfg +++ b/setup.cfg @@ -132,6 +132,7 @@ jenkins_jobs.publishers = performance=jenkins_jobs.modules.publishers:performance pipeline=jenkins_jobs.modules.publishers:pipeline plot=jenkins_jobs.modules.publishers:plot + postbuildscript=jenkins_jobs.modules.publishers:postbuildscript post-tasks=jenkins_jobs.modules.publishers:post_tasks robot=jenkins_jobs.modules.publishers:robot ruby-metrics=jenkins_jobs.modules.publishers:ruby_metrics diff --git a/tests/publishers/fixtures/postbuildscript000.xml b/tests/publishers/fixtures/postbuildscript000.xml new file mode 100644 index 000000000..78e988df9 --- /dev/null +++ b/tests/publishers/fixtures/postbuildscript000.xml @@ -0,0 +1,9 @@ + + + + + true + false + + + diff --git a/tests/publishers/fixtures/postbuildscript000.yaml b/tests/publishers/fixtures/postbuildscript000.yaml new file mode 100644 index 000000000..64df8f750 --- /dev/null +++ b/tests/publishers/fixtures/postbuildscript000.yaml @@ -0,0 +1,2 @@ +publishers: + - postbuildscript diff --git a/tests/publishers/fixtures/postbuildscript001.xml b/tests/publishers/fixtures/postbuildscript001.xml new file mode 100644 index 000000000..f6e4f4b5c --- /dev/null +++ b/tests/publishers/fixtures/postbuildscript001.xml @@ -0,0 +1,33 @@ + + + + + + + /tmp/one.sh + + + /tmp/two.sh + + + + + /tmp/one.groovy + + + /tmp/two.groovy + + + + + /** This is some inlined groovy */ + + + /** Some more inlined groovy */ + + + false + true + + + diff --git a/tests/publishers/fixtures/postbuildscript001.yaml b/tests/publishers/fixtures/postbuildscript001.yaml new file mode 100644 index 000000000..fd3af4bf3 --- /dev/null +++ b/tests/publishers/fixtures/postbuildscript001.yaml @@ -0,0 +1,13 @@ +publishers: + - postbuildscript: + generic-script: + - '/tmp/one.sh' + - '/tmp/two.sh' + groovy-script: + - '/tmp/one.groovy' + - '/tmp/two.groovy' + groovy: + - "/** This is some inlined groovy */" + - "/** Some more inlined groovy */" + onsuccess: False + onfailure: True diff --git a/tests/publishers/fixtures/postbuildscript002.xml b/tests/publishers/fixtures/postbuildscript002.xml new file mode 100644 index 000000000..84a68d954 --- /dev/null +++ b/tests/publishers/fixtures/postbuildscript002.xml @@ -0,0 +1,18 @@ + + + + + + + echo "Shell execution" + + + ant_target + default + + + true + false + + + diff --git a/tests/publishers/fixtures/postbuildscript002.yaml b/tests/publishers/fixtures/postbuildscript002.yaml new file mode 100644 index 000000000..447770ac8 --- /dev/null +++ b/tests/publishers/fixtures/postbuildscript002.yaml @@ -0,0 +1,5 @@ +publishers: + - postbuildscript: + builders: + - shell: 'echo "Shell execution"' + - ant: 'ant_target'