Adds support for Property Strategies

Multibranch pipeline jobs also support "Property Strategy" elements.
These are fairly limited, but do contain the very useful "Suppress
automatic SCM triggering" trait.  This commit adds support for
defining these for the "All branches get the same properties"
strategy, and could be easily amended to also support the "Named
branches get different properties" strategy in a future update.

Change-Id: I3d9281657e341260a23f357f6e247793379b0eed
Signed-off-by: sbussetti <steve.bussetti@gmail.com>
This commit is contained in:
sbussetti 2018-11-26 15:57:13 -05:00
parent d29998eaa9
commit dbbca01833
9 changed files with 143 additions and 0 deletions

View File

@ -325,6 +325,10 @@ def bitbucket_scm(xml_parent, data):
discovered initially or a change from the previous revision has been
detected. (optional)
Refer to :func:`~build_strategies <build_strategies>`.
:arg dict property-strategies: Provides control over how to build a branch
(like to disable SCM triggering or to override the pipeline durability)
(optional)
Refer to :func:`~property_strategies <property_strategies>`.
:arg bool local-branch: Check out to matching local branch
If given, checkout the revision to build as HEAD on this branch.
If selected, then the branch name is computed from the remote branch
@ -485,6 +489,9 @@ def bitbucket_scm(xml_parent, data):
helpers.convert_mapping_to_xml(
dbr, data, dbr_mapping, fail_required=True)
if data.get('property-strategies', None):
property_strategies(xml_parent, data)
if data.get('build-strategies', None):
build_strategies(xml_parent, data)
@ -564,6 +571,10 @@ def gerrit_scm(xml_parent, data):
discovered initially or a change from the previous revision has been
detected. (optional)
Refer to :func:`~build_strategies <build_strategies>`.
:arg dict property-strategies: Provides control over how to build a branch
(like to disable SCM triggering or to override the pipeline durability)
(optional)
Refer to :func:`~property_strategies <property_strategies>`.
Minimal Example:
@ -621,6 +632,9 @@ def gerrit_scm(xml_parent, data):
'.RefSpecsSCMSourceTrait_-RefSpecTemplate'))
XML.SubElement(e, 'value').text = x
if data.get('property-strategies', None):
property_strategies(xml_parent, data)
if data.get('build-strategies', None):
build_strategies(xml_parent, data)
@ -648,6 +662,10 @@ def git_scm(xml_parent, data):
discovered initially or a change from the previous revision has been
detected. (optional)
Refer to :func:`~build_strategies <build_strategies>`.
:arg dict property-strategies: Provides control over how to build a branch
(like to disable SCM triggering or to override the pipeline durability)
(optional)
Refer to :func:`~property_strategies <property_strategies>`.
:extensions:
@ -722,6 +740,9 @@ def git_scm(xml_parent, data):
'jenkins.scm.impl.trait.RegexSCMHeadFilterTrait')
XML.SubElement(rshf, 'regex').text = data.get('head-filter-regex')
if data.get('property-strategies', None):
property_strategies(xml_parent, data)
if data.get('build-strategies', None):
build_strategies(xml_parent, data)
@ -774,6 +795,10 @@ def github_scm(xml_parent, data):
discovered initially or a change from the previous revision has been
detected. (optional)
Refer to :func:`~build_strategies <build_strategies>`.
:arg dict property-strategies: Provides control over how to build a branch
(like to disable SCM triggering or to override the pipeline durability)
(optional)
Refer to :func:`~property_strategies <property_strategies>`.
:extensions:
@ -943,6 +968,9 @@ def github_scm(xml_parent, data):
helpers.convert_mapping_to_xml(
dpro, data, dpro_mapping, fail_required=True)
if data.get('property-strategies', None):
property_strategies(xml_parent, data)
if data.get('build-strategies', None):
build_strategies(xml_parent, data)
@ -1106,3 +1134,62 @@ def build_strategies(xml_parent, data):
nb['wildcards-name'],
wildcards_name_mapping,
fail_required=False)
def property_strategies(xml_parent, data):
"""Configure Basic Branch Property Strategies.
Requires the :jenkins-wiki:`Branch API Plugin <Branch+API+Plugin>`.
:arg dict property-strategies: Definition of property strategies.
* **all-branches** (list): A list of property strategy definitions
for use with all branches.
* **suppress-scm-triggering** (bool): Suppresses automatic SCM
triggering (optional)
* **pipeline-branch-durability-override** (str): Set a custom
branch speed/durability level. Valid values:
performance-optimized, survivable-nonatomic, or
max-survivability (optional) Requires the :jenkins-wiki:
`Pipeline Multibranch Plugin <Pipeline+Multibranch+Plugin>`
"""
# Valid options for the pipeline branch durability override.
pbdo_map = collections.OrderedDict([
("max-survivability", "MAX_SURVIVABILITY"),
("performance-optimized", "PERFORMANCE_OPTIMIZED"),
("survivable-nonatomic", "SURVIVABLE_NONATOMIC"),
])
basic_property_strategies = 'jenkins.branch'
workflow_multibranch = 'org.jenkinsci.plugins.workflow.multibranch'
dbps = XML.SubElement(xml_parent, 'strategy', {
'class': ''.join([basic_property_strategies,
'.DefaultBranchPropertyStrategy'])})
prop_strats = data.get('property-strategies', None)
if prop_strats:
props_elem = XML.SubElement(dbps, 'properties', {
'class': 'java.util.Arrays$ArrayList'})
props_elem = XML.SubElement(props_elem, 'a', {
'class': ''.join([
basic_property_strategies, '.BranchProperty-array'])})
for dbs_list in prop_strats.get('all-branches', None):
if dbs_list.get('suppress-scm-triggering', False):
XML.SubElement(props_elem, ''.join([
basic_property_strategies, '.NoTriggerBranchProperty']))
pbdo_val = dbs_list.get(
'pipeline-branch-durability-override', None)
if pbdo_val:
if not pbdo_map.get(pbdo_val):
raise InvalidAttributeError(
'pipeline-branch-durability-override',
pbdo_val,
pbdo_map.keys())
pbdo_elem = XML.SubElement(props_elem, ''.join([
workflow_multibranch, '.DurabilityHintBranchProperty']), {
'plugin': 'workflow-multibranch'})
XML.SubElement(pbdo_elem, 'hint').text = pbdo_map.get(pbdo_val)

