Add support for PostBuildScript 2.x

Updates the PostBuildScript plugin to support the 2.x version which
had a major overhaul of the XML output for the plugin.

Change-Id: I9ff3161bc50be236013e32f3ca9ad81b38004dc8
Signed-off-by: Thanh Ha <thanh.ha@linuxfoundation.org>
This commit is contained in:
Thanh Ha 2017-12-28 16:23:57 -05:00
parent 9663d32c60
commit 5579395bc3
No known key found for this signature in database
GPG Key ID: B0CB27E00DA095AA
17 changed files with 418 additions and 86 deletions

View File

@ -3572,30 +3572,81 @@ def postbuildscript(registry, xml_parent, data):
Requires the Jenkins :jenkins-wiki:`Post Build Script plugin
<PostBuildScript+Plugin>`.
:arg list generic-script: Paths to Batch/Shell scripts
:arg list generic-script: Series of Batch/Shell scripts to to run
:generic-script: * **file-path** (`str`) - Path to Batch/Shell scripts
* **role** (`str`) - Execute scripts on. One of
MASTER / SLAVE / BOTH. (default 'BOTH')
* **build-on** (`list`) - Build statuses which trigger
the scripts. Valid options:
SUCCESS / UNSTABLE / FAILURE / NOT_BUILT / ABORTED
(default 'SUCCESS')
:arg list groovy-script: Paths to Groovy scripts
:groovy-script: * **file-path** (`str`) - Path to Groovy scripts
* **role** (`str`) - Execute scripts on. One of
MASTER / SLAVE / BOTH. (default 'BOTH')
* **build-on** (`list`) - Build statuses which trigger
the scripts. Valid options:
SUCCESS / UNSTABLE / FAILURE / NOT_BUILT / ABORTED
(default 'SUCCESS')
:arg list groovy: Inline Groovy
:arg list builders: Any supported builders, see :doc:`builders`.
:groovy: * **content** (`str`) - Inline Groovy script.
* **role** (`str`) - Execute scripts on. One of
MASTER / SLAVE / BOTH. (default 'BOTH')
* **build-on** (`list`) - Build statuses which trigger
the scripts. Valid options:
SUCCESS / UNSTABLE / FAILURE / NOT_BUILT / ABORTED
(default 'SUCCESS')
:arg list builders: Execute any number of supported Jenkins builders.
:builders: * **build-steps** (`str`) - Any supported builders,
see :doc:`builders`.
* **role** (`str`) - Execute scripts on. One of
MASTER / SLAVE / BOTH. (default 'BOTH')
* **build-on** (`list`) - Build statuses which trigger
the scripts. Valid options:
SUCCESS / UNSTABLE / FAILURE / NOT_BUILT / ABORTED
(default 'SUCCESS')
:arg bool mark-unstable-if-failed: Build will be marked unstable
if job will be successfully completed but publishing script will return
non zero exit code (default false)
Deprecated Options for versions < 2.0 of plugin:
:arg bool onsuccess: Deprecated, replaced with script-only-if-succeeded
:arg bool script-only-if-succeeded: Scripts and builders are run only if
the build succeeded (default true)
the build succeeded (default true)
:arg bool onfailure: Deprecated, replaced with script-only-if-failed
:arg bool script-only-if-failed: Scripts and builders are run only if the
build failed (default false)
:arg bool mark-unstable-if-failed: Build will be marked unstable
if job will be successfully completed
but publishing script will return
non zero exit code (default false)
build failed (default false)
:arg str execute-on: For matrix projects, scripts can be run after each
axis is built (`axes`), after all axis of the matrix
are built (`matrix`) or after each axis AND the matrix
are built (`both`).
axis is built (`axes`), after all axis of the matrix are built
(`matrix`) or after each axis AND the matrix are built (`both`).
The `script-only-if-succeeded` and `bool script-only-if-failed` 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:
Minimal Example:
.. literalinclude::
/../../tests/publishers/fixtures/postbuildscript-minimal.yaml
:language: yaml
Full Example:
.. literalinclude::
/../../tests/publishers/fixtures/postbuildscript-full.yaml
:language: yaml
Example(s) versions < 2.0:
.. literalinclude::
/../../tests/publishers/fixtures/postbuildscript001.yaml
@ -3618,82 +3669,172 @@ def postbuildscript(registry, xml_parent, data):
xml_parent,
'org.jenkinsci.plugins.postbuildscript.PostBuildScript')
# Shell/Groovy in a file
script_types = {
'generic-script': 'GenericScript',
'groovy-script': 'GroovyScriptFile',
}
info = registry.get_plugin_info('Jenkins PostBuildScript Plugin')
# Note: Assume latest version of plugin is preferred config format
version = pkg_resources.parse_version(
info.get('version', str(sys.maxsize)))
if version >= pkg_resources.parse_version('2.0'):
pbs_xml = XML.SubElement(pbs_xml, 'config')
# Assuming yaml preserves order of input data make sure
# corresponding XML steps are generated in the same order
build_scripts = [(k, v) for k, v in data.items()
if k in script_types or k in ['groovy', 'builders']]
mapping = [
('mark-unstable-if-failed', 'markBuildUnstable', False),
]
helpers.convert_mapping_to_xml(pbs_xml, data, mapping, fail_required=True)
for step, script_data in build_scripts:
if step in script_types:
scripts_xml = XML.SubElement(pbs_xml, step[:-len('-script')] +
'ScriptFileList')
for shell_script in script_data:
script_xml = XML.SubElement(
scripts_xml,
'org.jenkinsci.plugins.postbuildscript.'
+ script_types[step])
file_path_xml = XML.SubElement(script_xml, 'filePath')
file_path_xml.text = shell_script
if version >= pkg_resources.parse_version("2.0"):
# Inlined Groovy
if step == 'groovy':
groovy_inline_xml = XML.SubElement(pbs_xml,
'groovyScriptContentList')
for groovy in script_data:
groovy_xml = XML.SubElement(
groovy_inline_xml,
'org.jenkinsci.plugins.postbuildscript.GroovyScriptContent'
)
groovy_content = XML.SubElement(groovy_xml, 'content')
groovy_content.text = groovy
################
# Script Files #
################
# Inject builders
if step == 'builders':
build_steps_xml = XML.SubElement(pbs_xml, 'buildSteps')
for builder in script_data:
script_mapping = [
('role', 'role', 'BOTH'),
('file-path', 'filePath', False),
]
sf_path = 'org.jenkinsci.plugins.postbuildscript.model.ScriptFile'
sf_xml = XML.SubElement(pbs_xml, 'scriptFiles')
for gs_data in data.get('generic-script', []):
x = XML.SubElement(sf_xml, sf_path)
results_xml = XML.SubElement(x, 'results')
for result in gs_data.get('build-on', ['SUCCESS']):
XML.SubElement(results_xml, 'string').text = result
helpers.convert_mapping_to_xml(
x, gs_data, script_mapping, fail_required=True)
XML.SubElement(x, 'scriptType').text = 'GENERIC'
for gs_data in data.get('groovy-script', []):
x = XML.SubElement(sf_xml, sf_path)
results_xml = XML.SubElement(x, 'results')
for result in gs_data.get('build-on', ['SUCCESS']):
XML.SubElement(results_xml, 'string').text = result
helpers.convert_mapping_to_xml(
x, gs_data, script_mapping, fail_required=True)
XML.SubElement(x, 'scriptType').text = 'GROOVY'
#################
# Inline Groovy #
#################
groovy_mapping = [
('role', 'role', 'BOTH'),
('content', 'content', False),
]
gs_path = 'org.jenkinsci.plugins.postbuildscript.model.Script'
gs_xml = XML.SubElement(pbs_xml, 'groovyScripts')
for gs_data in data.get('groovy', []):
x = XML.SubElement(gs_xml, gs_path)
results_xml = XML.SubElement(x, 'results')
for result in gs_data.get('build-on', ['SUCCESS']):
XML.SubElement(results_xml, 'string').text = result
helpers.convert_mapping_to_xml(
x, gs_data, groovy_mapping, fail_required=True)
############
# Builders #
############
builder_mapping = [
('role', 'role', 'BOTH'),
]
bs_path = 'org.jenkinsci.plugins.postbuildscript.model.PostBuildStep'
bs_xml = XML.SubElement(pbs_xml, 'buildSteps')
for bs_data in data.get('builders', []):
x = XML.SubElement(bs_xml, bs_path)
results_xml = XML.SubElement(x, 'results')
for result in bs_data.get('build-on', ['SUCCESS']):
XML.SubElement(results_xml, 'string').text = result
helpers.convert_mapping_to_xml(
x, bs_data, builder_mapping, fail_required=True)
build_steps_xml = XML.SubElement(x, 'buildSteps')
for builder in bs_data.get('build-steps'):
registry.dispatch('builder', build_steps_xml, builder)
# When to run the build? Note the plugin let one specify both options
# although they are antinomic
# onsuccess and onfailure parameters are deprecated, this is to keep
# backwards compatability
success_xml = XML.SubElement(pbs_xml, 'scriptOnlyIfSuccess')
if 'script-only-if-succeeded' in data:
success_xml.text = str(data.get('script-only-if-succeeded',
True)).lower()
else:
success_xml.text = str(data.get('onsuccess', True)).lower()
else: # Options below are all deprecated in version < 2.0 of plugin
failure_xml = XML.SubElement(pbs_xml, 'scriptOnlyIfFailure')
if 'script-only-if-failed' in data:
failure_xml.text = str(data.get('script-only-if-failed',
False)).lower()
else:
failure_xml.text = str(data.get('onfailure', False)).lower()
# Shell/Groovy in a file
script_types = {
'generic-script': 'GenericScript',
'groovy-script': 'GroovyScriptFile',
}
# Mark build unstable if publisher script return non zero exit code
XML.SubElement(pbs_xml, 'markBuildUnstable').text = str(
data.get('mark-unstable-if-failed', False)).lower()
# TODO: we may want to avoid setting "execute-on" on non-matrix jobs,
# either by skipping this part or by raising an error to let the user know
# an attempt was made to set execute-on on a non-matrix job. There are
# currently no easy ways to check for this though.
if 'execute-on' in data:
valid_values = ('matrix', 'axes', 'both')
execute_on = data['execute-on'].lower()
if execute_on not in valid_values:
raise JenkinsJobsException(
'execute-on must be one of %s, got %s' %
valid_values, execute_on
)
execute_on_xml = XML.SubElement(pbs_xml, 'executeOn')
execute_on_xml.text = execute_on.upper()
# Assuming yaml preserves order of input data make sure
# corresponding XML steps are generated in the same order
build_scripts = [(k, v) for k, v in data.items()
if k in script_types or k in ['groovy', 'builders']]
for step, script_data in build_scripts:
if step in script_types:
scripts_xml = XML.SubElement(
pbs_xml, step[:-len('-script')] + 'ScriptFileList')
for shell_script in script_data:
script_xml = XML.SubElement(
scripts_xml,
'org.jenkinsci.plugins.postbuildscript.'
+ script_types[step])
file_path_xml = XML.SubElement(script_xml, 'filePath')
file_path_xml.text = shell_script
# Inlined Groovy
if step == 'groovy':
groovy_inline_xml = XML.SubElement(
pbs_xml, 'groovyScriptContentList')
for groovy in script_data:
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 step == 'builders':
build_steps_xml = XML.SubElement(pbs_xml, 'buildSteps')
for builder in script_data:
registry.dispatch('builder', build_steps_xml, builder)
# When to run the build? Note the plugin let one specify both options
# although they are antinomic
# onsuccess and onfailure parameters are deprecated, this is to keep
# backwards compatability
success_xml = XML.SubElement(pbs_xml, 'scriptOnlyIfSuccess')
if 'script-only-if-succeeded' in data:
success_xml.text = str(
data.get('script-only-if-succeeded', True)).lower()
else:
success_xml.text = str(data.get('onsuccess', True)).lower()
failure_xml = XML.SubElement(pbs_xml, 'scriptOnlyIfFailure')
if 'script-only-if-failed' in data:
failure_xml.text = str(
data.get('script-only-if-failed', False)).lower()
else:
failure_xml.text = str(data.get('onfailure', False)).lower()
# TODO: we may want to avoid setting "execute-on" on non-matrix jobs,
# either by skipping this part or by raising an error to let the user
# know an attempt was made to set execute-on on a non-matrix job.
# There are currently no easy ways to check for this though.
if 'execute-on' in data:
valid_values = ('matrix', 'axes', 'both')
execute_on = data['execute-on'].lower()
if execute_on not in valid_values:
raise JenkinsJobsException(
'execute-on must be one of %s, got %s' %
valid_values, execute_on
)
execute_on_xml = XML.SubElement(pbs_xml, 'executeOn')
execute_on_xml.text = execute_on.upper()
def xml_summary(registry, xml_parent, data):

