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
- Ensure that you have an agent/node in your Jenkins with docker installed.
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
- Go to Pipeline section and update your Git/Repository details.
- 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.
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 🙂