December 23, 2024

Introduction:

In this rapidly evolving world it has become very neccessary to streamline the process of application deployment and ship our application qucikly to test and deploy the changes globally,

Jenkins is one such kind of an open source tool that is most widely used to continously integrate and continously deliver our application as and when new change is being committed by the developer in our code repository.

So, In this blog we will be looking at how to create a pipeline in jenkins using jenkinsfile, understand the syntax in jenkinsfile and trigger the pipeline on a scheduled basis to deploy the changes.

Getting Started:

Step 1: Fork the repository from here.

Step 2: Understanding Jenkinsfile

  1. pipeline { } : This is the top level of our pipeline and all the stages will be residing under this directive.
  2. agent any : This directive contains the name of our node where our pipeline will be executed. If you have agent nodes for your jenkins server then you can mention it here to execute the pipeline on that node. You can also define agent at individual stage level to perform stages steps.
  3. stages { } : This directive contains one or more different stage for our pipeline. For example: build, test and deploy.
  4. stage(‘build stage’) { ) : This directive comes under stages directive and we need to mention the name of our stage inside curly braces. All the work done in the pipelines or steps performed comes under this directive.
  5. steps { } : This section defines a series of one or more steps to be executed in a given stage directive.
  6. input { } : This directive allows you to add a prompt in your pipeline. It will stop the execution of your pipeline stage and wait for the input from the submitter. Our input contains message, ok and submitter field. Message: Message to be asked to the user, ok: Message to displayed to the user to perform next action, submitter: Name of the user who is allowed to give the input to this prompt.
  7. sh ‘npm install’ : To execute shell commands.
  8. git branch: ‘main’, url: ‘URL’ : This instruct jenkins to work with git directive with the specified branch and git url.

For more information check this url from official jenkins documentation.

pipeline {
    agent any
    stages {
        stage('Clone Repository'){
            steps {
                git branch: 'main', url: 'https://github.com/pankaj-Makhijani/Nodejs-hello-world.git'
            }
        }
        stage('Install Dependencies'){
            steps {
                sh 'npm install'
            }
        }
        stage('Perform API testing'){
            steps {
                sh 'npm test'
            }
        }
        stage('Start Application'){
            input {
                message "Should we continue?"
                ok "Yes, we should."
                submitter "pankaj"
            }
            steps {
                sh 'npm start'
            }
        }
    }
}

Step 3: Go to Jenkins Dashboard and create a pipeline project

Step 4: Configure Pipeline Project

Under general, give description for your pipeline. Under build triggers check the Poll SCM and enter the schedule for timeframe when you want to perform polling

“Poll SCM” polls the SCM periodically for checking if any changes/ new commits were made and shall build the project if any new commits were pushed since the last build.

You can use this tool to get a schedule syntax for your custom time period.

Under pipeline, select “Pipeline script from SCM” as Definition, “Git” as SCM, enter your repository URL and keep everything as it is.

Enter “*/main” under branch specifier and “Jenkinsfile” under scripts path. Your pipeline is now configured next click on Save and Apply.

Step 5: It’s time to test our pipeline so head over to your git repository and make some changes in your file. Once changes are made you can see build is being prepared under build status on the bottom right side.

Click on your build and head over to console output to view the steps being performed in your pipeline. You can see that build has reached the “Start application stage” and waiting for the input by the submitter mentioned in our jenkinsfile.

On the right side of your console output you could see “Paused for input”. Click on “Paused for input” and click “Yes, you should” to proceed the build further in the pipeline stage.

Now you can see that our pipeline stage was approved by the submitter and our application is started successfully. 🎉

Conclusion:

I hope you are now familiar with how to write jenkinsfile and steamline the process of application deployment. Though it was a simple hello world application and simple jenkinsfile with basic stage but I hope it gave a overview on how to proceed with your real world application.

Leave a Reply

Your email address will not be published. Required fields are marked *