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:
Daniel Watkins 2018-02-21 19:42:33 -05:00
parent 123387a86d
commit ac7bcb5337
6 changed files with 57 additions and 2 deletions

View File

@ -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,

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<actions/>
<description>&lt;!-- Managed by Jenkins Job Builder --&gt;</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>

View File

@ -0,0 +1,4 @@
- job:
name: test-job
builders:
- shell: !j2: "{% for x in [1, 2, 3] %}{{ x }}{% endfor %}"

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<actions/>
<description>&lt;!-- Managed by Jenkins Job Builder --&gt;</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>

View File

@ -0,0 +1,5 @@
- job:
name: test-job
builders:
- shell:
!include-jinja2: jinja03.yaml.inc

View File

@ -0,0 +1,3 @@
{% for x in [1, 2, 3] %}
{{ x }}
{%- endfor %}