Jenkins rollback

Hi Team,

We are new to Jenkins we are just setup ,Clone the java code from GitHub --and build using mavan–Dockerize the java jar – send it to the AWS ecr – and deployed to the AWS ecs

I wanted to know how to setup rollback for this pipeline.Please advice us.

Hello and welcome to this community, @Vjmax. :wave:

Rolling back in a CI/CD pipeline like the one you described typically involves deploying a previous, stable version of your application if the new deployment fails.

Here’s a general approach to setting up a rollback mechanism for your pipeline:

  1. Versioning: make sure each build artifact (Docker image in your case) is versioned.
    This is typically done using tags.
    Each time you build a new image, tag it with a unique identifier.
    This could be the commit hash, the build number, or a semantic version.
  2. Storing Artifacts: push every built Docker image to a repository (AWS ECR in your case).
    This way, you have a history of built images that you can deploy at any time.
  3. Deployment History: maintain a history of what was deployed and when.
    This could be a simple database table or file that keeps track of the version of the application currently in production, the version it was before, and timestamps for when the changes happened.
  4. Rollback Mechanism: create a mechanism in your pipeline that can take a version number as input and deploy that version to your environment.
    This could be a separate Jenkins job or a parameterized version of your existing deployment job.
    Here’s a simplified example of how you might set up a Jenkins pipeline script (Jenkinsfile) to implement this:
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                // Clone, build and dockerize your application
                // Tag the Docker image with Jenkins build number
                sh 'docker build -t my-app:${BUILD_NUMBER} .'
        stage('Push to ECR') {
            steps {
                // Push the Docker image to ECR
                sh 'docker push my-app:${BUILD_NUMBER}'
        stage('Deploy to ECS') {
            steps {
                // Deploy the Docker image to ECS
                // Here you would use the AWS CLI or a Jenkins plugin to deploy to ECS
                // Record the version being deployed in a deployment history
    post {
        failure {
            stage('Rollback') {
                steps {
                    // Fetch the previous version from your deployment history
                    // Deploy the previous version to ECS

In this example, if any stage of the pipeline fails, the post block will be executed.
The failure block within it will trigger a rollback stage that deploys the previous version of your application.

This is an over-simplified example and you would need to replace the shell commands and possibly add more steps based on your actual build, push, and deploy process.

Also, the rollback mechanism could be more complex depending on your needs.
For example, you may want to add notifications, checks, or manual approvals before performing a rollback. :person_shrugging: