How to rollback deployment in Jenkins.
Here we will learn how to rollback deployment in Jenkins. The objective is to roll back to the old release or version of a deployment. We will use two Jenkins projects one has the CICD that will deploy the code on the server and another one has the rollback functionality.
This is a basic example of a rollback project. We will deploy an image and an HTML file as artifacts on a server. To rollback the deployment, we will be using the build number of the CICD pipeline project as input in the rollback project in Jenkins.
Perquisites:
- Jenkins
- Sample code: https://github.com/dwops-git/demo-webpage.git
- A remote server
- Nginx configured on remote server.
Step 1. Configure remote server in Jenkins for deployment.
- Install the plugin by going to Manage Jenkins > Manage Plugin > Available Tab > Search SSH publisher > Install the plugin.
- Configure the plugin by going to Manage Jenkins > Configure System > Find Publish over SSH.

- Fill the required by fields as follows:
- Passphrase: If has any. (In this case, we don’t have any)
- Key: Paste the .pem file or private key of the staging server.
- SSH Servers: Click ‘Add’
- Name: Give an appropriate name
- Hostname: IP address of the staging server.
- Username: username used to log in to the staging server.
- Remote Directory: Directory path where you want to deploy the artifact.\
- After that test the configuration by clicking the Test button.
Step 2. Create a cicd pipeline job.
- On Jenkins dashboard, click on New Items to create a new project.
- Give the project a suitable name (ex. cicd) and choose the Pipeline project type.
- Use this declarative pipeline code for the CICD of the code.
- Stage 1 will clone the code from the git.
- Stage 2 can used for building and testing and here we are just printing the message.
- Stage 3 is creating artifact that is including all the files. In your case it can be a war file, jar file, or a zip of complete code.
- Staeg 4 is deploying the code on the remote server at the path specified on Nginx configuration file. In this example the remote path is /var/www/rollback-demo
pipeline {
agent any
stages {
stage('git clone') {
steps {
git branch: 'main', url: 'https://github.com/dwops-git/demo-webpage.git'
}
}
stage('Buil the code'){
steps{
echo 'Building the code.....'
}
}
stage('Creating Artifact'){
steps{
archiveArtifacts artifacts: '**', followSymlinks: false
}
}
stage('deploy the code on server'){
steps{
sshPublisher(publishers: [sshPublisherDesc(configName: 'stage', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'var/www/rollback-demo/' , remoteDirectorySDF: false, removePrefix: '', sourceFiles: '**/*')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
}
Step 3. Run and test the cicd project.
After a successful running and deployment, you can able to see the artifact in the Jenkins.

Now, check the home page on the server IP and it will look like this.

Step 4. Make some changes and deploy on the server.
- Next, we need to make any change like adding some text and push it in the github.
- Now, build the cicd project and you will be able to see your changes. In our case, I have added some exclamation marks in index file as an example.

Step 4. Installing Copy Artifact plugin.
In order to make a rollback job, we need to install a plugin called ‘Copy Artifact’ and install it with a restart.
Step 5. Create a rollback pipeline job.
- Create a new pipeline job and name it ‘rollback’.
- Create a string parameter and name it ‘build_number’. This is the build number of cicd Jenkins’s job and we will use artifact of that build for rollback.
- Use this declarative pipeline code for the rollback job:
- Stage 1 will use the Copy Artifact plugin to copy the artifact of a specific build of cicd project by referring a specific build number.
- Stage 2 will deploy that artifact to the same remote server.
pipeline {
agent any
stages {
stage('Copy Artifact') {
steps {
copyArtifacts filter: '**', fingerprintArtifacts: true, projectName: 'cicd', selector: specific('$build_number')
}
}
stage('deploy the code on server'){
steps{
sshPublisher(publishers: [sshPublisherDesc(configName: 'stage', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'var/www/rollback-demo/' , remoteDirectorySDF: false, removePrefix: '', sourceFiles: '**/*')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
}
- In order to work out, we need to allow rollback project in cicd project as shown in the below screenshot.

- By using the old build number of cicd project we will get the artifacts created in that build, and hence, we have successfully rollback the deployment with the old artifact.

Conclusion:
We have learned a simple and basic level example of “how to rollback in Jenkins”. We have used the artifacts plugin and the Copy Artifact plugin of Jenkins to achieve this. We can further automate this by using some conditions and post-build options in the pipeline. But this is the simple manual method to do this.