ingenieux / awseb-deployment-plugin

Jenkins Plugin for AWS Elastic Beanstalk
Apache License 2.0
29 stars 53 forks source link

[Guide] Basic replacement of this plugin with awscli/ebcli #107

Open FoxxMD opened 1 year ago

FoxxMD commented 1 year ago

Here's a semi-thorough guide on how to replace this plugin with the official eb cli and aws cli when using a Jenkins Pipeline

Generate an EB config

On your local machine with eb cli installed run eb init and set it up normally.

Copy your config (~/.elasticbeanstalk/config.yml) to the root of your project and name it ebConfig.example.yml. The only thing that needs to be verified here is that the Application used is the same as what you will eventually be deploying to.

Prepare your Build Host

Dependencies

Your system will need:

If you are using an Amazon Linux 2 AMI for your host then you are good to go. Otherwise you will need to get these setup manully

Permissions

This guide assumes you are using an EC2 instance as the host. You will need to setup an IAM profile and attach it to the instance. The profile should have:

Prepare your Build

EB Environments

The Environment Names option in this plugin is replaced by a build parameter named ebEnvironments. Multiple environments should be separated by a comma.

Includes/Excludes

Includes and Excludes are replaced by .ebignore in the root of your project.

Other

Nothing else is required (or included!) So no zero downtime, waiting between deploys, health checks, etc...you can implement that if you want since this is just plain groovy scripting.

Below is the Jenkinsfile template to use, given all the above assumptions, to replace the plugin:

pipeline {
    agent any

    environment {
        // gets short commit id from checked out repo/branch
        GIT_COMMIT = """${sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim()}"""
        // https://stackoverflow.com/a/62219834/1469797
        GIT_BRANCH = "${GIT_BRANCH.split('/').size() > 1 ? GIT_BRANCH.split('/')[1..-1].join('/') : GIT_BRANCH}"
    }

    stages {
        stage('Check EB Prereqs') {
            steps {
                script {
                // make sure pip is installed
                sh("python3 -m ensurepip --upgrade --user")
                // ensure awsebcli is installed
                sh("pip3 install awsebcli --upgrade --user")
                // write a default eb config to the workspace
                sh("mkdir -p .elasticbeanstalk")
                sh("mv ebconfig.example.yml .elasticbeanstalk/config.yml")
                }
            }
        }
        //
        // YOUR ACTUAL BUILD STAGES GO HERE
        //

        // Use .ebignore determine which files from the project to exclude in the application zip
        stage('Deploy Beanstalk Application') {
            when {
                expression { params.ebEnvironments != '' }
              }
              steps {
                echo "EB Environment names specified, will try to deploy to: ${params.ebEnvironments}"
                script {
                  def environmentNames = "${params.ebEnvironments}".split(',')
                  def index = 0
                  for(name in environmentNames) {
                      if(index == 0) {
                            // on first environment we use eb to zip/create application/deploy
                           sh("~/.local/bin/eb use ${name}")
                           sh("~/.local/bin/eb deploy -l ${env.GIT_COMMIT}-${env.BUILD_TAG}")
                      } else {
                          // on subsequent environments we can re-use the application version label with aws cli
                          // to speed up deploys (and we can assume first environment deployed successfully)
                          sh("aws elasticbeanstalk update-environment --environment-name ${name} --version-label ${env.GIT_COMMIT}-${env.BUILD_TAG} --region us-east-1")
                      }
                      index++
                  }
                }
             }
        }
    }
}