Jenkins- CI/CD Pipelines

Updated: Sep 1, 2020










What is a Jenkins Pipeline ?


According to the official documentation on jenkins.io


Jenkins Pipeline (or simply “Pipeline” with a capital “P”) is a suite of plugins which supports implementing and integrating continuous delivery pipelines 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. Every change to your software (committed in source control) goes through a complex process on its way to being released. This process involves building the software in a reliable and repeatable manner, as well as progressing the built software (called a “build”) through multiple stages of testing and deployment.


Pipeline provides an extensible set of tools for modeling simple-to-complex delivery pipelines “as code” via the Pipeline domain-specific language (DSL) syntax.

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


Pipeline Concepts


Pipeline


A Pipeline is a user-defined model of a CD pipeline. A Pipeline’s code defines your entire build process, which typically includes stages for building an application, testing it and then delivering it.Also, a pipeline block is a key part of DeclarativePipeline syntax.


Node


A node is a machine which is part of the Jenkins environment and is capable of executing a Pipeline. Also, a node block is a key part of Scripted Pipeline syntax.


Stage


A stage block defines a conceptually distinct subset of tasks performed through the entire Pipeline (e.g. “Build”, “Test” and “Deploy” stages), which is used by many plugins to visualize or present Jenkins Pipeline status/progress.e.g.

  • Build The Code

  • Test The Code

  • Deploy to Dev Environment

  • Deploy to Prod Environment

Stages are made up of steps that are discussed below.


Step


A single task. Fundamentally, a step tells Jenkins what to do at a particular point in time (or “step” in the process). For example, to execute the shell command make use the sh step: sh ‘make’. When a plugin extends the Pipeline DSL, that typically means the plugin has implemented a new step.

An Jenkinsfile is used to write code for the pipeline.It looks like below


pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Running build automation'
                sh './gradlew build --no-daemon'
                archiveArtifacts artifacts: 'dist/trainSchedule.zip'
            }
        }
    }


NodeJs App Deployment With Jenkins Pipeline


Now we are going to implement deployment of a nodejs app with the help of jenkins pipeline. For this we also need to have a git hub account so that we can keep the appcode and configure interaction between jenkins and github so that jenkins is able to fetch the code from repository and execute the pipeline.


Code can be cloned from the below commands


yum intall git -y 
git clone https://github.com/technoroots/devops-labs.git
  • Now we are going to login to our github account and create a personal access token

  • Login to your github account and click on the avatar on the top right hand corner

  • Click on settings and then click on Developer Settings

  • Now click on Personal Access Tokens and create one ( for demo purpose we can give full access to the token but this should definitely be controlled when working in production environment)

  • Remember to copy the token for sure as it will not be accessible again. If lost we need to create a new one.

  • Create a new jenkins project of type pipeline as shown below

Name it as MyTrainApp and select the project type to be MultiBranchPipeline . Steps are shown below.

1. Open jenkins homepage and click on New Item

2.Give the name as MyTrainApp and select the project as Multibranch Pipeline

3.Give the display name and project repository path. Select Branch Sources as git from the dropdown menu and not github.

4.Add the git hub credentials which will consist of the personal Access token that we created in the start of the article.

5.Select the credential that we created



6.As soon as we click OK , jenkins will fetch the code from the github repository and will start scanning it.

7.Our pipeline will start executing and its progress can be seen as below

Alright fellas , hope you enjoyed this series of tutorial on jenkins. Happy learning :)