Support the Post Build Script plugin

Lets one execute a shell/groovy script or any builder AFTER the build
has complete.

https://wiki.jenkins-ci.org/display/JENKINS/PostBuildScript+Plugin

All existing builders should be supported since this patch call the
usual dispatcher.

Change-Id: Ide059ec9fd0e556f0c50ec992a6021eb5afb261c
This commit is contained in:
Antoine Musso 2014-03-03 16:02:42 +01:00
parent d2ff8c098f
commit c88f827686
8 changed files with 163 additions and 0 deletions

View File

@ -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.
@ -2365,6 +2367,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.
<https://wiki.jenkins-ci.org/display/JENKINS/PostBuildScript+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 </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

View File

@ -124,6 +124,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

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<publishers>
<org.jenkinsci.plugins.postbuildscript.PostBuildScript>
<scriptOnlyIfSuccess>true</scriptOnlyIfSuccess>
<scriptOnlyIfFailure>false</scriptOnlyIfFailure>
</org.jenkinsci.plugins.postbuildscript.PostBuildScript>
</publishers>
</project>

View File

@ -0,0 +1,2 @@
publishers:
- postbuildscript

View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<publishers>
<org.jenkinsci.plugins.postbuildscript.PostBuildScript>
<genericScriptFileList>
<org.jenkinsci.plugins.postbuildscript.GenericScript>
<filePath>/tmp/one.sh</filePath>
</org.jenkinsci.plugins.postbuildscript.GenericScript>
<org.jenkinsci.plugins.postbuildscript.GenericScript>
<filePath>/tmp/two.sh</filePath>
</org.jenkinsci.plugins.postbuildscript.GenericScript>
</genericScriptFileList>
<groovyScriptFileList>
<org.jenkinsci.plugins.postbuildscript.GroovyScriptFile>
<filePath>/tmp/one.groovy</filePath>
</org.jenkinsci.plugins.postbuildscript.GroovyScriptFile>
<org.jenkinsci.plugins.postbuildscript.GroovyScriptFile>
<filePath>/tmp/two.groovy</filePath>
</org.jenkinsci.plugins.postbuildscript.GroovyScriptFile>
</groovyScriptFileList>
<groovyScriptContentList>
<org.jenkinsci.plugins.postbuildscript.GroovyScriptContent>
<content>/** This is some inlined groovy */</content>
</org.jenkinsci.plugins.postbuildscript.GroovyScriptContent>
<org.jenkinsci.plugins.postbuildscript.GroovyScriptContent>
<content>/** Some more inlined groovy */</content>
</org.jenkinsci.plugins.postbuildscript.GroovyScriptContent>
</groovyScriptContentList>
<scriptOnlyIfSuccess>false</scriptOnlyIfSuccess>
<scriptOnlyIfFailure>true</scriptOnlyIfFailure>
</org.jenkinsci.plugins.postbuildscript.PostBuildScript>
</publishers>
</project>

View File

@ -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

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<publishers>
<org.jenkinsci.plugins.postbuildscript.PostBuildScript>
<buildSteps>
<hudson.tasks.Shell>
<command>echo &quot;Shell execution&quot;</command>
</hudson.tasks.Shell>
<hudson.tasks.Ant>
<targets>ant_target</targets>
<antName>default</antName>
</hudson.tasks.Ant>
</buildSteps>
<scriptOnlyIfSuccess>true</scriptOnlyIfSuccess>
<scriptOnlyIfFailure>false</scriptOnlyIfFailure>
</org.jenkinsci.plugins.postbuildscript.PostBuildScript>
</publishers>
</project>

View File

@ -0,0 +1,5 @@
publishers:
- postbuildscript:
builders:
- shell: 'echo "Shell execution"'
- ant: 'ant_target'