Tuesday, February 5, 2019

Jenkins Pipeline Build and push Docker Image to Docker Hub

Jenkins Pipeline:
Jenkins Pipeline uses a build pipeline plugin which supports continues delivery pipeline into Jenkins.

A continuous delivery (CD) pipeline is an automated expression of your process for getting software from version control right through to your users and customers.

The definition of a Jenkins Pipeline is written into a text file (called a Jenkinsfile) which in turn can be committed to a project’s source control repository.



Jenkins support two types of the pipeline:
  1. Scripted pipeline
  2. Declarative pipeline
Declarative and Scripted Pipelines are constructed fundamentally differently. Declarative Pipeline is a more recent feature of Jenkins Pipeline which:
  • provides richer syntactical features over Scripted Pipeline syntax, and
  • is designed to make writing and reading Pipeline code easier.

PipeLine Concepts:
The following concepts are key aspects of Jenkins Pipeline, which tie in closely to Pipeline syntax
  • Node - Node is a machine which is part of Jenkins env and capable to execute pipeline.
  • Stage - It uses to describe conceptually subnet of task 
  • Step - It tells Jenkins what to do
Prerequisites: 
  1. Windows machine installed following software.
  2. Installed Docker on Windows.
  3. Installed Jenkins
  4. Docker Hub Login
Step 1- Install Docker on Windows:
I'm using Windows 7 for the demonstration, You can follow this blog to finish the Docker for Windows installation.

Step 2- Install Jenkins On Windows:
Follow this blog to finish the Jenkins installation on Windows: How to Install Jenkins on Windows Machine.

Step 3- Complete pre-requisites: 

  • Add Docker hub login details: 
To add docker hub login details open Jenkins and go to add credentials page:
On add credentials page add docker User and Password

After adding credentials. 
In the Jenkinsfile, I have defined steps to stop and delete existing container so we need to run following command before continuing. Open Docker quick terminal and the command.
$ docker run -dit --name apache2 -p 80:80 ubuntu
The above command will create a container of ubuntu with name apache2. Let's continue. 

Step 4- Setup Jenkins Job:
Open Jenkins > new project  >Enter Project name > Choose Project type Pipeline

Choose Ok > Choose this project is parameterized > select string parameter in the drop down list> fill out the details as in the picture below


On the Pipeline section select Definition  pipeline Script from SCM > Select type Git > Enter Git repository URL > click on Save


Jenkins Job is ready to build.

Step 5- Build and test Jenkins job:
choose to Build with parameters select your docker user and click on the build button.

After successfully complete, you will see output like this.
Login and Check pushed image in your Docker Hub account.

Build and push has completed successfully to Docker Hub. Based on this you build other customs Docker Image and push to your Docker hub account.

optionally, You can check My GitHub repository and view the dockerfile and Jenkinsfile pipeline.
https://github.com/amarsingh3d/jenkins-pipeline

Thanks for Reading blog!!

3 comments:

  1. Timely upgrading iot solutions was the main task due to which I had found the iot consulting services offered by this article to be the best provider.

    ReplyDelete
  2. Impressive blog, you explain very well in this article. I was searching for this kind of information. Now days it is very important to take right information from internet. Surely i am going to share this article with my friends. Also if you want to know about server hosting, i can help you.I am very impressed with this article, all the information I have found is very good, internet is a very convenient medium in which every information can be found in depth, this blog is very important
    You should know about USA VPS Hosting and how it can be important for this modern world. Thanks once again.

    ReplyDelete