Merge "Add !j2 directive to allow inline Jinja2 templates"

This commit is contained in:
Zuul 2018-02-23 16:37:58 +00:00 committed by Gerrit Code Review
commit 123387a86d
3 changed files with 59 additions and 0 deletions

View File

@ -141,6 +141,15 @@ Examples:
contents of jinja01.yaml.inc:
.. literalinclude:: /../../tests/yamlparser/fixtures/jinja01.yaml.inc
The tag ``!j2:`` takes a string and treats it as a Jinja2 template. It will be
rendered (with the variables in that context) and included in the calling YAML
construct.
Examples:
.. literalinclude:: /../../tests/yamlparser/fixtures/jinja-string01.yaml
"""
import functools
@ -310,6 +319,14 @@ class BaseYAMLObject(YAMLObject):
yaml_dumper = LocalDumper
class J2String(BaseYAMLObject):
yaml_tag = u'!j2:'
@classmethod
def from_yaml(cls, loader, node):
return Jinja2Loader(node.value)
class YamlInclude(BaseYAMLObject):
yaml_tag = u'!include:'

View File

@ -0,0 +1,23 @@
<?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>test variable
a
b
c
</command>
</hudson.tasks.Shell>
</builders>
<publishers/>
<buildWrappers/>
</project>

View File

@ -0,0 +1,19 @@
- builder:
name: test-builder
builders:
- shell:
!j2: |
{{ var }}
{% for item in test_list -%}
{{ item }}
{% endfor %}
- job:
name: test-job
builders:
- test-builder:
var: "test variable"
test_list:
- a
- b
- c