Day 18 Jenkins part 3 ( Advanced | Declarative Pipeline)

Day 18 Jenkins part 3 ( Advanced | Declarative Pipeline)

What is 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 aJenkinsfile)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 aJenkinsfile 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/i*teration on the Pipeline (along with the remaining source code).*Pipeline syntax

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

Task-01

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

  • Follow the Official Jenkins Hello world example

  • Complete the example using the Declarative pipeline

Steps

There are 2 ways of doing it : Below is the First way of doing

  1. Install the Docker Pipeline plugin through the Manage Jenkins > Plugins page

  2. After installing the plugin, restart Jenkins so that the plugin is ready to use

  3. Copy one of the examples below into your repository and name it Jenkinsfile

Classic UI left column

Once the project is created, go to Pipeline section and paste the code

pipeline {
    agent { docker { image 'python:3.12.1-alpine3.19' } }
    stages {
        stage('build') {
            steps {
                sh 'python --version'
            }
        }
    }
}

And Simply Run the project manually, "Build Now"

Below is the Second way of doing

1 . Create a Repository in Github

Put the Jenkins file there, and the code to run the Jenkins pipeline

Jenkinsfile (Declarative Pipeline)
/* Requires the Docker Pipeline plugin */
pipeline {
    agent { docker { image 'python:3.12.1-alpine3.19' } }
    stages {
        stage('build') {
            steps {
                sh 'python --version'
            }
        }
    }
}

2 . Create another project in Jenkins with Configuration and select "Multi-Configuration Project"

3 . Give a Source Add the Github Repo where Jenkins file is there

Save and Run the Pipeline Manually

This example provides a simple introduction to Declarative Pipelines in Jenkins. You can further expand and customize your pipeline by adding more stages, steps, and integrations based on your project's requirements.