Jenkins pipeline

·

5 min read

Cover Image for Jenkins pipeline

In this document, I am sharing some of my learnings with Jenkins Pipeline.


What is jenkins pipeline : -

Form my understanding jenkins pipeline is way of making all thing automatic .

Formal defination is : - Jenkins Pipeline (or simply "Pipeline" with a capital "P") is a suite of plugins which supports implementing and integrating continuous delivery pipelines into Jenkins.

to understand pipeline i watch some youtube video , blogs and used chatGPT as teacher. so through out learning i made Jenkina server on EC2 and my VM , i created diffrend job , i shere that in below.


Key Components of jenkins Pipeline :-

  1. Pipeline

    • The root block.

    • Defines the entire CI/CD workflow.

    pipeline {
        ...
    }
  1. Agent

    • Defines where the pipeline (or a stage) should run.

    • Examples:

      • agent any → Run on any available Jenkins node.

      • agent none → Disable default agent (stages define their own agents).

      • agent { label 'linux' } → Run on a specific node with label linux.

  2. Stages

    • Logical sections of the pipeline (e.g., Build, Test, Deploy).

    • Used for organizing and visualizing in Jenkins UI.

    stages {
        stage('Build') { ... }
        stage('Test') { ... }
    }
  1. Steps

    • The actual commands/tasks inside a stage.

    • Examples:

      • sh 'mvn clean install' (Linux shell command)

      • bat 'gradlew.bat build' (Windows batch command)

      • echo "Hello World"

  2. Post

    • Defines actions to run at the end of the pipeline or stage.

    • Useful for cleanup, notifications, archiving logs, etc.

    post {
        success {
            echo 'Pipeline succeeded!'
        }
        failure {
            echo 'Pipeline failed!'
        }
    }
  1. Environment

    • Used to set global or stage-specific environment variables.
    environment {
        JAVA_HOME = "/usr/lib/jvm/java-11"
        APP_ENV = "staging"
    }
  1. Parameters

    • Allows pipelines to take user inputs (useful for different environments or versions).
    parameters {
        string(name: 'BRANCH', defaultValue: 'main', description: 'Git branch to build')
        choice(name: 'ENV', choices: ['DEV', 'STAGING', 'PROD'], description: 'Environment')
    }
  1. Triggers

    • Defines what automatically starts the pipeline.
    triggers {
        cron('H/15 * * * *')    // Runs every 15 minutes
        pollSCM('* * * * *')    // Polls SCM every minute
    }
  1. Options

    • Configures pipeline-level settings (like timeouts, retries, etc.).
    options {
        timeout(time: 1, unit: 'HOURS')
        buildDiscarder(logRotator(numToKeepStr: '10'))
    }
  1. Input

    • Pause the pipeline and wait for manual approval or input.
    input {
        message "Deploy to production?"
        ok "Yes, Deploy"
    }

Above all are components of a pipeline, and I used some of them in my Jenkins pipeline job.


All job i created on my VM

In the Job - Parameter and pipeline

  1. Agent any

    • The pipeline can run on any available Jenkins agent (no restriction to a specific node).
  1. Parameters Section

    • This lets you pass inputs when starting the job.

    • Parameters defined:

      • ENVIRONMENT (choice parameter) → User selects DEVELOPMENT / STAGING / PRODUCTION.

      • APIKEY (password parameter) → Securely input API key (masked in logs).

      • CHANGELOG (string parameter) → Free text input (default: "This is the change log.").

✅ This makes your pipeline dynamic & reusable depending on user input.

  1. Stages

    • Stage: Test

        echo "This step tests the project"
      

      → Just prints a test message (in real life, you’d run unit tests, integration tests, etc.).

    • Stage: Deploy

        when {
            expression { params.ENVIRONMENT == "PRODUCTION" }
        }
      

      → This stage only runs if ENVIRONMENT = PRODUCTION.
      → If you selected DEVELOPMENT or STAGING, this stage will be skipped.

      ✅ This adds conditional execution → safer deployment control.

    • Stage: Report

        sh "echo '${params.CHANGELOG}' > ${params.ENVIRONMENT}_report.txt"
        archiveArtifacts artifacts: '*.txt', fingerprint: true
      

      → Creates a text report file named like:

      • DEVELOPMENT_report.txt

      • STAGING_report.txt

      • PRODUCTION_report.txt
        → The file contains the CHANGELOG message entered as a parameter.
        archiveArtifacts stores that file in Jenkins so it can be downloaded later.

This is how you’re generating & archiving artifacts.

Here is the output -


Next i careted job - SSH Agent

  1. Agent Selection

     agent { label 'dev' }
    
    • The pipeline runs on a Jenkins agent/node that has the label dev.

    • Ensures that builds only run on your chosen machine/environment.

  1. Tools Section

     tools {
         maven 'maven-3.9.11'
     }
    
    • Tells Jenkins to use the configured Maven installation (v3.9.11).

    • This means all mvn commands in your pipeline will use that Maven.

  1. Stage: Source

     sh 'mvn --version'
     sh 'git --version'
     git branch: 'main',
         url: 'https://github.com/LinkedInLearning/essential-jenkins-2468076.git'
    
    • Verifies Maven and Git are installed on the agent.

    • Pulls code from the GitHub repository (main branch).
      ✅ This is your source checkout stage.

  1. Stage: Clean

     dir("${env.WORKSPACE}/Ch04/04_02-ssh-agent") {
         sh 'mvn clean'
     }
    
    • Moves into the project’s subdirectory.

    • Runs mvn clean → deletes target/ folder, removing old compiled classes and artifacts.
      ✅ Ensures a fresh build.

  1. Stage: Test

     dir("${env.WORKSPACE}/Ch04/04_02-ssh-agent") {
         sh 'mvn test -Dmaven.compiler.source=17 -Dmaven.compiler.target=17'
     }
    
    • Runs unit tests for the project.

    • Explicitly sets Java compiler level to 17.
      ✅ Validates your code with automated tests.

  1. Stage: Package

     dir("${env.WORKSPACE}/Ch04/04_02-ssh-agent") {
         sh 'mvn package -DskipTests -Dmaven.compiler.source=17 -Dmaven.compiler.target=17'
     }
    
    • Runs mvn package to create a JAR/WAR file.

    • Skips tests in this stage (since you already ran them in the "Test" stage).

    • Output is stored in target/ folder.
      ✅ Produces the build artifact.

Here is the Output -

Here i am Using Github socrce repo - https://github.com/DheerenGaud/Jenkins-learning


in the next Docs i will cover my EC2 intsace job also