Recently Cloudbees releases the CloudBees Docker Workflow Plugin to make the integration of Docker with Jenkins workflows as easy as possible. Now, deploying a continuous delivery pipeline is (almost) straightforward. Here is a simple but comprehensive example.
Disclaimer: I’m using a maven project for this example because maven a tool I’m comfortable with. This post could be adapted to python, rails or whatever-you-want project with minor efforts.
First of all, we need to install the required plugins in Jenkins:
- Workflow: Aggregator
- CloudBees Docker Workflow
This sample workflow is simple and composed of 4 steps:
- Build and unit tests
- Build Docker image
- Acceptance Tests
- Push Docker image
Build and unit tests
In a new Workflow job enter this script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
This short script runs maven to build the jars and to execute the unit tests. Now we are ready to build the Docker image.
Build Docker image
The CloudBees Docker Workflow Plugin provides a global variable
docker which offers access to the common Docker functions in workflow scripts. For a comprehensive description of the plugin and the available commands, look at the plugin guide.
To build the image we call
build on the
docker variable. Two parameters are passed: the image name (with the Docker notation
[registry/]image[:tag]) and the directory where is located the
1 2 3 4 5
The call returns a handle on the built image so we can work with it.
In order to execute the acceptance tests we wants to run a container from our newly built image. The
withRun method can be invoked on the image handle. It is possible to pass the
docker run parameters like port mapping or volumes configuration.
withRun also takes a code block. The container is started at the begining of the block, then the code in the block is executed and the container is stopped at the end of the block. Note that the block is executed on the Jenkins node, not inside the container. Use the
inside method on the image handle to execute code inside the container.
1 2 3 4 5 6 7 8 9
Push Docker image
The last step consists in pushing the image to a Docker registry. It can be done with the method
In order to configure the registry credentials, go the Jenkins Manager Credentials page. Add a new username/password entry and enter your registry login and password. Click on Advanced to show the ID field and enter a unique identifier.
withRegistry method is also used to pass the registry URL and credentials ID configure above.
1 2 3 4 5 6 7
The image is pushed and, unless you pushed it to a custom registry, should be available on the Docker hub.
This example is simple and far from being perfect. Feel free to share suggestions or questions in the comments.
- The project I used for this sample: https://github.com/jcsirot/atmosphere-calculator
- The workflow script: https://gist.github.com/jcsirot/4de001d280998f27aa82