STORY: 2010534 Fix bug: j2-yaml is not allowed inside macro body
Change-Id: I24916df5f0592a5ea8197e7f372bd37fdba3a461
This commit is contained in:
parent
af9e03ec08
commit
a701920d7b
@ -26,6 +26,7 @@ from six import PY2
|
|||||||
|
|
||||||
from jenkins_jobs.errors import JenkinsJobsException
|
from jenkins_jobs.errors import JenkinsJobsException
|
||||||
from jenkins_jobs.expander import Expander, ParamsExpander
|
from jenkins_jobs.expander import Expander, ParamsExpander
|
||||||
|
from jenkins_jobs.yaml_objects import BaseYamlObject
|
||||||
|
|
||||||
__all__ = ["ModuleRegistry"]
|
__all__ = ["ModuleRegistry"]
|
||||||
|
|
||||||
@ -319,12 +320,16 @@ class ModuleRegistry(object):
|
|||||||
expander = self._params_expander
|
expander = self._params_expander
|
||||||
else:
|
else:
|
||||||
expander = self._expander
|
expander = self._expander
|
||||||
|
expander_params = {**component_data, **(job_data or {})}
|
||||||
|
|
||||||
for b in macro.elements:
|
elements = macro.elements
|
||||||
|
if isinstance(elements, BaseYamlObject):
|
||||||
|
# Expand !j2-yaml element right below macro body.
|
||||||
|
elements = elements.expand(expander, expander_params)
|
||||||
|
|
||||||
|
for b in elements:
|
||||||
try:
|
try:
|
||||||
element = expander.expand(
|
element = expander.expand(b, expander_params)
|
||||||
b, params={**component_data, **(job_data or {})}
|
|
||||||
)
|
|
||||||
except JenkinsJobsException as x:
|
except JenkinsJobsException as x:
|
||||||
raise JenkinsJobsException(f"While expanding macro {name!r}: {x}")
|
raise JenkinsJobsException(f"While expanding macro {name!r}: {x}")
|
||||||
# Pass component_data in as template data to this function
|
# Pass component_data in as template data to this function
|
||||||
|
19
tests/yamlparser/job_fixtures/jinja-yaml-macro-body.xml
Normal file
19
tests/yamlparser/job_fixtures/jinja-yaml-macro-body.xml
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<project>
|
||||||
|
<actions/>
|
||||||
|
<description><!-- Managed by Jenkins Job Builder --></description>
|
||||||
|
<keepDependencies>false</keepDependencies>
|
||||||
|
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
|
||||||
|
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
|
||||||
|
<concurrentBuild>false</concurrentBuild>
|
||||||
|
<canRoam>true</canRoam>
|
||||||
|
<properties/>
|
||||||
|
<scm class="hudson.scm.NullSCM"/>
|
||||||
|
<builders>
|
||||||
|
<hudson.tasks.Shell>
|
||||||
|
<command>echo ${var}</command>
|
||||||
|
</hudson.tasks.Shell>
|
||||||
|
</builders>
|
||||||
|
<publishers/>
|
||||||
|
<buildWrappers/>
|
||||||
|
</project>
|
17
tests/yamlparser/job_fixtures/jinja-yaml-macro-body.yaml
Normal file
17
tests/yamlparser/job_fixtures/jinja-yaml-macro-body.yaml
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
- builder:
|
||||||
|
name: sample-macro
|
||||||
|
builders:
|
||||||
|
!j2-yaml: |
|
||||||
|
# Check that inner elements are expanded, but only once.
|
||||||
|
- shell: !j2: |
|
||||||
|
echo ${var}
|
||||||
|
|
||||||
|
- job-template:
|
||||||
|
name: sample-job
|
||||||
|
builders:
|
||||||
|
- sample-macro
|
||||||
|
|
||||||
|
- project:
|
||||||
|
name: sample-project
|
||||||
|
jobs:
|
||||||
|
- sample-job
|
Loading…
x
Reference in New Issue
Block a user