View File

@ -104,6 +104,16 @@
</jenkins.plugins.git.traits.AuthorInChangelogTrait>
</traits>
</source>
<strategy class="jenkins.branch.DefaultBranchPropertyStrategy">
<properties class="java.util.Arrays$ArrayList">
<a class="jenkins.branch.BranchProperty-array">
<jenkins.branch.NoTriggerBranchProperty/>
<org.jenkinsci.plugins.workflow.multibranch.DurabilityHintBranchProperty plugin="workflow-multibranch">
<hint>MAX_SURVIVABILITY</hint>
</org.jenkinsci.plugins.workflow.multibranch.DurabilityHintBranchProperty>
</a>
</properties>
</strategy>
<buildStrategies>
<jenkins.branch.buildstrategies.basic.TagBuildStrategyImpl plugin="basic-branch-build-strategies">
<atMostMillis>86400000</atMostMillis>

View File

@ -19,6 +19,10 @@ scm:
filter-by-name-wildcard:
includes: '*'
excludes: 'master'
property-strategies:
all-branches:
- suppress-scm-triggering: true
- pipeline-branch-durability-override: max-survivability
build-strategies:
- tags:
ignore-tags-newer-than: 1

View File

@ -49,6 +49,16 @@
</jenkins.plugins.git.traits.RefSpecsSCMSourceTrait>
</traits>
</source>
<strategy class="jenkins.branch.DefaultBranchPropertyStrategy">
<properties class="java.util.Arrays$ArrayList">
<a class="jenkins.branch.BranchProperty-array">
<jenkins.branch.NoTriggerBranchProperty/>
<org.jenkinsci.plugins.workflow.multibranch.DurabilityHintBranchProperty plugin="workflow-multibranch">
<hint>MAX_SURVIVABILITY</hint>
</org.jenkinsci.plugins.workflow.multibranch.DurabilityHintBranchProperty>
</a>
</properties>
</strategy>
<buildStrategies>
<jenkins.branch.buildstrategies.basic.TagBuildStrategyImpl plugin="basic-branch-build-strategies">
<atMostMillis>86400000</atMostMillis>

View File

@ -7,6 +7,10 @@ scm:
credentials-id: secret
ignore-on-push-notifications: true
refspecs: 'refs/heads/*'
property-strategies:
all-branches:
- suppress-scm-triggering: true
- pipeline-branch-durability-override: max-survivability
build-strategies:
- tags:
ignore-tags-newer-than: 1

View File

@ -81,6 +81,16 @@
</jenkins.plugins.git.traits.AuthorInChangelogTrait>
</traits>
</source>
<strategy class="jenkins.branch.DefaultBranchPropertyStrategy">
<properties class="java.util.Arrays$ArrayList">
<a class="jenkins.branch.BranchProperty-array">
<jenkins.branch.NoTriggerBranchProperty/>
<org.jenkinsci.plugins.workflow.multibranch.DurabilityHintBranchProperty plugin="workflow-multibranch">
<hint>MAX_SURVIVABILITY</hint>
</org.jenkinsci.plugins.workflow.multibranch.DurabilityHintBranchProperty>
</a>
</properties>
</strategy>
<buildStrategies>
<jenkins.branch.buildstrategies.basic.TagBuildStrategyImpl plugin="basic-branch-build-strategies">
<atMostMillis>86400000</atMostMillis>

View File

@ -9,6 +9,10 @@ scm:
ignore-on-push-notifications: true
discover-tags: true
head-filter-regex: 'master|\d+\.\d+'
property-strategies:
all-branches:
- suppress-scm-triggering: true
- pipeline-branch-durability-override: max-survivability
build-strategies:
- tags:
ignore-tags-newer-than: 1

View File

@ -93,6 +93,16 @@
<com.adobe.jenkins.disable__github__multibranch__status.DisableStatusUpdateTrait plugin="disable-github-multibranch-status"/>
</traits>
</source>
<strategy class="jenkins.branch.DefaultBranchPropertyStrategy">
<properties class="java.util.Arrays$ArrayList">
<a class="jenkins.branch.BranchProperty-array">
<jenkins.branch.NoTriggerBranchProperty/>
<org.jenkinsci.plugins.workflow.multibranch.DurabilityHintBranchProperty plugin="workflow-multibranch">
<hint>MAX_SURVIVABILITY</hint>
</org.jenkinsci.plugins.workflow.multibranch.DurabilityHintBranchProperty>
</a>
</properties>
</strategy>
<buildStrategies>
<jenkins.branch.buildstrategies.basic.TagBuildStrategyImpl plugin="basic-branch-build-strategies">
<atMostMillis>86400000</atMostMillis>

View File

@ -14,6 +14,10 @@ scm:
discover-pr-forks-trust: everyone
discover-pr-origin: both
discover-tags: true
property-strategies:
all-branches:
- suppress-scm-triggering: true
- pipeline-branch-durability-override: max-survivability
build-strategies:
- tags:
ignore-tags-newer-than: 1