DevOps, Day - 26

DevOps, Day - 26

Jenkins Declarative Pipeline

As we previously used a Jenkins Freestyle project, today we will be selecting a Jenkins Pipeline instead...

So first let's see what Jenkins's declarative pipeline means..!

  1. Pipeline:

    • A Pipeline is a set of connected tasks, like steps in a recipe, that automatically build and deploy your software.
  2. Declarative:

    • Declarative is the easy way to list those steps in plain language, making it clear and simple to understand.
  3. Scripted:

    • Scripted is a more technical way to list the steps, like writing a detailed plan in a specific coding language (Groovy).

Why You Should Have A Pipeline?

A Jenkins Pipeline is like a recipe for building and deploying software. It's written in a file (called a Jenkinsfile) that can be stored with your project's code.

When you create a Jenkinsfile and store it with your code, it brings several advantages:

  1. Automated Builds: It automatically sets up the process for building your software whenever changes are made to your code.

  2. Code Review: You can review and improve the build process, just like you review and improve your code.


Pipeline Syntax

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                sh 'echo "Building the software"'
                // Add your actual build commands here
            }
        }

        stage('Test') {
            steps {
                sh 'echo "Testing the software"'
                // Add your actual test commands here
            }
        }

        stage('Deploy') {
            steps {
                sh 'echo "Deploying the software"'
                // Add your actual deployment commands here
            }
        }
    }
}

Let's Complete the Hello World Example, by following the steps given in this documentation --> Link


Step 1: Click on localhost:8080

Step 2: Click on my blog link to install Jenkins --> link

Step 3: Create a New Jenkins Pipeline

  • Click on "New Item" on the Jenkins homepage.

  • Enter a name for your pipeline (e.g., "HelloWorld") --> Choose "Pipeline" as the project type --> Click "OK."

Step 4: Configure the Pipeline

  • In the pipeline configuration, scroll down to the "Pipeline" section.

  • Select the "Pipeline script" option.

  • Copy and paste the example pipeline code from the Jenkins official website into the script block.

Here's the code from the official example:

pipeline {
    agent any
    stages {
        stage('Hello') {
            steps {
                echo 'Hello, world!'
            }
        }
    }
}

Step 5: After saving, click --> Build Now

Step 6: Click on #1

Step 7: Here is the output...


Thank you so much for reading.

Follow me on LinkedIn to see interesting posts like this : )

Linkedin

Β