View File

@ -0,0 +1,3 @@
- longName: 'Jenkins PostBuildScript Plugin'
shortName: 'postbuildscript'
version: "1.0"

View File

@ -12,6 +12,7 @@
<builders/>
<publishers>
<org.jenkinsci.plugins.postbuildscript.PostBuildScript>
<markBuildUnstable>false</markBuildUnstable>
<buildSteps>
<hudson.tasks.Shell>
<command>fine</command>
@ -22,7 +23,6 @@
</buildSteps>
<scriptOnlyIfSuccess>true</scriptOnlyIfSuccess>
<scriptOnlyIfFailure>false</scriptOnlyIfFailure>
<markBuildUnstable>false</markBuildUnstable>
</org.jenkinsci.plugins.postbuildscript.PostBuildScript>
</publishers>
<buildWrappers/>

View File

@ -0,0 +1,101 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<publishers>
<org.jenkinsci.plugins.postbuildscript.PostBuildScript>
<config>
<markBuildUnstable>true</markBuildUnstable>
<scriptFiles>
<org.jenkinsci.plugins.postbuildscript.model.ScriptFile>
<results>
<string>SUCCESS</string>
<string>UNSTABLE</string>
</results>
<role>MASTER</role>
<filePath>/fakepath/generic</filePath>
<scriptType>GENERIC</scriptType>
</org.jenkinsci.plugins.postbuildscript.model.ScriptFile>
<org.jenkinsci.plugins.postbuildscript.model.ScriptFile>
<results>
<string>NOT_BUILT</string>
<string>ABORTED</string>
<string>FAILURE</string>
</results>
<role>SLAVE</role>
<filePath>/fakepath/generic-two</filePath>
<scriptType>GENERIC</scriptType>
</org.jenkinsci.plugins.postbuildscript.model.ScriptFile>
<org.jenkinsci.plugins.postbuildscript.model.ScriptFile>
<results>
<string>SUCCESS</string>
<string>UNSTABLE</string>
</results>
<role>MASTER</role>
<filePath>/fakepath/groovy</filePath>
<scriptType>GROOVY</scriptType>
</org.jenkinsci.plugins.postbuildscript.model.ScriptFile>
<org.jenkinsci.plugins.postbuildscript.model.ScriptFile>
<results>
<string>NOT_BUILT</string>
<string>ABORTED</string>
<string>FAILURE</string>
</results>
<role>SLAVE</role>
<filePath>/fakepath/groovy-too</filePath>
<scriptType>GROOVY</scriptType>
</org.jenkinsci.plugins.postbuildscript.model.ScriptFile>
</scriptFiles>
<groovyScripts>
<org.jenkinsci.plugins.postbuildscript.model.Script>
<results>
<string>SUCCESS</string>
<string>UNSTABLE</string>
</results>
<role>MASTER</role>
<content>println &quot;Hello world!&quot;</content>
</org.jenkinsci.plugins.postbuildscript.model.Script>
<org.jenkinsci.plugins.postbuildscript.model.Script>
<results>
<string>NOT_BUILT</string>
<string>ABORTED</string>
<string>FAILURE</string>
</results>
<role>SLAVE</role>
<content>println &quot;Hello world!&quot;
println &quot;Multi-line script&quot;
</content>
</org.jenkinsci.plugins.postbuildscript.model.Script>
</groovyScripts>
<buildSteps>
<org.jenkinsci.plugins.postbuildscript.model.PostBuildStep>
<results>
<string>SUCCESS</string>
<string>UNSTABLE</string>
</results>
<role>MASTER</role>
<buildSteps>
<hudson.tasks.Shell>
<command>echo &quot;Hello world!&quot;</command>
</hudson.tasks.Shell>
</buildSteps>
</org.jenkinsci.plugins.postbuildscript.model.PostBuildStep>
<org.jenkinsci.plugins.postbuildscript.model.PostBuildStep>
<results>
<string>NOT_BUILT</string>
<string>ABORTED</string>
<string>FAILURE</string>
</results>
<role>SLAVE</role>
<buildSteps>
<hudson.tasks.Shell>
<command>echo &quot;Hello world!&quot;</command>
</hudson.tasks.Shell>
<hudson.tasks.Shell>
<command>echo &quot;Goodbye world!&quot;</command>
</hudson.tasks.Shell>
</buildSteps>
</org.jenkinsci.plugins.postbuildscript.model.PostBuildStep>
</buildSteps>
</config>
</org.jenkinsci.plugins.postbuildscript.PostBuildScript>
</publishers>
</project>

View File

@ -0,0 +1,57 @@
publishers:
- postbuildscript:
mark-unstable-if-failed: true
generic-script:
- file-path: '/fakepath/generic'
role: MASTER
build-on:
- SUCCESS
- UNSTABLE
- file-path: '/fakepath/generic-two'
role: SLAVE
build-on:
- NOT_BUILT
- ABORTED
- FAILURE
groovy-script:
- file-path: '/fakepath/groovy'
role: MASTER
build-on:
- SUCCESS
- UNSTABLE
- file-path: '/fakepath/groovy-too'
role: SLAVE
build-on:
- NOT_BUILT
- ABORTED
- FAILURE
groovy:
- role: MASTER
build-on:
- SUCCESS
- UNSTABLE
content: 'println "Hello world!"'
- role: SLAVE
build-on:
- NOT_BUILT
- ABORTED
- FAILURE
content: |
println "Hello world!"
println "Multi-line script"
builders:
- role: MASTER
build-on:
- SUCCESS
- UNSTABLE
build-steps:
- shell: 'echo "Hello world!"'
- role: SLAVE
build-on:
- NOT_BUILT
- ABORTED
- FAILURE
build-steps:
- shell: 'echo "Hello world!"'
- shell: 'echo "Goodbye world!"'

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<publishers>
<org.jenkinsci.plugins.postbuildscript.PostBuildScript>
<config>
<markBuildUnstable>false</markBuildUnstable>
<scriptFiles/>
<groovyScripts/>
<buildSteps/>
</config>
</org.jenkinsci.plugins.postbuildscript.PostBuildScript>
</publishers>
</project>

