Skip to content

Instantly share code, notes, and snippets.

@prateekkathal
Created September 9, 2020 19:19
Show Gist options
  • Save prateekkathal/cca7cd9280029d6c7abed95d17cb08af to your computer and use it in GitHub Desktop.
Save prateekkathal/cca7cd9280029d6c7abed95d17cb08af to your computer and use it in GitHub Desktop.
CircleCI config to build & deploy to AWS EBS/EC2
version: 2.1
aliases:
- &restore-cache
restore_cache:
name: Restore Yarn Package Cache
key: yarn-packages-{{ checksum "yarn.lock" }}
- &install-deps
run:
name: Install dependencies
command: yarn install --frozen-lockfile
- &copy-env
run:
name: Copy Env File
command: cp .env.circleci .env
- &build-app
run:
name: Build App
command: yarn build
- &deployment-deps
run:
name: Installing deployment dependencies
command: |
sudo apt-get -y -qq update
sudo apt-get install python-pip python-dev build-essential
sudo pip install --upgrade setuptools
sudo pip install awsebcli --upgrade
jobs:
build:
docker:
- image: circleci/node:12.17.0
steps:
- checkout
- *restore-cache
- *install-deps
- save_cache:
name: Save Yarn Package Cache
key: yarn-packages-{{ checksum "yarn.lock" }}
paths:
- ~/.cache/yarn
- ./node_modules
- *copy-env
- *build-app
migrate_and_seed:
docker:
- image: circleci/node:12.17.0
environment:
POSTGRES_HOST: localhost
POSTGRES_PORT: 5432
POSTGRES_DATABASE: app_circleci
POSTGRES_USERNAME: app
POSTGRES_PASSWORD: app
- image: circleci/postgres:11.5-alpine
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: app
POSTGRES_DB: app_circleci
steps:
- checkout
- *restore-cache
- *install-deps
- *copy-env
- run:
name: Waiting for Postgres to be ready
command: |
for i in `seq 1 10`;
do
nc -z localhost 5432 && echo Success && exit 0
echo -n .
sleep 1
done
echo Failed waiting for Postgres && exit 1
- run:
name: Run migrations
command: yarn migration:run
- run:
name: Run seeders
command: yarn seed
deploy_to_ebs:
docker:
- image: circleci/python:3.8
steps:
- checkout
- *deployment-deps
- run:
name: Initialize Elastic Beanstalk
command: eb init $AWS_EB_STAGING_APP -p "$AWS_EB_STAGING_PLATFORM" -r $AWS_DEFAULT_REGION -k $AWS_EB_STAGING_EC2_KEY
- run:
name: Deploy app using Elastic Beanstalk
command: eb deploy $AWS_EB_STAGING_ENV -m "{$APP_DESCRIPTION}"
deploy_to_ec2:
machine:
enabled: true
steps:
- add_ssh_keys:
fingerprints:
- "{$SERVER_FINGERPRINT}"
- checkout
- run:
name: Copy files to production server
command: |
rsync -avr --exclude ".env" --exclude ".git" ./ $SSH_USER@$SSH_HOST:/home/ubuntu/app --delete
- run:
name: Build image on production server and restart docker service
command: |
ssh $SSH_USER@$SSH_HOST "cd /home/ubuntu/app && /snap/bin/docker-compose -f docker-compose.production.yml build backend && /snap/bin/docker-compose -f docker-compose.production.yml up --no-deps -d backend && docker image prune -a -f"
workflows:
version: 2.1
build-and-test:
jobs:
- build:
context: app-testing
- migrate_and_seed:
context: app-testing
requires:
- build
build-and-deploy-to-ebs:
jobs:
- build:
context: app-testing
filters:
branches:
only:
- develop
- deploy_to_ebs:
context: app-staging
requires:
- build
filters:
branches:
only:
- develop
build-and-deploy-to-ec2:
jobs:
- build:
context: app-testing
filters:
branches:
only:
- master
- deploy_to_ec2:
context: app-production
requires:
- build
filters:
branches:
only:
- master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment