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>.

What Is Jenkins Declarative Pipeline ?

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.

Similar Reads

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....

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....

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....

Jenkins Declarative Pipeline – FAQ’s

What are the two pipelines in Jenkins?...

Contact Us