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 Requires the Jenkins :jenkins-wiki:`Post Build Script plugin
<PostBuildScript+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 :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 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 onsuccess: Deprecated, replaced with script-only-if-succeeded
:arg bool script-only-if-succeeded: Scripts and builders are run only if :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 onfailure: Deprecated, replaced with script-only-if-failed
:arg bool script-only-if-failed: Scripts and builders are run only if the :arg bool script-only-if-failed: Scripts and builders are run only if the
build failed (default false) 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)
:arg str execute-on: For matrix projects, scripts can be run after each :arg str execute-on: For matrix projects, scripts can be run after each
axis is built (`axes`), after all axis of the matrix axis is built (`axes`), after all axis of the matrix are built
are built (`matrix`) or after each axis AND the matrix (`matrix`) or after each axis AND the matrix are built (`both`).
are built (`both`).
The `script-only-if-succeeded` and `bool script-only-if-failed` options are 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 confusing. If you want the post build to always run regardless of the build
status, you should set them both to `false`. 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:: .. literalinclude::
/../../tests/publishers/fixtures/postbuildscript001.yaml /../../tests/publishers/fixtures/postbuildscript001.yaml
@ -3618,6 +3669,98 @@ def postbuildscript(registry, xml_parent, data):
xml_parent, xml_parent,
'org.jenkinsci.plugins.postbuildscript.PostBuildScript') 'org.jenkinsci.plugins.postbuildscript.PostBuildScript')
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')
mapping = [
('mark-unstable-if-failed', 'markBuildUnstable', False),
]
helpers.convert_mapping_to_xml(pbs_xml, data, mapping, fail_required=True)
if version >= pkg_resources.parse_version("2.0"):
################
# Script Files #
################
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)
else: # Options below are all deprecated in version < 2.0 of plugin
# Shell/Groovy in a file # Shell/Groovy in a file
script_types = { script_types = {
'generic-script': 'GenericScript', 'generic-script': 'GenericScript',
@ -3631,8 +3774,8 @@ def postbuildscript(registry, xml_parent, data):
for step, script_data in build_scripts: for step, script_data in build_scripts:
if step in script_types: if step in script_types:
scripts_xml = XML.SubElement(pbs_xml, step[:-len('-script')] + scripts_xml = XML.SubElement(
'ScriptFileList') pbs_xml, step[:-len('-script')] + 'ScriptFileList')
for shell_script in script_data: for shell_script in script_data:
script_xml = XML.SubElement( script_xml = XML.SubElement(
scripts_xml, scripts_xml,
@ -3643,12 +3786,13 @@ def postbuildscript(registry, xml_parent, data):
# Inlined Groovy # Inlined Groovy
if step == 'groovy': if step == 'groovy':
groovy_inline_xml = XML.SubElement(pbs_xml, groovy_inline_xml = XML.SubElement(
'groovyScriptContentList') pbs_xml, 'groovyScriptContentList')
for groovy in script_data: for groovy in script_data:
groovy_xml = XML.SubElement( groovy_xml = XML.SubElement(
groovy_inline_xml, groovy_inline_xml,
'org.jenkinsci.plugins.postbuildscript.GroovyScriptContent' 'org.jenkinsci.plugins.postbuildscript.'
'GroovyScriptContent'
) )
groovy_content = XML.SubElement(groovy_xml, 'content') groovy_content = XML.SubElement(groovy_xml, 'content')
groovy_content.text = groovy groovy_content.text = groovy
@ -3665,25 +3809,22 @@ def postbuildscript(registry, xml_parent, data):
# backwards compatability # backwards compatability
success_xml = XML.SubElement(pbs_xml, 'scriptOnlyIfSuccess') success_xml = XML.SubElement(pbs_xml, 'scriptOnlyIfSuccess')
if 'script-only-if-succeeded' in data: if 'script-only-if-succeeded' in data:
success_xml.text = str(data.get('script-only-if-succeeded', success_xml.text = str(
True)).lower() data.get('script-only-if-succeeded', True)).lower()
else: else:
success_xml.text = str(data.get('onsuccess', True)).lower() success_xml.text = str(data.get('onsuccess', True)).lower()
failure_xml = XML.SubElement(pbs_xml, 'scriptOnlyIfFailure') failure_xml = XML.SubElement(pbs_xml, 'scriptOnlyIfFailure')
if 'script-only-if-failed' in data: if 'script-only-if-failed' in data:
failure_xml.text = str(data.get('script-only-if-failed', failure_xml.text = str(
False)).lower() data.get('script-only-if-failed', False)).lower()
else: else:
failure_xml.text = str(data.get('onfailure', False)).lower() failure_xml.text = str(data.get('onfailure', False)).lower()
# 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, # 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 # either by skipping this part or by raising an error to let the user
# an attempt was made to set execute-on on a non-matrix job. There are # know an attempt was made to set execute-on on a non-matrix job.
# currently no easy ways to check for this though. # There are currently no easy ways to check for this though.
if 'execute-on' in data: if 'execute-on' in data:
valid_values = ('matrix', 'axes', 'both') valid_values = ('matrix', 'axes', 'both')
execute_on = data['execute-on'].lower() execute_on = data['execute-on'].lower()

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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