View File

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

View File

@ -0,0 +1,3 @@
- longName: 'Jenkins PostBuildScript Plugin'
shortName: 'postbuildscript'
version: "1.0"

View File

@ -2,9 +2,9 @@
<project>
<publishers>
<org.jenkinsci.plugins.postbuildscript.PostBuildScript>
<markBuildUnstable>false</markBuildUnstable>
<scriptOnlyIfSuccess>true</scriptOnlyIfSuccess>
<scriptOnlyIfFailure>false</scriptOnlyIfFailure>
<markBuildUnstable>false</markBuildUnstable>
</org.jenkinsci.plugins.postbuildscript.PostBuildScript>
</publishers>
</project>

View File

@ -0,0 +1,3 @@
- longName: 'Jenkins PostBuildScript Plugin'
shortName: 'postbuildscript'
version: "1.0"

View File

@ -2,6 +2,7 @@
<project>
<publishers>
<org.jenkinsci.plugins.postbuildscript.PostBuildScript>
<markBuildUnstable>true</markBuildUnstable>
<genericScriptFileList>
<org.jenkinsci.plugins.postbuildscript.GenericScript>
<filePath>/tmp/one.sh</filePath>
@ -28,7 +29,6 @@
</groovyScriptContentList>
<scriptOnlyIfSuccess>false</scriptOnlyIfSuccess>
<scriptOnlyIfFailure>true</scriptOnlyIfFailure>
<markBuildUnstable>true</markBuildUnstable>
</org.jenkinsci.plugins.postbuildscript.PostBuildScript>
</publishers>
</project>

