Open In App

What Is Jenkins Declarative Pipeline ?

Last Updated : 20 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Jenkins Declarative Pipeline is a more recent addition to Jenkins as a code pipeline definition approach. It offers a more simplified and structured way of defining your pipelines compared to the traditional scripted pipeline syntax.

Syntax Of Jenkins Declarative Pipeline To Build & Push Docker Image

The following Declarative Pipeline can be used to build and docker push the docker image to DockerHub.

pipeline {
agent any

tools {
// Specify Maven tool with version 3.8.6
maven "maven 3.8.6"
}

stages {
stage('Code Checkout') {
steps {
// Git checkout step
git credentialsId: '<Git Credentials>', url: '<GitHub URL>'
}
}

stage('Building the Code') {
steps {
// Maven clean and package step
sh "mvn clean package"
}
}

stage('Build the Image') {
steps {
// Docker build step, naming the image using DockerHub repository name and build number
sh "docker build -t <dockerhubname>/<image name>:${BUILD_NUMBER} ."
}
}

stage('Login and Push the Image') {
steps {
// Docker login step
sh "docker login -u <Username> -p <password>"
// Docker push step, pushing the image with tagged with build number to DockerHub repository
sh "docker push <dockerhubname>/<image name>:${BUILD_NUMBER}"
}
}
}
}


Explanation Of Jenkins Code

  • agent any: This specifies that the pipeline can run on any available agent. An agent is a worker machine on which Jenkins runs its jobs.
  • tools: This block defines the tools required for the pipeline. Here, Maven is specified with version 3.8.6.
  • stages: This block defines the different stages of the pipeline.
    • Code Checkout: This stage checks out the code from the specified Git repository using the provided credentials.
    • Building the Code: In this stage, the Maven build is performed with mvn clean package.
    • Build the Image: Here, the Docker image is built using the Dockerfile present in the repository. The -t option is used to tag the image with the specified DockerHub repository name, image name, and build number.
    • Login and Push the Image: This stage handles logging into DockerHub using provided credentials and then pushes the built Docker image to the specified DockerHub repository with the tagged build number.

Make sure to replace <placeholders> with your actual values, such as <Git Credentials>, <GitHub URL>, <dockerhubname>, <image name>, <Username>, and <password>.

Step-By-Step Guide To Write The Jenkins Declarative Pipeline

Step 1:Launch Jenkins and select the new project option, as indicated below. Type the project name according to the specifications.Jenkins new project

Step 2: After you scroll down the last bit, you will see a script option where you may enter your desired decleratice pipeline script. Once you have finished writing it, click save and apply.Screenshot-(689)

Step 3: When you select the Build Know option, your pipeline will start running. If there are any errors, you may view them in the console output option. Click the “Build Know” option once more after reading the error and fixing the problem. Screenshot-(692)-(1)

Step 4:After extensive debugging, the output total pipeline line execution is shown below.

Pipeline output

Conclusion

To sum up, Jenkins Declarative Pipeline provides a strong and adaptable method for defining workflows for continuous integration and continuous delivery. Developers can quickly construct and maintain complicated pipelines to automate their software delivery process by using specified steps and a standardised syntax.

We have covered the fundamentals of Declarative Pipeline syntax in this article, including stages, steps, and post-actions. We have also covered how to integrate tools and carry out typical operations like code checkout, building, testing, and deployment.

Jenkins Declarative Pipeline – FAQ’s

What are the two pipelines in Jenkins?

Following are the two types of pipelines in jenkins.

  • Scripted Pipeline: Scripted Pipeline is the traditional way of writing Jenkins pipelines.
  • Declarative Pipeline: Declarative Pipeline is a more recent addition to Jenkins and provides a simpler, more structured way of defining pipelines.

What is Jenkins CI CD pipeline?

CI/CD stands for Continuous Integration / Continuous Deployment first let us try to understand what is a pipeline. In computing, a pipeline is a set of stages or processes linked together to form a processing system. Each stage in the pipeline takes an input, processes it in accordance with a set of rules, and then sends the outputs to the stage that follows. Frequently, the pipeline’s overall output is its final step’s output. like the procedures outlined below.

Refer to the – How to Make a CI-CD Pipeline in Jenkins?



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads