Skip to content

Latest commit

 

History

History
189 lines (145 loc) · 5.29 KB

introduction.md

File metadata and controls

189 lines (145 loc) · 5.29 KB
sidebar_position title
1
Jenkins Introduction

Jenkins is an open source automation server. It helps automate the parts of software development related to building, testing, and deploying, facilitating continuous integration and continuous delivery.

Jenkinsfile - Pipeline as Code

Insted of configuring the pipeline in the Jenkins UI, we can define the entire pipeline in a Jenkinsfile and check it into source control. The file name is case sensitive and must be named Jenkinsfile.

This pipeline can be written in two ways:

Scripted Pipeline

Scripted Pipeline is based on Groovy syntax. It is a general-purpose programming language with a syntax similar to Java, but with additional features that support common programming idioms.

node {
    stage('Build') {
        sh 'npm install'
        echo 'Building..'
    }
    stage('Test') {
        echo 'Testing..'
    }
    stage('Deploy') {
        echo 'Deploying..'
    }
}

Declarative Pipeline

Declarative Pipeline is a new way of defining the entire pipeline using a simple and easy to understand structure.

pipeline {
    agent any 
    stages {
        stage('Build') {
            steps {
                sh npm install
                echo 'Building..'
            }
        }
        stage('Test') {
            steps {
                echo 'Testing..'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying..'
            }
        }
    }
}
  • Pipeline: The top-level directive that defines the entire pipeline.
  • Agent: The agent directive defines where the entire pipeline, or a specific stage, will execute in the Jenkins environment.
  • Stages: The stages directive defines the different stages in pipeline.
  • Stage: The stage directive defines a single stage in a pipeline.
  • Steps: The steps directive defines the steps to be executed in a stage.

Post Step:

The post section is used to define actions to be taken after the completion of the pipeline. It can be used to send notifications, sccess/failure messages, etc.

stages{}
post{
    always {
        sh 'echo pipeline completed'
    }
    success {
        sh 'echo pipeline sucess'
    }
    failure {
        sh 'echo pipeline failed'
    }
}

When:

The when directive is used to control the flow of the pipeline based on the conditions. It can be used to skip stages, or entire pipelines, based on the conditions.

when{
    expression { BRANCH_NAME == 'main' }
}
stage('Building the image') {
    when{
    expression { BRANCH_NAME == 'main' }
    }
    steps {
    sh 'docker build -t pradumnasaraf/devops:latest .'
    }
}

Environment Variables and Credentials

To check default environment variables in Jenkins, we can use the following URL:

http://<host>/env-vars.html
http://localhost:8080/env-vars.html

Buils Tools

The tools directive is used to define the tools required for the pipeline. It can be used to define the JDK, Maven and Gradle tools.

With the 1st approach we can directly use the tools and its commands in the pipeline.

tools {
    maven 'maven-3.6.3'
    jdk 'jdk-11'
}

We can also follow wrapper approach to define the tools.

stage('Build') {
    steps {
        withMaven(maven: 'maven-3.6.3') {
            sh 'mvn clean install'
        }
    }
}
stage('Build') {
    steps {
        nodejs('node-12.18.3') { // nodejs is the name of the tool
            sh 'npm install'
        }
    }
}

Note: The tools must be installed in Jenkins. To install the tools, go to Manage Jenkins > Global Tool Configuration.

Parameters

The parameters directive is used to define the parameters required for the pipeline. It can be used to define the parameters like string, boolean, choice, etc. we can check the parameters in the UI by clicking on the Build with Parameters button.

parameters {
    string(name: 'NAME', defaultValue: 'pradumnasaraf', description: 'Enter your name')
    booleanParam(name: 'DEBUG', defaultValue: true, description: 'Enable debug mode')
    choice(name: 'VERSION', choices: ['1.0', '2.0', '3.0'], description: 'Select version')
}
stage('Build') { 
    steps {
        echo "Hello ${params.NAME}" //Accessing the parameters by params.{parameter_name}
        echo "Debug mode is ${params.DEBUG}"
        echo "Version is ${params.VERSION}"
    }
}

Screenshot 2023-01-07 at 1 31 11 PM

Triggers

The triggers directive is used to define the triggers for the pipeline. Common way are, poll, github webhooks, etc.

Screenshot 2023-01-08 at 12 40 23 AM

Replay

The replay option is used to re-run the pipeline. It is useful to test withou making/committing any changes to the code.

Screenshot 2023-01-07 at 2 18 28 PM

What's next?