Always deep format Jinja2 templates
As Jinja2 provides functionality other than interpolation, it's entirely feasible to use it outside of a context where there are template variables. This change ensures that we always deep format Jinja2 templates, even when we wouldn't normally interpolate a regular JJB template. Change-Id: Ia3f86dd2073d48594eaf791fa2277db7e5ebf0ab
This commit is contained in:
parent
123387a86d
commit
ac7bcb5337
@ -23,6 +23,7 @@ import types
|
||||
|
||||
from jenkins_jobs.errors import JenkinsJobsException
|
||||
from jenkins_jobs.formatter import deep_format
|
||||
from jenkins_jobs.local_yaml import Jinja2Loader
|
||||
|
||||
__all__ = [
|
||||
"ModuleRegistry"
|
||||
@ -160,9 +161,10 @@ class ModuleRegistry(object):
|
||||
if isinstance(component, dict):
|
||||
# The component is a singleton dictionary of name: dict(args)
|
||||
name, component_data = next(iter(component.items()))
|
||||
if template_data:
|
||||
if template_data or isinstance(component_data, Jinja2Loader):
|
||||
# Template data contains values that should be interpolated
|
||||
# into the component definition
|
||||
# into the component definition. To handle Jinja2 templates
|
||||
# that don't contain any variables, we also deep format those.
|
||||
try:
|
||||
component_data = deep_format(
|
||||
component_data, template_data,
|
||||
|
19
tests/yamlparser/fixtures/jinja-string02.xml
Normal file
19
tests/yamlparser/fixtures/jinja-string02.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>123</command>
|
||||
</hudson.tasks.Shell>
|
||||
</builders>
|
||||
<publishers/>
|
||||
<buildWrappers/>
|
||||
</project>
|
4
tests/yamlparser/fixtures/jinja-string02.yaml
Normal file
4
tests/yamlparser/fixtures/jinja-string02.yaml
Normal file
@ -0,0 +1,4 @@
|
||||
- job:
|
||||
name: test-job
|
||||
builders:
|
||||
- shell: !j2: "{% for x in [1, 2, 3] %}{{ x }}{% endfor %}"
|
22
tests/yamlparser/fixtures/jinja03.xml
Normal file
22
tests/yamlparser/fixtures/jinja03.xml
Normal file
@ -0,0 +1,22 @@
|
||||
<?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>
|
||||
1
|
||||
2
|
||||
3</command>
|
||||
</hudson.tasks.Shell>
|
||||
</builders>
|
||||
<publishers/>
|
||||
<buildWrappers/>
|
||||
</project>
|
5
tests/yamlparser/fixtures/jinja03.yaml
Normal file
5
tests/yamlparser/fixtures/jinja03.yaml
Normal file
@ -0,0 +1,5 @@
|
||||
- job:
|
||||
name: test-job
|
||||
builders:
|
||||
- shell:
|
||||
!include-jinja2: jinja03.yaml.inc
|
3
tests/yamlparser/fixtures/jinja03.yaml.inc
Normal file
3
tests/yamlparser/fixtures/jinja03.yaml.inc
Normal file
@ -0,0 +1,3 @@
|
||||
{% for x in [1, 2, 3] %}
|
||||
{{ x }}
|
||||
{%- endfor %}
|
Loading…
Reference in New Issue
Block a user