Jenkins Declarative Pipeline with project

Jenkins Declarative Pipeline with project

click the below link for Jenkins basic and freestyle projects:

https://devopswithnilesh.hashnode.dev/mastering-jenkins-a-comprehensive-guide

What is a Pipeline - A pipeline is a collection of steps or jobs interlinked in a sequence.

Declarative: Declarative is a more recent and advanced implementation of a pipeline as a code.

Scripted: Scripted was the first and most traditional implementation of the pipeline as a code in Jenkins. It was designed as a general-purpose DSL (Domain Specific Language) built with Groovy.

Why you should have a Pipeline

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.
This is the foundation of "Pipeline-as-code"; treating the CD pipeline as a part of the application to be versioned and reviewed like any other code.

Creating a Jenkinsfile and committing it to source control provides a number of immediate benefits:

  • Automatically creates a Pipeline build process for all branches and pull requests.

  • Code review/iteration on the Pipeline (along with the remaining source code).

Pipeline syntax

COPY

pipeline {
    agent any 
    stages {
        stage('Build') { 
            steps {
                // 
            }
        }
        stage('Test') { 
            steps {
                // 
            }
        }
        stage('Deploy') { 
            steps {
                // 
            }
        }
    }
}

Project 01:-

Create a New Job, this time select Pipeline instead of Freestyle Project.

Create a Jenkins declarative pipeline select new items > pipeline and give the pipeline name.

  • Pipeline:

    The Declarative pipeline should start with the pipeline block and this is the mandatory block.

    Agent:

    Agent signifies where the Jenkins build job should run. In this case, we have selected agents as any.

    Stages:

    stages block consists of different executable stage blocks. we have name stage “Hello”.

    Steps:

    Steps blocks consist of the actual operation which needs to be performed inside Jenkins.

    we are printing “Hello World“.

You can manually build the project by clicking on "Build Now".

Project 2 :- Jenkins Declarative Pipeline with Docker

In this project we will deploy applications in docker container

prerequisite:-

  1. docker and docker-compose installed in machine

  2. Jenkins to access docker (sudo usermod -aG docker Jenkins)

How will the pipeline look

pipeline {
  agent any  
    stages{
        stage("code"){
            steps{
              echo "code cloned"
              git url: 'https://github.com/Niljay892/node-todo-cicd.git' , branch: 'master'
            }
        }
        stage("code build"){
            steps{
              echo "code build"
              sh 'docker build . -t node-app-pipe'    
            }
        }
        stage("code test"){
            steps{
              echo "code test"
            }
        }
        stage("code deploy"){
            steps{
              echo "code deploy"
            }
        }
    }
  • Create a docker-integrated Jenkins declarative pipeline

    Click on new-items > Pipeline project type >Name the pipeline

    • Save and run the pipeline.

    • You should see the pipeline execute each stage and run your application inside a Docker container.

Project 2:- Deployment using Jenkins pipeline in docker container using Jenkins agent.

step 1:-For jenkins agent setup click the below link:-

https://devopswithnilesh.hashnode.dev/setting-up-jenkins-master-node-on-aws

step 2:-

  • Create a docker-integrated Jenkins declarative pipeline

    Click on new-items > Pipeline project type >Name the pipeline

    now we have write pipeline before that if you want to push docker image in docker hub then below process needs to be done in Jenkins for docker login

    • goto Dashboard>Manage jenkins> security > credentials

      click on system

      click on add credentials

      select the option as shown and then provide the docker username,password and id( refers as credential id) as there can be multiple credentials that we can use in Jenkins

  • save credential and in pipe we use below syntax to login docker using groovy syntax

    1.      steps{
                              echo "logging and code push in docker hub"
                              withCredentials([usernamePassword(credentialId:"dockerHub" ,passwordVariable:"dockerHubPassword" ,usernameVariable:"dockerHubUser")]){
                              sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPassword}"
                              sh "docker tag my-note-app nileshops/my-note-app:latest"
                              sh "docker push nileshops/my-note-app:latest"
                            }
      

pipeline:-

How will the pipeline look

             pipeline {
                 agent { label 'dev' }

                 stages{
                     stage('code'){
                         steps{
                                 git url: 'https://github.com/Niljay892/django-notes-app.git' , branch: 'main'
                         }
                     }    
                     stage("code build"){
                         steps{
                           echo "code build"
                           sh 'docker build . -t my-note-app'    
                         }
                     }
                     stage("login and push image"){
                         steps{
                                 echo "logging and code push in docker hub"
                                 withCredentials([usernamePassword(credentialsId:"dockerHub" ,passwordVariable:"dockerHubPassword" ,usernameVariable:"dockerHubUser")]){
                                 sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPassword}"
                                 sh "docker tag my-note-app nileshops/my-note-app:latest"
                                 sh "docker push nileshops/my-note-app:latest"
                               } 
                         }
                     }
                     stage("code deploy"){
                         steps{
                           echo "code deploy"
                           sh 'docker-compose down && docker-compose up -d'
                         }
                     }


                 }
             }
  • after saving job , build now and below are the results

Project :

use below git url and same can be deployed using docker.

https://github.com/Niljay892/node-todo-cicd.git

I hope you like this article.

Happy learning!