From 28cab53d5d78438949b1ca14b50d0ea99377eed9 Mon Sep 17 00:00:00 2001 From: Jan Hruban Date: Fri, 9 Dec 2016 16:51:03 +0100 Subject: [PATCH] Allow template-name in the global defaults Inject the template-name into parameters earlier, so it's possible to use it in the global defaults. Mark the template name as 'verbatim' so the interpolation is skipped by the formatter. Change-Id: I5d98e32ed2bedaa6b536e61fbdec589f358f861f --- doc/source/definition.rst | 3 +++ jenkins_jobs/formatter.py | 11 ++++++++++- jenkins_jobs/parser.py | 4 ++-- tests/yamlparser/fixtures/complete001.xml | 3 ++- tests/yamlparser/fixtures/complete001.yaml | 8 +++++++- 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/doc/source/definition.rst b/doc/source/definition.rst index ee5d379c5..1ff49ef97 100644 --- a/doc/source/definition.rst +++ b/doc/source/definition.rst @@ -401,6 +401,9 @@ You can define variables that will be realized in a `Job Template`. Would create jobs ``build-i386`` and ``build-amd64``. +You can also reference a variable ``{template-name}`` in any value and it will +be subtitued by the name of the current job template being processed. + .. _variable_references: Variable References diff --git a/jenkins_jobs/formatter.py b/jenkins_jobs/formatter.py index 4d0468c15..5fee022f3 100644 --- a/jenkins_jobs/formatter.py +++ b/jenkins_jobs/formatter.py @@ -32,7 +32,9 @@ def deep_format(obj, paramdict, allow_empty=False): # limitations on the values in paramdict - the post-format result must # still be valid YAML (so substituting-in a string containing quotes, for # example, is problematic). - if hasattr(obj, 'format'): + if getattr(obj, 'verbatim', False) is True: + ret = obj + elif hasattr(obj, 'format'): try: ret = CustomFormatter(allow_empty).format(obj, **paramdict) except KeyError as exc: @@ -124,3 +126,10 @@ class CustomFormatter(Formatter): ) return '' raise + + +class VerbatimString(str): + """ + String which is not expanded by `deep_format`. + """ + verbatim = True diff --git a/jenkins_jobs/parser.py b/jenkins_jobs/parser.py index 3594f8b93..f76d8ced2 100644 --- a/jenkins_jobs/parser.py +++ b/jenkins_jobs/parser.py @@ -24,7 +24,7 @@ import os from jenkins_jobs.constants import MAGIC_MANAGE_STRING from jenkins_jobs.errors import JenkinsJobsException -from jenkins_jobs.formatter import deep_format +from jenkins_jobs.formatter import deep_format, VerbatimString import jenkins_jobs.local_yaml as local_yaml from jenkins_jobs import utils @@ -337,6 +337,7 @@ class YamlParser(object): for values in itertools.product(*dimensions): params = copy.deepcopy(project) + params['template-name'] = VerbatimString(template_name) params = self._applyDefaults(params, template) try: @@ -378,7 +379,6 @@ class YamlParser(object): if key not in params: params[key] = template[key] - params['template-name'] = template_name try: expanded = deep_format( template, params, diff --git a/tests/yamlparser/fixtures/complete001.xml b/tests/yamlparser/fixtures/complete001.xml index c34f622f4..40fee172e 100644 --- a/tests/yamlparser/fixtures/complete001.xml +++ b/tests/yamlparser/fixtures/complete001.xml @@ -1,7 +1,8 @@ - <!-- Managed by Jenkins Job Builder --> + JJB template: complete001_{version} +<!-- Managed by Jenkins Job Builder --> false false false diff --git a/tests/yamlparser/fixtures/complete001.yaml b/tests/yamlparser/fixtures/complete001.yaml index 2a9ce7ec6..f64b8084c 100644 --- a/tests/yamlparser/fixtures/complete001.yaml +++ b/tests/yamlparser/fixtures/complete001.yaml @@ -1,3 +1,8 @@ +- defaults: + name: global + description: | + JJB template: {template-name} + - wrapper: name: timeout-wrapper wrappers: @@ -85,10 +90,11 @@ version: - 1.2 jobs: - - 'complete001_{version}' + - 'complete001' - job-template: name: 'complete001_{version}' + id: 'complete001' scm: - gerrit-scm: project_pattern: openstack-infra/jenkins-job-builder