Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Jenkins Pipeline - Deploy to Auto Scaling Group

In a previous tutorial, I showed you how to get Jenkins to build a Docker image, and deploy it to a single server by SSHing into it. However, if you are making use of an auto-scaling group of EC2 instances, you will want to re-deploy by simply sending a request to AWS to perform an instance refresh. This can be done with a deploy stage like so:

stage("deploy") {
    agent {
        docker { image 'ubuntu:20.04' }
    steps {
        configFileProvider([configFile(fileId: 'myBranchSettingsFile', variable: 'BRANCH_SETTINGS')]) {
            echo "Branch ${env.BRANCH_NAME}"
            echo "Branch Settings: ${BRANCH_SETTINGS}"

            script {
                def config = readJSON file:"$BRANCH_SETTINGS"
                def branchConfig = config."${env.BRANCH_NAME}"

                if (branchConfig) {
                    echo "using config for branch ${env.BRANCH_NAME}"
                    def ASG_NAME = branchConfig.ASG_NAME
                    def AWS_REGION = branchConfig.AWS_REGION
                    def IAM_CREDENTIALS_NAME = branchConfig.IAM_ASG_CREDENTIALS_NAME

                    withCredentials([usernamePassword(credentialsId: IAM_CREDENTIALS_NAME, passwordVariable: 'AWS_KEY_SECRET', usernameVariable: 'AWS_KEY_ID')]) {
                        // install the AWS CLI
                        sh "apt-get update && apt-get install python3-pip -y && pip3 install awscli"

                        // set the aws credentials
                        sh 'aws configure set aws_access_key_id ' + AWS_KEY_ID
                        sh 'aws configure set aws_secret_access_key ' + AWS_KEY_SECRET
                        sh 'aws configure set region ' + AWS_REGION

                        // use the AWS CLI to send the request to rotate the servers,
                        // which will cause them to pull the latest Docker image.
                        sh 'aws autoscaling start-instance-refresh --auto-scaling-group-name ' + ASG_NAME
                else {
                    error("Build failed because failed to fetch settings for branch")

That code on it's own isn't too useful, so I have created an example codebase in GitHub which provides both the Terraform code to set things up, as well as a working Jenkinsfile. The hardest part is plugging in all of the credentials and the configuration file as outlined in the README.

Last updated: 31st March 2022
First published: 31st March 2022