Last active
April 22, 2019 14:40
-
-
Save nisevi/36b8560a975cb5e24eda9eac776fcfbd to your computer and use it in GitHub Desktop.
How to deploy to Elastic Beanstalk by using CircleCI 2.1?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: 2.1 | |
orbs: | |
aws-cli: circleci/aws-cli@0.1.13 | |
commands: | |
zip-app: | |
steps: | |
- run: | |
name: "Zip directory structure" | |
command: bash .circleci/zipper.sh | |
- persist_to_workspace: | |
root: /tmp/builds | |
paths: | |
- version | |
- app.zip | |
cached-bundle: | |
steps: | |
# Restore bundle cache | |
- restore_cache: | |
keys: | |
- rails-bundle-{{ checksum "Gemfile.lock" }} | |
- rails-bundle- | |
- run: | |
name: Bundle Install | |
command: bundle check || bundle install | |
# Store bundle cache | |
- save_cache: | |
key: rails-bundle-{{ checksum "Gemfile.lock" }} | |
paths: | |
- vendor/bundle | |
database-setup: | |
steps: | |
# Database setup | |
- run: | |
name: "Wait for DB" | |
command: dockerize -wait tcp://localhost:5432 -timeout 1m | |
- run: | |
name: "Database setup" | |
command: bin/rails db:schema:load --trace | |
rspec-tests: | |
steps: | |
# Run rspec in parallel | |
- run: | | |
bundle exec rspec \ | |
--profile 10 \ | |
--format RspecJunitFormatter \ | |
--out test_results/rspec.xml \ | |
--format progress \ | |
$(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings) | |
# Save test results for timing analysis | |
- store_test_results: | |
path: test_results | |
install-libpython-dev: | |
steps: | |
- run: | |
name: "Install missing library on awscli" | |
command: sudo apt-get install -y libpython-dev | |
eb-setup: | |
steps: | |
- run: sudo apt-get update | |
- install-libpython-dev | |
- aws-cli/install | |
- aws-cli/configure: | |
aws-access-key-id: AWS_ACCESS_KEY_ID | |
aws-secret-access-key: AWS_SECRET_ACCESS_KEY | |
aws-region: AWS_DEFAULT_REGION | |
eb-update: | |
steps: | |
- attach_workspace: | |
at: /tmp/builds | |
- run: | |
name: "Deploy zipped app to Elastic Beanstalk" | |
command: | | |
aws elasticbeanstalk create-application-version \ | |
--application-name "$EB_APP_NAME" \ | |
--version-label "`head -n 1 $TMP_BUILDS/version`" \ | |
--source-bundle "S3Bucket=$AWS_ENV_NAME, S3Key=$EB_APP_NAME/`head -n 1 $TMP_BUILDS/version`.zip" | |
- run: | |
command: | | |
aws elasticbeanstalk update-environment \ | |
--environment-name $AWS_ENV_NAME \ | |
--version-label "`head -n 1 $TMP_BUILDS/version`" | |
s3-setup: | |
steps: | |
- run: sudo apt-get update | |
- install-libpython-dev | |
- aws-cli/install | |
- aws-cli/configure: | |
aws-access-key-id: AWS_ACCESS_KEY_ID | |
aws-secret-access-key: AWS_SECRET_ACCESS_KEY | |
configure-default-region: false | |
s3-update: | |
steps: | |
- zip-app | |
- run: | |
name: "Copy zipped APP to AWS S3" | |
command: aws s3 cp "$TMP_BUILDS/app.zip" s3://$AWS_ENV_NAME/$EB_APP_NAME/"`head -n 1 $TMP_BUILDS/version`".zip | |
env-name-setup: | |
steps: | |
- run: | |
name: "Evaluate and setup env name to be deployed" | |
command: | | |
if [ "${CIRCLE_BRANCH}" == "master" ]; then | |
echo 'export AWS_ENV_NAME=nisevi-production' >> $BASH_ENV | |
elif [ "${CIRCLE_BRANCH}" == "staging" ]; then | |
echo 'export AWS_ENV_NAME=nisevi-staging' >> $BASH_ENV | |
else | |
echo 'export AWS_ENV_NAME=nisevi-development' >> $BASH_ENV | |
fi | |
executors: | |
ruby260-pg969: | |
docker: | |
- image: circleci/ruby:2.6.2 | |
environment: | |
BUNDLE_JOBS: 3 | |
BUNDLE_RETRY: 3 | |
BUNDLE_PATH: vendor/bundle | |
PGHOST: 127.0.0.1 | |
PGUSER: nisevi_user | |
RAILS_ENV: test | |
- image: circleci/postgres:9.6.9-alpine | |
environment: | |
POSTGRES_USER: nisevi_user | |
POSTGRES_DB: nisevi_test | |
POSTGRES_PASSWORD: "" | |
jobs: | |
build: | |
working_directory: ~/app | |
executor: ruby260-pg969 | |
steps: | |
- checkout | |
- cached-bundle | |
- database-setup | |
- rspec-tests | |
s3-copy: | |
working_directory: ~/app | |
executor: ruby260-pg969 | |
steps: | |
- checkout | |
- s3-setup | |
- env-name-setup | |
- s3-update | |
eb-deploy: | |
working_directory: ~/app | |
executor: ruby260-pg969 | |
steps: | |
- checkout | |
- eb-setup | |
- env-name-setup | |
- eb-update | |
workflows: | |
main: | |
jobs: | |
- build | |
- s3-copy: | |
requires: | |
- build | |
filters: | |
branches: | |
only: | |
- development | |
- staging | |
- master | |
- eb-deploy: | |
requires: | |
- s3-copy | |
filters: | |
branches: | |
only: | |
- development | |
- staging | |
- master |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
FILES=('config.ru' \ | |
'Gemfile' \ | |
'Gemfile.lock' \ | |
'Rakefile') | |
FOLDERS=('.ebextensions' \ | |
'app/assets' \ | |
'app/controllers' \ | |
'app/helpers' \ | |
'app/mailers' \ | |
'app/models' \ | |
'app/services' \ | |
'app/views' \ | |
'bin' \ | |
'config' \ | |
'public' \ | |
'lib') | |
for i in ${FOLDERS[@]} | |
do | |
FILES=(${FILES[@]} "$i/*"); | |
done | |
mkdir -p $TMP_BUILDS | |
touch $TMP_BUILDS/version | |
echo "`date +%F`-$CIRCLE_SHA1-`date +%s`" > $TMP_BUILDS/version | |
zip -r $TMP_BUILDS/app.zip ${FILES[@]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you want to see it running you can take a look here for the
development
branch on CircleCI and here to take a look to the project itself.