Monday, February 4, 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!!

5 comments:

  1. Thank you so much for this nice information. Hope so many people will get aware of this and useful as well. And please keep update like this.

    Big Data Services

    Data Lake Services

    Advanced Analytics Solutions

    Full Stack Development Services

    ReplyDelete
  2. 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
  3. Nice Article!! I like it and thanks for sharing that wonderful information. Thank you
    E- Learning Training Portal
    Portal- ELearning
    DevOps Online Hub
    E-learning Portal

    ReplyDelete
  4. As we know there are many companies which are converting into Automated big data engineering. with the right direction we can definitely predict the future.

    ReplyDelete

Note: Only a member of this blog may post a comment.

How to Setup Chef Workstation, Hosted Chef Server and Configure Node using Chef cookbook

Chef:   Chef is a powerful tool for automation that transforms infrastructure into code. Whether you're operating in the cloud or on-pre...