2. Jenkins Declarative Pipeline: Automating CI/CD with Code

2. Jenkins Declarative Pipeline: Automating CI/CD with Code

ยท

2 min read

What is a Jenkins Declarative Pipeline?

A Declarative Pipeline is a structured way to define Jenkins Pipelines using a predefined format. It follows a scripted syntax but is easier to read and maintain compared to traditional scripted pipelines.

๐Ÿ“ Creating a Jenkins Declarative Pipeline

Step 1: Create a New Pipeline Job

  1. Open Jenkins Dashboard.

  2. Click New Item

  3. Enter a name for the job.

  4. Select Pipeline and click OK

Step 2: Define the Pipeline Script

In the Pipeline Definition section, select Pipeline Script and enter the following:

pipeline {
    agent any
    stages {
        stage('Hello') {
            steps {
                echo 'Hello World...'

            }
        }
        stage('Create Folder') {
            steps {
                echo 'creating folder...'
                sh 'mkdir -p devops'
            }
        }
        stage('Bye') {
            steps {
                echo 'Bye...'
            }
        }
    }
}

Step 3: Save and Run the Pipeline

  1. Click Save

  2. Click Build Now to execute the pipeline

๐Ÿ“ Advantages of Declarative Pipelines

  • Easier to read and maintain due to structured syntax.

  • Supports version control by storing pipelines in a Jenkinsfile.

  • Error handling with built-in validation.

  • Modular stages make debugging easier.


๐Ÿ“ Freestyle Project vs. Declarative Pipeline: Which One to Use?

FeatureFreestyle ProjectDeclarative Pipeline
Configuration MethodUI-basedCode (Jenkinsfile)
ComplexityLowMedium-High
Version ControlLimitedFully supported
FlexibilityBasic stepsAdvanced workflows
Recommended ForSmall projects, beginnersLarge projects, teams with CI/CD experience

Conclusion

Jenkins Freestyle Projects provide an easy way to automate build and deployment tasks with minimal setup, while Declarative Pipelines offer a scalable, version-controlled approach for CI/CD workflows. Understanding both methods allows you to choose the best fit for your project.

๐Ÿš€ Stay tuned for upcoming blogs on Multibranch Pipelines and Jenkinsfile Best Practices!

ย