Day 26 of #90DaysOfDevops || Jenkins Declarative Pipeline

Day 26 of #90DaysOfDevops || Jenkins Declarative Pipeline

·

2 min read

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

pipeline{
    agent{
        label "node"
    }
    stages{
        stage("A"){
            steps{
                echo "========executing A========"
            }
            post{
                always{
                    echo "========always========"
                }
                success{
                    echo "========A executed successfully========"
                }
                failure{
                    echo "========A execution failed========"
                }
            }
        }
    }
    post{
        always{
            echo "========always========"
        }
        success{
            echo "========pipeline executed successfully ========"
        }
        failure{
            echo "========pipeline execution failed========"
        }
    }
}

Task-01

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

  • Complete the example using the Declarative pipeline


  • Use this project. Complete Full stack project. To check how to setup and install this project Readme file is provided. To know how to create docker file and docker compose file visit this link

  • Now we need to create declarative pipeline for the build of this project.

  • So we create new item and name it and choose pipeline

  • Next we fill up details and provide github url and also in this iam creating webhooks. To know more about webhooks and github push trigger visist this blog

  • And in the pipeline select Git SCM and there we need to put github repo link and use credentials of github if it is not public repository. To setup credentials visit here

  • Now we provide the Jenkinsfile name in the same

Now we will write down jenkinsfile on our local system in the project as:

pipeline {
    agent any

    stages {
        stage("Docker compose") {
            steps{
                sh '''
                ls
                sudo docker compose down
                sudo docker compose up -d
                sudo docker compose ps
                '''
            }
        }

    }
    post{
        always{
            echo "========always========"
        }
        success{
            echo "========pipeline executed successfully ========"
        }
        failure{
            echo "========pipeline execution failed========"
        }
    }
}
  • Then we push this Jenkinsfile to github from local and on puch it will automatically trigger the build as we have setup github push trogger.

End of Post


Reach me on:

  1. Github-> github.com/Hrmn97

  2. Twitter -> twitter.com/Harman9765

  3. LinkedIn -> linkedin.com/in/chetan-harman-56310424a

  4. Website -> devhrmn.netlify.app