Selenium Docker Integration Through Jenkinsfile – Part 2 – Building Docker Image & Pushing To Dockerhub

Overview:

This is Part 2 of Selenium Docker Integration Through Jenkinsfile series. If you have not read the Part 1, I would request you to check here.

In this part, Lets see how to create a Dockerfile, build an Image using Dockerfile and push the image to Docker hub for distribution.

Udemy – Selenium WebDriver With Docker:

TestAutomationGuru has released a brand new course in Udemy on Selenium WebDriver with Docker. 14 hours course which starts with installing docker from scratch and goes all the way up to running dockerized selenium tests on AWS cloud. Please access the above link which gives you the special discount.  You can also get your money back if you do not like the course within 30 days.

Prerequisite:

  • You have working project in GitHub or some Source Control.
  • If you do not have any, you could simply fork this project.

Dockerfile:

Now Lets create a Dockerfile to build an image with all the dependencies.

For the sample project we had created in Part 1, I need to have

  • an environment which has java 8 installed
  • container-test.jar
  • libs directory with all the dependency jars
  • suite folder with xmls

If an environment has all these above 4, I should be able to run my test just fine. If you have modified the project, just add/remove the required items based on your requirements as I had done above.

In my case, I create the below Dockerfile.

  • I took a base image which has java8 installed. (We do not need JDK. We want only JRE as our project has been built already)
  • I consider an arbitrary directory usr/share/tag as my WORKDIR and add all the dependencies into the directory.
  • Once I added all the dependencies, I provide the ENTRYPOINT which is – the required command to invoke the test.
FROM openjdk:8-jre-slim

#A Directory in the base image to copy our depedencies
WORKDIR /usr/share/tag

# Add the project jar & copy dependencies
ADD  target/container-test.jar container-test.jar
ADD  target/libs libs

# Add the suite xmls
ADD suite/order-module.xml order-module.xml
ADD suite/search-module.xml search-module.xml

# Command line to execute the test
# Expects below ennvironment variables
# BROWSER = chrome / firefox
# MODULE  = order-module / search-module
# SELENIUM_HUB = selenium hub hostname / ipaddress

ENTRYPOINT java -cp container-test.jar:libs/* -DseleniumHubHost=$SELENIUM_HUB -Dbrowser=$BROWSER org.testng.TestNG $MODULE

We do not want to hard code selenium grid IP details, browser and module details. So those are assumed to be passed as an environment variables. (If you have modified the sample project, modify the Dockerfile as required.)

You should commit this Dockerfile as part of the project in Github.

Next Lets see how to build a Docker image and push it to Docker hub.

Dockerhub:

If you do not have a Docker hub account, I would encourage you register here to create an account. It is Free. We can also use our own registries. But setting it up and using it requires a separate article to explain that. So, for this article, we would be using Docker hub as it is Free & easy to distribute images.

Once you have an account, make a note of your username and password.

Jenkins – Credentials & Node Labels:

I assume you have a latest version of Jenkins with enough privileges. If you have docker installed, you can easily spin up a new instance of Jenkins just for playing with it.

  • Add your Docker hub credentials in Jenkins

Screenshot from 2018-07-08 11-19-02

  • Ensure that you have an agent/node in your Jenkins with docker installed.

Screenshot from 2018-07-08 11-17-26

 

Jenkinsfile:

Jenkinsfile is basically a textfile – like Dockerfile – which contains build information as code.

  • Our Jenkinsfile has 3 stages
    • Build Jar – Our project is built by issuing mvn clean package -DskipTests
    • Build Image – Once the project is built, Docker image is built using the Dockerfile in the project directory.
    • Push Image – Once the image is built, we need to push it to Dockerhub using the credentials id you had given.

Windows users will have to replace below ‘sh‘ commands with ‘bat

 

pipeline {
    agent none
    stages {    
        stage('Build Jar') {
            agent {
                docker {
                    image 'maven:3-alpine'
                    args '-v $HOME/.m2:/root/.m2'
                }
            }
            steps {
                sh 'mvn clean package -DskipTests'
            }
        }
        stage('Build Image') {
            steps {
                script {
                      // vinsdocker/containertest => organization/application - it could be anything
                      app = docker.build("vinsdocker/containertest")
                }
            }
        }
        stage('Push Image') {
            steps {
                script {
                    docker.withRegistry('https://registry.hub.docker.com', 'dockerhub') {
                        app.push("${BUILD_NUMBER}")
                        app.push("latest")
                    }
                }
            }
        }        
    }
}

This Jenkinsfile should be part of your project. Commit this as well – just as you did for Dockerfile. Both of them should be present under the project directory.

Jenkins – Building The Project:

  • Lets create a new Project in Jenkins

Screenshot from 2018-07-08 11-22-23

  • Go to Pipeline section and update your Git/Repository details.

 

Screenshot from 2018-07-08 11-26-37

  • Now your project is ready to build. No other information is required as everything required is already part of the Jenkinsfile.
  • Run the project and see if you are able to build and push the docker image.

  • I was able to successfully build and push the Docker image as shown here.

Screenshot from 2018-07-08 11-57-55

 

Summary:

We were able to create a Dockerfile with all the dependencies required. By using Jenkins declarative pipeline, we were also be able to build the project, build the docker image and push it to docker hub.

Any machine in your office/house/cloud with Docker can pull the image and execute without any dependency related issues.

Lets see how to start Selenium Grid and run the tests using Jenkinsfile in the next article.

Happy Testing & Subscribe 🙂

 

 

 

Share This:

11 thoughts on “Selenium Docker Integration Through Jenkinsfile – Part 2 – Building Docker Image & Pushing To Dockerhub

  1. Hello.
    I’m using windows and getting Jenkins build error while executing this : sh ‘mvn clean package -DskipTests’. Please advise.

  2. On below how can i replace with bat?

    stage(‘Build Image’) {
    steps {
    script {
    // vinsdocker/containertest => organization/application – it could be anything
    app = docker.build(“vinsdocker/containertest”)
    }
    }
    }
    stage(‘Push Image’) {
    steps {
    script {
    docker.withRegistry(‘https://registry.hub.docker.com’, ‘dockerhub’) {
    app.push(“${BUILD_NUMBER}”)
    app.push(“latest”)

    1. only sh should be replaced with bat on windows. Others are fine. Ensure that you have docker installed

  3. if i want to include stage for tests and reports with latest image it self what are the changes i need to make , Can you please put that configuration changes too ?

  4. Thanks for posting very nice article.

    I have question on below step:

    Ensure that you have an agent/node in your Jenkins with docker and maven installed. Add labels docker and maven.

    how to set this up

      1. Appreciate for prompt response. This means we dont need to create node. Where the tests will be run? Is it on Jenkin Master where docker is already installed or do we need to setup any new slave with docker installed.

        Is there dependency like Jenkin has to be on any particular OS like windows or Linux.

        Thanks

        1. We still a node with docker. Your Jenkins master has docker installed, yes it can run run there as well.if you have machines with some docker installed – some do not have docker, then i would suggest you to use labels to identify the machine where you need to run the job

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.