Site icon Vinsguru

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:

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

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.

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.

 

Jenkinsfile:

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

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:

 

 

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:

Exit mobile version