View File

@ -0,0 +1,3 @@
- longName: 'Jenkins PostBuildScript Plugin'
shortName: 'postbuildscript'
version: "1.0"

View File

@ -2,6 +2,7 @@
<project>
<publishers>
<org.jenkinsci.plugins.postbuildscript.PostBuildScript>
<markBuildUnstable>false</markBuildUnstable>
<buildSteps>
<hudson.tasks.Shell>
<command>echo &quot;Shell execution&quot;</command>
@ -13,7 +14,6 @@
</buildSteps>
<scriptOnlyIfSuccess>true</scriptOnlyIfSuccess>
<scriptOnlyIfFailure>false</scriptOnlyIfFailure>
<markBuildUnstable>false</markBuildUnstable>
</org.jenkinsci.plugins.postbuildscript.PostBuildScript>
</publishers>
</project>

View File

@ -0,0 +1,3 @@
- longName: 'Jenkins PostBuildScript Plugin'
shortName: 'postbuildscript'
version: "1.0"

View File

@ -2,6 +2,7 @@
<project>
<publishers>
<org.jenkinsci.plugins.postbuildscript.PostBuildScript>
<markBuildUnstable>false</markBuildUnstable>
<buildSteps>
<hudson.tasks.Shell>
<command>echo &quot;Shell execution&quot;</command>
@ -9,7 +10,6 @@
</buildSteps>
<scriptOnlyIfSuccess>true</scriptOnlyIfSuccess>
<scriptOnlyIfFailure>false</scriptOnlyIfFailure>
<markBuildUnstable>false</markBuildUnstable>
<executeOn>MATRIX</executeOn>
</org.jenkinsci.plugins.postbuildscript.PostBuildScript>
</publishers>

View File

@ -0,0 +1,3 @@
- longName: 'Jenkins PostBuildScript Plugin'
shortName: 'postbuildscript'
version: "1.0"

View File

@ -2,6 +2,7 @@
<project>
<publishers>
<org.jenkinsci.plugins.postbuildscript.PostBuildScript>
<markBuildUnstable>true</markBuildUnstable>
<buildSteps>
<hudson.tasks.Shell>
<command>echo &quot;Shell execution should be first&quot;</command>
@ -37,7 +38,6 @@
</groovyScriptFileList>
<scriptOnlyIfSuccess>false</scriptOnlyIfSuccess>
<scriptOnlyIfFailure>true</scriptOnlyIfFailure>
<markBuildUnstable>true</markBuildUnstable>
</org.jenkinsci.plugins.postbuildscript.PostBuildScript>
</publishers>
</project>