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
This commit is contained in:
parent
888928ee18
commit
f22d7b89eb
@ -42,6 +42,117 @@ logger = logging.getLogger(__name__)
|
|||||||
MIN_TO_SEC = 60
|
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):
|
def ci_skip(parser, xml_parent, data):
|
||||||
"""yaml: ci-skip
|
"""yaml: ci-skip
|
||||||
Skip making a build for certain push.
|
Skip making a build for certain push.
|
||||||
|
20
tests/wrappers/fixtures/docker-custom-build-env001.xml
Normal file
20
tests/wrappers/fixtures/docker-custom-build-env001.xml
Normal file
@ -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>
|
10
tests/wrappers/fixtures/docker-custom-build-env001.yaml
Normal file
10
tests/wrappers/fixtures/docker-custom-build-env001.yaml
Normal file
@ -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
|
33
tests/wrappers/fixtures/docker-custom-build-env002.xml
Normal file
33
tests/wrappers/fixtures/docker-custom-build-env002.xml
Normal file
@ -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>
|
19
tests/wrappers/fixtures/docker-custom-build-env002.yaml
Normal file
19
tests/wrappers/fixtures/docker-custom-build-env002.yaml
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user