Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
FoxxMD opened this issue Sep 28, 2022 · 0 comments
Open

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

FoxxMD opened this issue Sep 28, 2022 · 0 comments

Comments

@FoxxMD
Copy link

FoxxMD commented Sep 28, 2022

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:

  • python3.7+
  • pip3

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:

  • READ/WRITE permissions to s3 for elasticbeanstalk-* buckets
  • AWSElasticBeanstalkRoleCore managed policy

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++
                  }
                }
             }
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant