Jenkins supports creating an assembly line. It uses a simple script from the Groovy-based Pipeline DSL.

And these scripts are usually called JenkinsFiles. It defines steps to perform simple or complex tasks based on specified parameters. Once the pipeline is created, it can be used to build code or orchestrate the work needed to move from code submission to delivery.

Pipelining includes steps, nodes and stages. Pipelining is performed on nodes. The nodes are part of Jenkins’ installation. Pipelining usually consists of multiple phases. A phase consists of several steps. Assembly line started guide (https://jenkins.io/doc/pipeline/) to view more content.

For our application in this article, the basic deployment process looks like this:



  1. Developer updates workspace

  2. Jenkins was notified

  3. Jenkins clone workspace

  4. Jenkins creates a Docker image

  5. Jenkins run tests

  6. Jenkins pushed the image to the Docker Hub

The full application code is placed in the GitHub repository (https://github.com/arun-gupta/docker-jenkins-pipeline).

The application is located in the WebApp directory in the repository. The application uses Couchbase’s Java SDK to save a simple JSON document using a connection to the Couchbase database. The application also includes a test to verify that the database contains persistent documents.

Special thanks to @alexsotob for helping me with the Jenkins configuration.

Let’s get started!

Download Jenkins from jenkins.io. Jenkins 2.21 is used here.

Let’s get Jenkins up and running:

JENKINS_HOME=~/.jenkins java-jar ~/Downloads/jenkins-2.21.war --httpPort=9090Copy the code

This command specifies the path to the Jenkin home directory. The home directory contains all the configuration information. The Jenkins listening port is also specified. Port 9090 is specified.

Booting Jenkins will see the following message in the middle terminal:

Jenkins initial setup is required. An admin user has been created and a password generated. Please use the following Jenkins requires the setup to proceed to installation. We have created an administrator account for you and generated a password. Please use the following password to continue the installation. 3521fbc3d40448efa8942f8e464b2dd9 This may also be found at: / Users/arungupta /. Jenkins/secrets/initialAdminPassword (translation: The contents of this code at the same time can also be in ` / Users/arungupta /. Jenkins/secrets/initialAdminPassword `.)Copy the code

Copy the password displayed on the terminal. It will be used to unlock Jenkins.

Enter localhost:9090 in your browser to access Jenkins and paste the password:



Click Next to proceed to the Next step:

Create the first administrator user as shown:



Click Save and Finish to continue.

Click Install Suggested Plugins:



A set of default plug-ins will then be installed:



(Strangely, Ant and Subversion are the default plug-ins.)

Then the login box will pop up:



Enter the user name and password you created earlier.

Finally Jenkins is ready to use.



Admittedly, there are quite a few steps required to set up a simple Jenkins. Is it really necessary to go through so many steps to start using Jenkins? Could there be an easier, dumber, slacker way to start using Jenkins? You want to follow Convention-over-Configuration and provide a pre-configured one-click installation package.

Let’s create a job in Jenkins to run the pipeline.

  1. After Jenkins restarts, he will display a login screen. Enter the user name and password you created earlier. This will bring you back toInstalling Plugins/UpgradePage. Click on the Jenkins icon in the top left corner of the page to see the main control panel:



  2. Click on thecreate new jobAnd the assignment’s name is dodocker-jenkins-pipeline, type selectedPipeline.

    Click the OK button.

  3. Configure the pipeline as shown in the figure:

    Here we use a local warehouse. You can also choose to host the repository on Github. Alternatively, the repository can be configured with a Git hook or timed poller to trigger pipelining. Click the Save button to Save the configuration.

Before starting this job, the Couchbase database needs to be started explicitly:

docker run -d --name db -p 8091-8093:8091-8093 -p 11210:11210 arungupta/oreilly-couchbase:latestCopy the code

This issue will be resolved after issue # 9 (https://github.com/arun-gupta/docker-jenkins-pipeline/issues/9) is fixed. Make sure you can access Couchbase using the username Administrator and password at http://localhost:8091. Click the Data Bucket TAB to see the Bucket named Books that was created.



Click Build Now and you should see output similar to the following:



Everything looks fine.

Let’s try to understand what’s going on behind the scenes.

Jenkinsfile describes how the pipeline is built. As a whole it has four stages – packaging, creating a Docker image, running the application, and running tests. Each stage is displayed in a box in Jenkins’ control panel. The total time spent in each phase is shown in the middle of each box.

And then we try to understand what’s going on at each stage.

Package: The source code for the application is located in the WebApp directory. And this Maven command:

mvn clean package -DskipTestsCopy the code

JAR package used to create the application. Note that the Maven project also contains tests, but this is deliberately ignored through -dskiptests.

Typically, tests are placed separately in a downstream project (https://github.com/arun-gupta/docker-jenkins-pipeline/issues/7). The Maven project creates a Fat JAR file for the application and contains all the dependencies.

Create a Docker image: The Docker image of the application is built using the Dockerfile in the WebApp directory. This image contains only a Fat JAR and can be run through java-JAR.

Each image is labeled with a build number:

${env.BUILD_NUMBER}Copy the code

Running the app: To run the app, you need to run the Docker container of the app. The IP address of the database container can be found by using the following command:

docker inspectCopy the code

Both the database container and the application container run in the default Bridge network. This allows the two containers to communicate with each other. It is also possible to run pipelining in swarm-mode clusters, which requires the creation and use of an overlay network.

Run tests: Tests can be performed using the following command:

mvn testCopy the code

If the test passes, the image is pushed to the Docker Hub. The run structure is captured regardless of whether the test succeeds or not. We also show the use of the try/catch/finally block in Jenkinsfile. If the test passes, the image is pushed to the Docker Hub. In our case, it was pushed to here (https://hub.docker.com/r/arungupta/docker-jenkins-pipeline/tags/).

  • Move the test to a downstream project

  • Use Git hooks or polling to trigger pipelining

  • Automatic database start and stop

  • Docker engine cluster in Swarm mode was used to run pipeline

  • Added additional configuration to push images to Bintray

  • Another sore spot is the inability to find documentation on global variable syntax. Only at < jenkins-host >:< jenkins-port >/job/docker-jenkins-pipeline/pipeline-syntax/globals This is a little bit of a poke.

  • “Not impossible, but there was no implementation (https://twitter.com/agentdero/status/773987834982076416)” # sadpanda