Getting Started With Jenkins Declarative Pipeline

Getting Started With Jenkins Declarative Pipeline

·

5 min read

📜 Some basic terms for understanding

What is a Pipeline ⁉️

  • A pipeline is a collection of steps or jobs interlinked in a sequence.

  • Example: Imagine you're baking a cake. You have a step-by-step process: mixing ingredients, baking, and decorating. A pipeline is like this process, but for software. It's a set of automated steps that take your code and handle things like testing and putting it live. This way, changes to your code are tested and deployed consistently.

What do you mean by Declarative pipeline⁉️

  • Declarative is a more recent and advanced implementation of a pipeline as a code.

  • Example: Think of this like a recipe card with simple instructions. It's a modern way to describe your software pipeline. You write down the steps in an easy-to-read way, focusing on what each step does. It's like following a straightforward recipe without getting too fancy.

What is a Scripted Pipeline⁉️

  • Scripted was the first and most traditional implementation of the pipeline as a code in Jenkins. It was designed as a general-purpose DSL (Domain Specific Language) built with Groovy.

  • Example: This is like being a skilled chef who can create recipes from scratch. With Scripted Pipelines, you have more control and freedom. You can write detailed instructions using a special scripting language. It's like crafting a recipe exactly the way you want it, but it might get a bit complex.

What is the difference between declarative and scripted pipeline⁉️

  • Both Declarative and Scripted Pipelines help automate the process of making and delivering software. Declarative is like using a simple recipe card, while Scripted gives you the power to create a custom recipe. Which one you choose depends on how complicated your software process is and how comfortable you are with writing special instructions.

📜 Why you should have a Pipeline🤔

Let's understand this by comparing without pipeline and with pipeline:

Imagine you're building a mobile app that lets users create and share to-do lists. You and your team are constantly adding new features and fixing bugs. Without a pipeline, your process might look something like this:

Without a Pipeline:

  1. A developer makes changes to the code.

  2. They manually build the app on their local machine.

  3. They do some testing, but it's hard to cover all scenarios.

  4. If everything seems okay, they manually upload the app to a server for deployment.

  5. Sometimes it works fine, but other times the app breaks in unexpected ways.

  6. You spend a lot of time troubleshooting and fixing issues in production.

  7. The process is slow, error-prone, and you're not sure which version of the app is running where.

Now, let's see how having a pipeline improves this situation:

With a Pipeline:

  1. A developer makes changes to the code and pushes them to the version control system (e.g., Git).

  2. The pipeline automatically detects the code changes and starts the process.

  3. The pipeline first builds the app, converting the code into a working application.

  4. Automated tests run on the built app to check for bugs and issues.

  5. If the tests pass, the pipeline deploys the app to a testing environment.

  6. Automated tests run again in the testing environment to catch any environment-specific issues.

  7. If all tests pass, the pipeline can deploy the app to a staging environment for final testing.

  8. Once the app is thoroughly tested in the staging environment, the pipeline can deploy it to the production environment for users to access.

  9. The entire process is automated, repeatable, and consistent.

In this example, a pipeline streamlines your development and deployment process, making it faster, more reliable, and less error-prone. It's like having a conveyor belt that takes your code through various checks and stages before it reaches your users.

How to create a Jenkins pipeline🤔⁉️

The definition of a Jenkins Pipeline is written into a text file (called a Jenkinsfile) which in turn can be committed to a project’s source control repository. This is the foundation of "Pipeline-as-code"; treating the CD pipeline as a part of the application to be versioned and reviewed like any other code.

In the context of Jenkins, pipelines are defined using the Groovy scripting language. Groovy is a dynamic programming language that runs on the Java Virtual Machine (JVM), which makes it suitable for integration with Java-based tools and frameworks like Jenkins

Creating a Jenkinsfile and committing it to source control provides several immediate benefits:

  • Automatically creates a Pipeline build process for all branches and pull requests.

  • Code review/iteration on the Pipeline (along with the remaining source code).

Pipeline Syntax:

pipeline {
    agent any 
    stages {
        stage('Build') { 
            steps {
                // 
            }
        }
        stage('Test') { 
            steps {
                // 
            }
        }
        stage('Deploy') { 
            steps {
                // 
            }
        }
    }
}

Hands-on💻

  • 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

Pre-requisite:

  • Make sure you install docker otherwise it will be line DOCKER ❓ WHO❓and add user, jenkins to docker group by using sudo usermod -aG docker $USER , sudo usermod -aG docker jenkins

  • And install docker pipeline plugin from manage jenkins>plug in >Available plugin

Step 1 : From your jenkins dashboard click on create job/ new item( If you don't know how to do that it's never late , you can refer this ) , then give name to your job and select pipeline .

Step 2: Give a description as you want , scroll down under pipeline script , paste / write your script , save it and click on build now.

you can refer Hello world example for the basic scripts

That's it..!! you have created your first pipeline

You can click on build number (left hand side bottom) for console output and pipeline steps .

You can add your github repo in the step 2 where you give a description , Jenkins will automatically detect any new Branches or Pull Requests that are created in your repository and start running Pipelines for them.

I hope now you can create your pipelines !!

Will learn more about them in further blogs

Stay tuned !!

Happy Learning :D