From 271917c757d5783addcd04e13ac9a0779ed52218 Mon Sep 17 00:00:00 2001 From: Wayne Date: Tue, 27 Jan 2015 01:09:13 -0800 Subject: [PATCH] Add Parameterized Remote Trigger Plugin support. Trigger build of job on remote Jenkins instance. Requires the Jenkins Parameterized Remote Trigger Plugin: https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Remote+Trigger+Plugin Please note that this plugin requires system configuration on the Jenkins Master that is unavailable from individual job views; specifically, one must add remote jenkins servers whose 'Display Name' field are what make up valid fields on the `remote-jenkins-name` attribute below. Change-Id: I88a653b315b8609ba016e88d77156544dacd68ea --- jenkins_jobs/modules/builders.py | 97 +++++++++++++++++++ setup.cfg | 1 + .../trigger-remote/trigger-remote001.xml | 25 +++++ .../trigger-remote/trigger-remote001.yaml | 14 +++ .../trigger-remote/trigger-remote002.xml | 17 ++++ .../trigger-remote/trigger-remote002.yaml | 4 + 6 files changed, 158 insertions(+) create mode 100644 tests/builders/fixtures/trigger-remote/trigger-remote001.xml create mode 100644 tests/builders/fixtures/trigger-remote/trigger-remote001.yaml create mode 100644 tests/builders/fixtures/trigger-remote/trigger-remote002.xml create mode 100644 tests/builders/fixtures/trigger-remote/trigger-remote002.yaml diff --git a/jenkins_jobs/modules/builders.py b/jenkins_jobs/modules/builders.py index a4f780770..c6d02e767 100644 --- a/jenkins_jobs/modules/builders.py +++ b/jenkins_jobs/modules/builders.py @@ -277,6 +277,103 @@ def ant(parser, xml_parent, data): XML.SubElement(ant, 'antName').text = data.get('ant-name', 'default') +def trigger_remote(parser, xml_parent, data): + """yaml: trigger-remote + Trigger build of job on remote Jenkins instance. + + :jenkins-wiki:`Parameterized Remote Trigger Plugin + ` + + Please note that this plugin requires system configuration on the Jenkins + Master that is unavailable from individual job views; specifically, one + must add remote jenkins servers whose 'Display Name' field are what make up + valid fields on the `remote-jenkins-name` attribute below. + + :arg str remote-jenkins-name: the remote Jenkins server (required) + :arg str job: the Jenkins project to trigger on the remote Jenkins server + (required) + :arg bool should-not-fail-build: + if true, remote job failure will not lead current job to fail + (default false) + :arg bool prevent-remote-build-queue: + if true, wait to trigger remote builds until no other builds + (default false) + :arg bool block: whether to wait for the trigger jobs to finish or not + (default true) + :arg str poll-interval: polling interval in seconds for checking statues of + triggered remote job, only necessary if current job is configured to + block + (default 10) + :arg str connection-retry-limit: number of connection attempts to remote + Jenkins server before giving up. + (default 5) + :arg str predefined-parameters: predefined parameters to send to the remote + job when triggering it + (optional) + :arg str property-file: file in workspace of current job containing + additional parameters to be set on remote job + (optional) + + Example: + + .. literalinclude:: \ + /../../tests/builders/fixtures/trigger-remote/trigger-remote001.yaml + :language: yaml + """ + triggerr = XML.SubElement(xml_parent, + 'org.jenkinsci.plugins.' + 'ParameterizedRemoteTrigger.' + 'RemoteBuildConfiguration') + XML.SubElement(triggerr, + 'remoteJenkinsName').text = data.get('remote-jenkins-name') + XML.SubElement(triggerr, 'token').text = data.get('token', '') + + for attribute in ['job', 'remote-jenkins-name']: + if attribute not in data: + raise MissingAttributeError(attribute, "builders.trigger-remote") + if data[attribute] == '': + raise InvalidAttributeError(attribute, + data[attribute], + "builders.trigger-remote") + + XML.SubElement(triggerr, 'job').text = data.get('job') + + XML.SubElement(triggerr, 'shouldNotFailBuild').text = str( + data.get('should-not-fail-build', False)).lower() + + XML.SubElement(triggerr, + 'pollInterval').text = str(data.get('poll-interval', 10)) + XML.SubElement(triggerr, 'connectionRetryLimit').text = str( + data.get('connection-retry-limit', 5)) + + XML.SubElement(triggerr, 'preventRemoteBuildQueue').text = str( + data.get('prevent-remote-build-queue', False)).lower() + + XML.SubElement(triggerr, 'blockBuildUntilComplete').text = str( + data.get('block', True)).lower() + + if 'predefined-parameters' in data: + parameters = XML.SubElement(triggerr, 'parameters') + parameters.text = data.get('predefined-parameters', '') + params_list = parameters.text.split("\n") + + parameter_list = XML.SubElement(triggerr, 'parameterList') + for param in params_list: + if param == '': + continue + tmp = XML.SubElement(parameter_list, 'string') + tmp.text = param + + if 'property-file' in data and data['property-file'] != '': + XML.SubElement(triggerr, 'loadParamsFromFile').text = 'true' + XML.SubElement(triggerr, + 'parameterFile').text = data.get('property-file') + else: + XML.SubElement(triggerr, 'loadParamsFromFile').text = 'false' + + XML.SubElement(triggerr, 'overrideAuth').text = "false" + + def trigger_builds(parser, xml_parent, data): """yaml: trigger-builds Trigger builds of other jobs. diff --git a/setup.cfg b/setup.cfg index d753b3c0a..0fd2e49d3 100644 --- a/setup.cfg +++ b/setup.cfg @@ -78,6 +78,7 @@ jenkins_jobs.builders = ssh-builder=jenkins_jobs.modules.builders:ssh_builder system-groovy=jenkins_jobs.modules.builders:system_groovy trigger-builds=jenkins_jobs.modules.builders:trigger_builds + trigger-remote=jenkins_jobs.modules.builders:trigger_remote jenkins_jobs.reporters = email=jenkins_jobs.modules.reporters:email findbugs=jenkins_jobs.modules.reporters:findbugs diff --git a/tests/builders/fixtures/trigger-remote/trigger-remote001.xml b/tests/builders/fixtures/trigger-remote/trigger-remote001.xml new file mode 100644 index 000000000..a22e0b4df --- /dev/null +++ b/tests/builders/fixtures/trigger-remote/trigger-remote001.xml @@ -0,0 +1,25 @@ + + + + + http://example.jenkinsmaster.lan + BLAH + build-things + false + 5 + 5 + true + true + FOO="bar" +herp="derp" + + + FOO="bar" + herp="derp" + + true + .props + false + + + diff --git a/tests/builders/fixtures/trigger-remote/trigger-remote001.yaml b/tests/builders/fixtures/trigger-remote/trigger-remote001.yaml new file mode 100644 index 000000000..06b7c04b6 --- /dev/null +++ b/tests/builders/fixtures/trigger-remote/trigger-remote001.yaml @@ -0,0 +1,14 @@ +builders: + - trigger-remote: + remote-jenkins-name: "http://example.jenkinsmaster.lan" + token: "BLAH" + job: "build-things" + should-fail-build: True + prevent-remote-build-queue: True + poll-interval: 5 + connection-retry-limit: 5 + block: true + property-file: '.props' + predefined-parameters: | + FOO="bar" + herp="derp" diff --git a/tests/builders/fixtures/trigger-remote/trigger-remote002.xml b/tests/builders/fixtures/trigger-remote/trigger-remote002.xml new file mode 100644 index 000000000..6a7344e80 --- /dev/null +++ b/tests/builders/fixtures/trigger-remote/trigger-remote002.xml @@ -0,0 +1,17 @@ + + + + + http://example.jenkinsmaster.lan + + build-things + false + 10 + 5 + false + true + false + false + + + diff --git a/tests/builders/fixtures/trigger-remote/trigger-remote002.yaml b/tests/builders/fixtures/trigger-remote/trigger-remote002.yaml new file mode 100644 index 000000000..b69cd7c1d --- /dev/null +++ b/tests/builders/fixtures/trigger-remote/trigger-remote002.yaml @@ -0,0 +1,4 @@ +builders: + - trigger-remote: + remote-jenkins-name: "http://example.jenkinsmaster.lan" + job: "build-things"