ECS rollback with Jenkins Active Choice Parameter

Browser: Chrome or Firefox or Brave? (Sorry Edge! :p )

Social Media: Facebook or Instagram?

In our daily lives, we always have choices, which one to use.

That was a brief intro about what we are going to read today. Yes, choices!

In Jenkins, while defining parameters, we can mention whether it needs to be a string parameter, a choice parameter, etc. These parameters will work on the input that you are mentioning in your job while running it. For example, if you are asking the user to choose between options A or B, the user chooses the parameter, and then Jenkins runs the script with that parameter.

As an advanced feature to choice parameters, Jenkins has a plugin named Active Choices.

Installation

The installation is quite simple. You need to go to Manage Plugins, check for the Active Choices plugin, and install. There you go!!

Dashboard -> Manage Jenkins -> Manage Plugins -> Available -> Search for Active Choices.

This plugin installs 3 parameter types:

  1. Active Choices Parameter: This parameter uses a groovy script to dynamically provide a list of choices for the user.
  2. Active Choices Reactive Parameter: Works the same way as Active Choices Parameter, you need to reference the Active choice parameter name. It also dynamically provides a list of choices but depends on the Active Choice parameter.
  3. Active Choices Reactive Reference Parameter: Similar to those above, but with a lot more rendering options.

The Use Case:

In the project that I am working on, we have ECS clusters created and there are frequent deployments. There might be a case where the Developer wants to rollback to the previous deployment (due to multiple reasons) in the preprod environment. 

What I had to do, is to create a rollback job, which will help the Developer to rollback to a previous version of the application. That’s when I came to know about Active Choice and here is a sample Groovy script that helps you list down regions in AWS.

import groovy.json.JsonSlurper
def regions = "aws ec2 describe-regions --query Regions --region us-east-1"
def region = regions.execute() 
region.waitFor()
def output = region.in.text
def exitcode= region.exitValue()
def error = region.err.text
def refString = ""
if (error) {
    	println "Std Err: ${error}"
    	println "Process exit code: ${exitcode}"
    	return exitcode
    }
for(i in output.tokenize()){
  refString += i
}
def jsonSlurper = new JsonSlurper()
def parseJson = jsonSlurper.parseText(refString)
def arrays = []
for(i in 0..(parseJson.size()-1)) {
arrays.add(parseJson[i].RegionName)
}
return arrays

Output:

Likewise, I have defined multiple other parameters:

  1. Region: List downs all the regions present in AWS
  2. Cluster: Depends on the parameter Region, and then shows the clusters in a particular region. For example, if you choose ap-south-1, it will list all the clusters present in ap-south-1
  3. Service: Depends on Region Cluster. Shows all the services present in a cluster.
  4. Task: The task parameter depends on all the above parameters. It lists the active task definitions for that Service.
  5. force_new_deployment: This is a choice parameter with choices: true or false

Now, with all these parameters in hand, we run our pipeline:

pipeline {
    agent any


    stages {
        stage('testing') {
            steps {
                println "Cluster Name: ${Cluster}"
                println "Service Name: ${service}"
                
                println "Task: ${Task}"
                println "Force: ${force_new_deployment}"
            }  
        }
        
        stage ('Force deployment') {
      when {
                expression { force_new_deployment == 'true'}
            }
            steps {
                sh 'aws ecs update-service --cluster ${Cluster} --service ${Service} --task-definition ${Task} --force-new-deployment'
            }
    }
    
    stage ('Normal deployment') {
      when {
                expression { force_new_deployment == 'false'}
            }
            steps {
                sh 'aws ecs update-service --cluster ${Cluster} --service ${Service} --task-definition ${Task}'
            }
        }
    }
}

If the force_new_deployment is true, it will force a new deployment of the task definition that you will choose and create new tasks. 

If it is false, the service will wait for the other tasks to stop and then will create new tasks with the selected task definition.

You can find the code here:
https://github.com/prakashjha08/ecs-rollback

Help was taken from:
https://stackoverflow.com/questions/48982349/query-aws-cli-to-populate-jenkins-active-choices-reactive-parameter-linux

So, that’s a use-case that I worked upon, and it was quite a good learning on how ECS deployment works and how Active choice in Jenkins works. Also, groovy!

What else can I do with Active choice plugin??

Here is a list of things that we have done with this plugin:

  1. Copy file from local to EC2 Instances using Jenkins (dropdown will show Instances present in the selected region)
  2. Upload files directly to S3 bucket via Jenkins
  3. List down branches from GitHub and build the job with selected branch

…….and many more.

You can also comment down any such scenario if performed in your infra.

Conclusion:

Here we are, at the end of this blog, what are the key takeaways?

  1. Jenkins has a lot of plugins which we are not aware of!
  2. Keep exploring, you may find something that might help you with the work that you have been provided with.
  3. It doesn’t harm if you learn new tech :p

If you have come this far, a big shoutout to you. Thanks for reading the whole blog. 

Happy Learning! 🙂

Image Reference

Blog Pundit: Naveen Verma and Abhishek Dubey

Opstree is an End to End DevOps solution provider

Connect Us

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: