visit
Jenkins Pipeline is a powerful tool when you are using Jenkins to automate your deployments. Flexible and customized actions split between stages are a good reason to try this feature.
Building your own Docker Image and upload to Docker Hub to keep your repository updated is a good example to understand how Jenkins Pipelines can improve your way of work.
Manage Jenkins → Manage Plugins.
Search Docker Pipelines, click on Install without restart and wait until is done.
Upload your Dockerfile definition to your Github repository. Click on the green button Clone or Download and copy the URL as you will need it later.
On Jenkins you need to create a new credential with your Docker Hub account details. Go to Credentials → Global → Add credentials and fill out the form with your username and password. Fill in ID and Descriptions. Note that if you set the ID, you will need this specific ID to refer this credential from your scripts. Here we are just using dockerhub_id.
Now, we are ready to create our first Pipeline. On Jenkins go to New Item → Pipeline, type the name you want for this Pipeline project and then click OK.
Following that you can skip all General and Build Trigger options and go straight to the Pipeline section. Here you can include a Pipeline definition (usually named Jenkinsfile) or you can refer to an external location like Git or Subversion.
The Pipeline we are defining have four stages:$BUILD_NUMBER
to tag the version,pipeline {
environment {
registry = "YourDockerhubAccount/YourRepository"
registryCredential = 'dockerhub_id'
dockerImage = ''
}
agent any
stages {
stage('Cloning our Git') {
steps {
git '//github.com/YourGithubAccount/YourGithubRepository.git'
}
}
stage('Building our image') {
steps{
script {
dockerImage = docker.build registry + ":$BUILD_NUMBER"
}
}
}
stage('Deploy our image') {
steps{
script {
docker.withRegistry( '', registryCredential ) {
dockerImage.push()
}
}
}
}
stage('Cleaning up') {
steps{
sh "docker rmi $registry:$BUILD_NUMBER"
}
}
}
}
Go to your Pipeline project on Jenkins and click on Build Now to run manually. You should get a sequential output of the different stages similar to this one:
About the author - Sudip is a Solution Architect with more than 15 years of working experience, and is the founder of . He likes sharing his knowledge through writing for Hackernoon, , and many more, and while he is not doing that, he must be fishing or playing chess.
Previously posted at .