Skip to main content

Day 9 – Jenkins Pipeline Basics (Declarative) | #30DaysOfDevOps

Published 3 min readMarkdown

Series: 30 Days DevOps Interview Preparation

📌 Introduction

Jenkins has evolved far beyond simple Freestyle jobs. Modern DevOps teams rely on Jenkins Pipelines because they provide automation as code, integrate seamlessly with version control, and allow for complex workflows.

Today, we’ll dive into Declarative Pipelines — the beginner-friendly, structured syntax that makes CI/CD both readable and maintainable.


1. What is a Jenkins Pipeline?

A Jenkins Pipeline is a set of plugins that lets you define your build, test, and deployment process in a Jenkinsfile.

Key benefits:

  • CI/CD as code — version-controlled in Git

  • Modular stages (Build → Test → Deploy)

  • Easy rollback & history tracking

  • Integrates with multiple tools (Docker, Kubernetes, AWS, GCP, etc.)


2. Declarative vs Scripted Pipelines

FeatureDeclarativeScripted
SyntaxStructured pipeline {} blockPure Groovy
ReadabilityHighMedium (Groovy knowledge needed)
Error handlingBuilt-inManual
FlexibilityModerateHigh
Best for beginners?✅ Yes❌ No

3. Basic Structure of a Declarative Pipeline

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                echo 'Building application...'
            }
        }

        stage('Test') {
            steps {
                echo 'Running tests...'
            }
        }

        stage('Deploy') {
            steps {
                echo 'Deploying application...'
            }
        }
    }

    post {
        success {
            echo 'Pipeline completed successfully!'
        }
        failure {
            echo 'Pipeline failed!'
        }
    }
}

4. Practical: Setting Up a Declarative Pipeline on AWS EC2

Step 1: Launch Jenkins

  • Start an AWS EC2 Ubuntu instance

  • Install Jenkins and Java

  • Open Jenkins UI on http://<EC2-IP>:8080

Step 2: Create a New Pipeline Job

  • New Item → Pipeline → OK

  • In the Pipeline script section, paste your Jenkinsfile code.

Step 3: Save & Build

  • Click Build Now and watch the console output.

5. Adding Real Commands

Example for a Java Maven project:

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                sh 'mvn clean install'
            }
        }

        stage('Test') {
            steps {
                sh 'mvn test'
            }
        }

        stage('Deploy') {
            steps {
                sh '''
                echo "Deploying to server..."
                scp -i /path/key.pem target/myapp.jar ec2-user@<EC2-IP>:/home/ec2-user/
                '''
            }
        }
    }
}

6. Common Declarative Pipeline Questions & Answers

Q1: What is the difference between Declarative and Scripted Pipelines?

Declarative has a strict, readable syntax inside a pipeline {} block, while Scripted is written entirely in Groovy and is more flexible but harder to maintain.

Q2: Why store Jenkinsfile in Git?

Version control allows tracking changes, easy rollback, collaboration, and integration with Git hooks for automation.

Q3: What is agent in Jenkins pipelines?

The agent directive tells Jenkins where to run the pipeline (any, specific label, or a Docker container).

Q4: How do you handle errors in Declarative pipelines?

Use the post block with failure {} or always {} to define actions after a build failure.

Q5: How do you run a pipeline on a specific node?

agent { label 'my-node' }

Q6: How do you parameterize a pipeline?

parameters {
    string(name: 'VERSION', defaultValue: '1.0', description: 'App version')
}

7. Best Practices

  • Always use pipeline {} structure for clarity

  • Keep build, test, and deploy steps in separate stages

  • Store Jenkinsfile in the repo root

  • Use parameters for flexibility

  • Use post block for notifications and cleanup


✅ Day 9 takeaway: Declarative Pipelines make CI/CD more maintainable, readable, and collaborative — a must-have skill for any DevOps Engineer.


#DevOps #Jenkins #CICD #Pipelines #InterviewPreparation #30DaysOfDevOps

No comments yet. Be the first to comment.

More from this blog