Retain user specified order of post build scripts

Avoid imposing hardcoded order of the scripts within the module and
instead favour the order of the input data. Combined with use of ordered
dicts for yaml, this ensures that users may control the order of the
various steps to use with the post build scripts from yaml.

Change-Id: Icf540a53cb6f1b13f80f09d9103610aee10d3861
This commit is contained in:
Darragh Bailey 2014-09-01 15:27:02 +01:00
parent 2d74b16620
commit c9ed6891cf
3 changed files with 88 additions and 25 deletions

View File

@ -2396,40 +2396,45 @@ postbuildscript003.yaml
# Shell/Groovy in a file
script_types = {
'generic': 'GenericScript',
'groovy': 'GroovyScriptFile',
'generic-script': 'GenericScript',
'groovy-script': 'GroovyScriptFile',
}
for script_type in sorted(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:
# 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[script_type])
+ script_types[step])
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
# 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 '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)
# Inject builders
if step == 'builders':
build_steps_xml = XML.SubElement(pbs_xml, 'buildSteps')
for builder in script_data:
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

View File

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

View File

@ -0,0 +1,16 @@
publishers:
- postbuildscript:
builders:
- shell: 'echo "Shell execution should be first"'
- ant: 'ant_target'
groovy:
- "/** This is some inlined groovy */"
- "/** Some more inlined groovy */"
generic-script:
- '/tmp/one.sh'
- '/tmp/two.sh'
groovy-script:
- '/tmp/one.groovy'
- '/tmp/two.groovy'
onsuccess: False
onfailure: True