diff --git a/jenkins_jobs/local_yaml.py b/jenkins_jobs/local_yaml.py index 797ca2bbe..2ef39ffde 100644 --- a/jenkins_jobs/local_yaml.py +++ b/jenkins_jobs/local_yaml.py @@ -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:' diff --git a/tests/yamlparser/fixtures/jinja-string01.xml b/tests/yamlparser/fixtures/jinja-string01.xml new file mode 100644 index 000000000..de5ed0184 --- /dev/null +++ b/tests/yamlparser/fixtures/jinja-string01.xml @@ -0,0 +1,23 @@ + + + + <!-- Managed by Jenkins Job Builder --> + false + false + false + false + true + + + + + test variable +a +b +c + + + + + + diff --git a/tests/yamlparser/fixtures/jinja-string01.yaml b/tests/yamlparser/fixtures/jinja-string01.yaml new file mode 100644 index 000000000..70e64238b --- /dev/null +++ b/tests/yamlparser/fixtures/jinja-string01.yaml @@ -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