Day 26 Task: Jenkins Declarative Pipeline

Day 26 Task: Jenkins Declarative Pipeline

ยท

3 min read

This is #90DaysofDevops challenge under the guidance of Shubham Londhe.

Day 26 TASK

  • Welcome to another day in our DevOps journey! Today, we're exploring Jenkins Declarative Pipeline, a powerful tool that makes your coding life easier.

What's a Pipeline, Anyway?

  • Think of a pipeline as a recipe for your software. It tells Jenkins what steps to follow in order to build, test, and deploy your code smoothly.

Understanding Declarative vs. Scripted

#5 Jenkins Pipelines | Declarative vs scripted Syntax - YouTube

Jenkins gives you two ways to write pipelines: Declarative and Scripted.

  • Declarative: It's the newer, simpler way to write pipelines. It's like following a clear set of instructions.

  • Scripted: This is the older method. It's more flexible but can be a bit like solving a puzzle.

Why Do You Need a Pipeline?

  • Having a Jenkinsfile (that's the name of the file where you write your pipeline) is like having a map for your software journey. It helps Jenkins know what to do with your code.

Here's why it's awesome:

  • Automatic Pipeline Creation: Jenkins can set up a pipeline for every branch and pull request automatically.

  • Code Harmony: Treat your pipeline script like a treasure. Review it, improve it, and keep your codebase happy.

Decoding the Pipeline Language

  • Let's break down the Declarative Pipeline language:
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                // This is where you build your code
            }
        }
        stage('Test') {
            steps {
                // Here, you test your code
            }
        }
        stage('Deploy') {
            steps {
                // And here, you deploy your code
            }
        }
    }
}

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.

    1. Set Up Your Environment
  • Create an EC2 instance and install Jenkins on it.

  • Access Jenkins using the public IP of the EC2 instance and port 8080.

    1. Create a New Pipeline Job

      • Open the Jenkins Dashboard and select "New Item."

      • Add a Project Name and choose the project type as "Pipeline" since we'll be creating a Jenkinsfile.

  • Click "OK" to proceed to the Project Configuration section.

    1. Configure Your Pipeline
  • In the Project Configuration section, navigate to the "Pipeline" section.

  • Select "Pipeline script" as the definition.

  • Now, let's write a basic Pipeline Script for our "Hello World" example.

    1. Write Your Pipeline Script
    pipeline {
      agent any
      stages {
          stage('Hello') {
              steps {
                  echo 'Hello World'
              }
          }
      }
    }
    

  • Here's a quick breakdown of the script:

    • Pipeline: The Declarative pipeline must start with the pipeline block.

    • Agent: Specifies where the Jenkins build job should run. In this case, we've selected any.

    • Stages/Stage: Contains different executable stage blocks. At least one stage block is mandatory. Here, we've named the stage as "Hello."

    • Steps: Consists of the actual operations to be performed inside Jenkins. In our example, we're printing "Hello World."

    1. Run Your Pipeline
  • Save the Pipeline configuration.

  • Click on the "Build Now" tab to start the build process manually.

  • Once the build is complete, check the output by clicking on the "Console Output" tab. You should see "Hello World" printed.

  • You can also view the multi-stage view by clicking on "Full Stage View" in the project main page.

    Feel free to drop any questions in the comments below. I'm here to help!

    If you found this post helpful and informative, please consider showing your support by following and clicking the clap ๐Ÿ‘ button below. Your feedback and encouragement mean a lot to us as we strive to provide valuable content to our readers.

    Thank you for reading and engaging with our posts! ๐Ÿ’š

Did you find this article valuable?

Support Akash Dhengale by becoming a sponsor. Any amount is appreciated!

ย