summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTimon Wong <timon86.wang@gmail.com>2015-12-16 10:00:13 +0000
committerDarragh Bailey <daragh.bailey@gmail.com>2016-01-21 23:27:51 +0000
commitf22d7b89eb8a9ad13bf489d9286ce6e376b7f3f7 (patch)
tree4383cd54baa90e42a6c74da3822d2d54b0227544
parent888928ee18ec3fd5e42a5f13eca4ec53e277286e (diff)
downloadpython-jenkins-job-builder-f22d7b89eb8a9ad13bf489d9286ce6e376b7f3f7.tar.gz
python-jenkins-job-builder-f22d7b89eb8a9ad13bf489d9286ce6e376b7f3f7.tar.xz
python-jenkins-job-builder-f22d7b89eb8a9ad13bf489d9286ce6e376b7f3f7.zip
Add Docker Custom Build Environment Plugin support
This build wrapper uses the "Cloudbees Docker Custom Build Environment plugin". It allows to define build environment using either a Dockerfile stored in project SCM, or a docker image from docker registry directly. Change-Id: I11f94a10b8a06bd4aa61a817c21a1ee1dd885072
-rw-r--r--jenkins_jobs/modules/wrappers.py111
-rw-r--r--tests/wrappers/fixtures/docker-custom-build-env001.xml20
-rw-r--r--tests/wrappers/fixtures/docker-custom-build-env001.yaml10
-rw-r--r--tests/wrappers/fixtures/docker-custom-build-env002.xml33
-rw-r--r--tests/wrappers/fixtures/docker-custom-build-env002.yaml19
5 files changed, 193 insertions, 0 deletions
diff --git a/jenkins_jobs/modules/wrappers.py b/jenkins_jobs/modules/wrappers.py
index 6d12000d..d53eb32b 100644
--- a/jenkins_jobs/modules/wrappers.py
+++ b/jenkins_jobs/modules/wrappers.py
@@ -42,6 +42,117 @@ logger = logging.getLogger(__name__)
MIN_TO_SEC = 60
+def docker_custom_build_env(parser, xml_parent, data):
+ """yaml: docker-custom-build-env
+ Allows the definition of a build environment for a job using a Docker
+ container.
+ Requires the Jenkins :jenkins-wiki:`CloudBees Docker Custom Build
+ Environment Plugin<CloudBees+Docker+Custom+Build+Environment+Plugin>`.
+
+ :arg str image-type: Docker image type. Valid values and their
+ additional attributes described in the image_types_ table
+ :arg str docker-tool: The name of the docker installation to use
+ (default 'Default')
+ :arg str host: URI to the docker host you are using
+ :arg str credentials-id: Argument to specify the ID of credentials to use
+ for docker host (optional)
+ :arg str registry-credentials-id: Argument to specify the ID of
+ credentials to use for docker registry (optional)
+ :arg list volumes: Volumes to bind mound from slave host into container
+
+ :volume: * **host-path** (`str`) Path on host
+ * **path** (`str`) Path inside container
+
+ :arg bool verbose: Log docker commands executed by plugin on build log
+ (default false)
+ :arg bool privileged: Run in privileged mode (default false)
+ :arg bool force-pull: Force pull (default false)
+ :arg str group: The user to run build has to be the same as the Jenkins
+ slave user so files created in workspace have adequate owner and
+ permission set
+ :arg str command: Container start command (default '/bin/cat')
+ :arg str net: Network bridge (default 'bridge')
+
+ .. _image_types:
+
+ ================== ====================================================
+ Image Type Description
+ ================== ====================================================
+ dockerfile Build docker image from a Dockerfile in project
+ workspace. With this option, project can define the
+ build environment as a Dockerfile stored in SCM with
+ project source code
+
+ :context-path: (str) Path to docker context
+ (default '.')
+ :dockerfile: (str) Use an alternate Dockerfile to
+ build the container hosting this build
+ (default 'Dockerfile')
+ pull Pull specified docker image from Docker repository
+
+ :image: (str) Image id/tag
+ ================== ====================================================
+
+ Example:
+
+ .. literalinclude::
+ /../../tests/wrappers/fixtures/docker-custom-build-env001.yaml
+ :language: yaml
+ """
+ core_prefix = 'com.cloudbees.jenkins.plugins.okidocki.'
+ entry_xml = XML.SubElement(
+ xml_parent, core_prefix + 'DockerBuildWrapper')
+ entry_xml.set('plugin', 'docker-custom-build-environment')
+
+ selectorobj = XML.SubElement(entry_xml, 'selector')
+ image_type = data['image-type']
+ if image_type == 'dockerfile':
+ selectorobj.set('class', core_prefix + 'DockerfileImageSelector')
+ XML.SubElement(selectorobj, 'contextPath').text = data.get(
+ 'context-path', '.')
+ XML.SubElement(selectorobj, 'dockerfile').text = data.get(
+ 'dockerfile', 'Dockerfile')
+ elif image_type == 'pull':
+ selectorobj.set('class', core_prefix + 'PullDockerImageSelector')
+ XML.SubElement(selectorobj, 'image').text = data.get(
+ 'image', '')
+
+ XML.SubElement(entry_xml, 'dockerInstallation').text = data.get(
+ 'docker-tool', 'Default')
+
+ host = XML.SubElement(entry_xml, 'dockerHost')
+ host.set('plugin', 'docker-commons')
+ if data.get('host'):
+ XML.SubElement(host, 'uri').text = data['host']
+ if data.get('credentials-id'):
+ XML.SubElement(host, 'credentialsId').text = data['credentials-id']
+ XML.SubElement(entry_xml, 'dockerRegistryCredentials').text = data.get(
+ 'registry-credentials-id', '')
+
+ volumesobj = XML.SubElement(entry_xml, 'volumes')
+ volumes = data.get('volumes', [])
+ if not volumes:
+ volumesobj.set('class', 'empty-list')
+ else:
+ for volume in volumes:
+ volumeobj = XML.SubElement(
+ volumesobj, 'com.cloudbees.jenkins.plugins.okidocki.Volume')
+ XML.SubElement(volumeobj, 'hostPath').text = volume['volume'].get(
+ 'host-path', '')
+ XML.SubElement(volumeobj, 'path').text = volume['volume'].get(
+ 'path', '')
+
+ XML.SubElement(entry_xml, 'forcePull').text = str(data.get(
+ 'force-pull', False)).lower()
+ XML.SubElement(entry_xml, 'privileged').text = str(data.get(
+ 'privileged', False)).lower()
+ XML.SubElement(entry_xml, 'verbose').text = str(data.get(
+ 'verbose', False)).lower()
+ XML.SubElement(entry_xml, 'group').text = data.get('group', '')
+ XML.SubElement(entry_xml, 'command').text = data.get('command', '/bin/cat')
+ XML.SubElement(entry_xml, 'net').text = data.get('net', 'bridge')
+
+
def ci_skip(parser, xml_parent, data):
"""yaml: ci-skip
Skip making a build for certain push.
diff --git a/tests/wrappers/fixtures/docker-custom-build-env001.xml b/tests/wrappers/fixtures/docker-custom-build-env001.xml
new file mode 100644
index 00000000..b78b7466
--- /dev/null
+++ b/tests/wrappers/fixtures/docker-custom-build-env001.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<project>
+ <buildWrappers>
+ <com.cloudbees.jenkins.plugins.okidocki.DockerBuildWrapper plugin="docker-custom-build-environment">
+ <selector class="com.cloudbees.jenkins.plugins.okidocki.PullDockerImageSelector">
+ <image>centos:7</image>
+ </selector>
+ <dockerInstallation>Default</dockerInstallation>
+ <dockerHost plugin="docker-commons"/>
+ <dockerRegistryCredentials/>
+ <volumes class="empty-list"/>
+ <forcePull>true</forcePull>
+ <privileged>true</privileged>
+ <verbose>true</verbose>
+ <group>jenkins</group>
+ <command>/bin/cat</command>
+ <net>bridge</net>
+ </com.cloudbees.jenkins.plugins.okidocki.DockerBuildWrapper>
+ </buildWrappers>
+</project>
diff --git a/tests/wrappers/fixtures/docker-custom-build-env001.yaml b/tests/wrappers/fixtures/docker-custom-build-env001.yaml
new file mode 100644
index 00000000..2fc630f0
--- /dev/null
+++ b/tests/wrappers/fixtures/docker-custom-build-env001.yaml
@@ -0,0 +1,10 @@
+wrappers:
+ - docker-custom-build-env:
+ image-type: 'pull'
+ image: 'centos:7'
+ force-pull: true
+ privileged: true
+ verbose: true
+ group: jenkins
+ command: /bin/cat
+ net: bridge
diff --git a/tests/wrappers/fixtures/docker-custom-build-env002.xml b/tests/wrappers/fixtures/docker-custom-build-env002.xml
new file mode 100644
index 00000000..9a9e4737
--- /dev/null
+++ b/tests/wrappers/fixtures/docker-custom-build-env002.xml
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="utf-8"?>
+<project>
+ <buildWrappers>
+ <com.cloudbees.jenkins.plugins.okidocki.DockerBuildWrapper plugin="docker-custom-build-environment">
+ <selector class="com.cloudbees.jenkins.plugins.okidocki.DockerfileImageSelector">
+ <contextPath>./docker</contextPath>
+ <dockerfile>Dockerfile</dockerfile>
+ </selector>
+ <dockerInstallation>Custom Docker</dockerInstallation>
+ <dockerHost plugin="docker-commons">
+ <uri>tcp://127.0.0.1:1234</uri>
+ <credentialsId>myCredentials</credentialsId>
+ </dockerHost>
+ <dockerRegistryCredentials>myRegistryCredentials</dockerRegistryCredentials>
+ <volumes>
+ <com.cloudbees.jenkins.plugins.okidocki.Volume>
+ <hostPath>/hostJenkins</hostPath>
+ <path>/jenkins</path>
+ </com.cloudbees.jenkins.plugins.okidocki.Volume>
+ <com.cloudbees.jenkins.plugins.okidocki.Volume>
+ <hostPath>/my-volume</hostPath>
+ <path>/my-volume</path>
+ </com.cloudbees.jenkins.plugins.okidocki.Volume>
+ </volumes>
+ <forcePull>false</forcePull>
+ <privileged>false</privileged>
+ <verbose>false</verbose>
+ <group/>
+ <command>/bin/cat</command>
+ <net>bridge</net>
+ </com.cloudbees.jenkins.plugins.okidocki.DockerBuildWrapper>
+ </buildWrappers>
+</project>
diff --git a/tests/wrappers/fixtures/docker-custom-build-env002.yaml b/tests/wrappers/fixtures/docker-custom-build-env002.yaml
new file mode 100644
index 00000000..075b30fa
--- /dev/null
+++ b/tests/wrappers/fixtures/docker-custom-build-env002.yaml
@@ -0,0 +1,19 @@
+wrappers:
+ - docker-custom-build-env:
+ image-type: 'dockerfile'
+ context-path: './docker'
+ dockerfile: 'Dockerfile'
+ docker-tool: 'Custom Docker'
+ host: 'tcp://127.0.0.1:1234'
+ credentials-id: 'myCredentials'
+ registry-credentials-id: 'myRegistryCredentials'
+ volumes:
+ - volume:
+ host-path: '/hostJenkins'
+ path: '/jenkins'
+ - volume:
+ host-path: '/my-volume'
+ path: '/my-volume'
+ force-pull: false
+ privileged: false
+ verbose: false