From 98b69476eaca38647f88a23d266cc677213ba1fd Mon Sep 17 00:00:00 2001 From: Antoine Musso Date: Fri, 13 Jun 2014 23:02:30 +0200 Subject: [PATCH] Apply defaults to job-templates parameters I had the use case of a lot of projects sharing the same job template that uses a lot of variables. That requires each project to define all the variables even if they are mostly identical. This patch propose to define varialbes as 'defaults' and have them applied in the job-template magically (oneline diffs always have that feeling to me). So instead of: - project: name: project1 arch: amd64 builder: debuild lintian: true jobs: - '{name}-build-{arch}' And so on ... I can just: - defaults: name: sanebuild - project: name: project1 defaults: sanebuild jobs: - '{name}-build-{arch}' And override the arch as needed either in the project or by passing it to the job. Without this patch, the provided yamlparser fixture template_honor_defaults.yaml would raise: arch parameter missing to format echo Build arch {arch}. Given: {'': '', 'jobs': ['build-{arch}'], 'name': 'project-name'} Change-Id: Ida1e27eb47356d9cae42175743bd2fd52eb9d869 --- doc/source/definition.rst | 8 ++++ jenkins_jobs/builder.py | 1 + .../fixtures/template_honor_defaults.xml | 39 +++++++++++++++++++ .../fixtures/template_honor_defaults.yaml | 15 +++++++ 4 files changed, 63 insertions(+) create mode 100644 tests/yamlparser/fixtures/template_honor_defaults.xml create mode 100644 tests/yamlparser/fixtures/template_honor_defaults.yaml diff --git a/doc/source/definition.rst b/doc/source/definition.rst index 0e131d0d2..929a73320 100644 --- a/doc/source/definition.rst +++ b/doc/source/definition.rst @@ -108,6 +108,8 @@ If you need several jobs defined that are nearly identical, except perhaps in their names, SCP targets, etc., then you may use a Job Template to specify the particulars of the job, and then use a `Project`_ to realize the job with appropriate variable substitution. +Any variables not specified at the project level will be inherited from +the `Defaults`_. A Job Template has the same syntax as a `Job`_, but you may add variables anywhere in the definition. Variables are indicated by @@ -298,6 +300,12 @@ definitions unless they specify a different Default object with the Will set the job description for every job created. +You can define variables that will be realized in a `Job Template`. + +.. literalinclude:: /../../tests/yamlparser/fixtures/template_honor_defaults.yaml + +Would create jobs ``build-i386`` and ``build-amd64``. + .. _advanced: Advanced diff --git a/jenkins_jobs/builder.py b/jenkins_jobs/builder.py index f21d3d3cc..66de84acc 100644 --- a/jenkins_jobs/builder.py +++ b/jenkins_jobs/builder.py @@ -270,6 +270,7 @@ class YamlParser(object): checksums = set([]) for values in itertools.product(*dimensions): params = copy.deepcopy(project) + params = self.applyDefaults(params) expanded_values = {} for (k, v) in values: diff --git a/tests/yamlparser/fixtures/template_honor_defaults.xml b/tests/yamlparser/fixtures/template_honor_defaults.xml new file mode 100644 index 000000000..ecccd5cd8 --- /dev/null +++ b/tests/yamlparser/fixtures/template_honor_defaults.xml @@ -0,0 +1,39 @@ + + + + <!-- Managed by Jenkins Job Builder --> + false + false + false + false + true + + + + + echo Build arch amd64. + + + + + + + + + + <!-- Managed by Jenkins Job Builder --> + false + false + false + false + true + + + + + echo Build arch i386. + + + + + diff --git a/tests/yamlparser/fixtures/template_honor_defaults.yaml b/tests/yamlparser/fixtures/template_honor_defaults.yaml new file mode 100644 index 000000000..e7a72ee48 --- /dev/null +++ b/tests/yamlparser/fixtures/template_honor_defaults.yaml @@ -0,0 +1,15 @@ +- defaults: + name: global + arch: 'i386' + +- project: + name: project-name + jobs: + - 'build-{arch}' + - 'build-{arch}': + arch: 'amd64' + +- job-template: + name: 'build-{arch}' + builders: + - shell: "echo Build arch {arch}."