Skip to content

Instantly share code, notes, and snippets.

@RobinDaugherty
Created August 7, 2017 18:05
Show Gist options
  • Save RobinDaugherty/136d4afa5f65748476f37d860c38d0b7 to your computer and use it in GitHub Desktop.
Save RobinDaugherty/136d4afa5f65748476f37d860c38d0b7 to your computer and use it in GitHub Desktop.
CircleCI 2.0 conversion for Rails app with staging and production deployed to Flynn cluster
dependencies:
post:
- gem install circleci_deployment_notifier
deployment:
staging:
branch: master
owner: omniboard
commands:
- git fetch --unshallow || true
- echo $CIRCLE_SHA1 > REVISION
- git add REVISION
- git config user.email "admins+circleci@example.com"
- git config user.name "CircleCI Build"
- git commit -m "Add revision"
- 'L=./flynn && curl -sSL -A "`uname -sp`" https://dl.flynn.io/cli | zcat >$L && chmod +x $L'
- ./flynn cluster add -p $FLYNN_STAGING_TLS_PIN staging $FLYNN_STAGING_CLUSTER $FLYNN_STAGING_AUTH_KEY
- ./flynn -c staging -a mink-staging remote add staging
- git push --force staging HEAD:refs/heads/master
- circleci_deployment_notify_slack -a "Mink Staging" -u $SLACK_STAGING_WEBHOOK_URL
- ./flynn run newrelic deployments "--user=$CIRCLE_USERNAME" --revision=$CIRCLE_SHA1
production:
tag: /v[0-9]+\.[0-9]+\.[0-9]+/
owner: omniboard
commands:
- git fetch --unshallow || true
- echo $CIRCLE_TAG > REVISION
- git add REVISION
- git config user.email "admins+circleci@example.com"
- git config user.name "CircleCI Build"
- git commit -m "Add revision"
- 'L=./flynn && curl -sSL -A "`uname -sp`" https://dl.flynn.io/cli | zcat >$L && chmod +x $L'
- ./flynn cluster add -p $FLYNN_PRODUCTION_TLS_PIN production $FLYNN_PRODUCTION_CLUSTER $FLYNN_PRODUCTION_AUTH_KEY
- ./flynn -c production -a mink-production remote add production
- git push --force production HEAD:refs/heads/master
- circleci_deployment_notify_slack -a "Mink Production" -u $SLACK_PRODUCTION_WEBHOOK_URL
- ./flynn run newrelic deployments "--user=$CIRCLE_USERNAME" --revision=$CIRCLE_TAG
version: 2
jobs:
test:
working_directory: ~/mink
docker:
- image: ruby:2.4.0
environment:
DATABASE_URL: postgresql://ubuntu@localhost/mink_test?sslmode=disable
RAILS_ENV: test
RACK_ENV: test
- image: postgres:9.6.2
environment:
POSTGRES_USER: ubuntu
POSTGRES_DB: mink_test
POSTGRES_PASSWORD: ""
steps:
- checkout
- restore_cache:
keys:
- mink-{{ .Branch }}-{{ checksum "Gemfile.lock" }}
- mink-master-{{ checksum "Gemfile.lock" }}
- run:
name: bundle install
command: bundle check --path=vendor/bundle || bundle install --path=vendor/bundle --jobs=4 --retry=3
- save_cache:
key: mink-{{ .Branch }}-{{ checksum "Gemfile.lock" }}
paths:
- "~/mink/vendor/bundle"
- run:
name: set up database
command: bundle exec rake db:create db:migrate --trace
- run:
name: results folder
command: mkdir /tmp/test-reports
- run:
name: rspec
command: bundle exec rspec --color --require rails_helper --format RspecJunitFormatter --out /tmp/test-reports/rspec.xml spec --format doc
- store_artifacts:
path: /tmp/artifacts
destination: test
- store_test_results:
path: /tmp/test-reports
deploy-to-staging:
working_directory: ~/mink
docker:
- image: ruby:2.4.0
environment:
DATABASE_URL: postgresql://ubuntu@localhost/mink_test?sslmode=disable
RAILS_ENV: test
RACK_ENV: test
steps:
- checkout
- run:
command: gem install circleci_deployment_notifier
- run:
name: install flynn CLI
command: L=~/flynn && curl -sSL -A "`uname -sp`" https://dl.flynn.io/cli | zcat >$L && chmod +x $L
- run:
name: deploy to flynn
command: |
DEPLOY_REVISION=$CIRCLE_SHA1
FLYNN_CLUSTER_NAME=$FLYNN_STAGING_CLUSTER
FLYNN_APP_NAME=mink-staging
FRIENDLY_APP_NAME="Mink Staging"
FLYNN_TLS_PIN=$FLYNN_STAGING_TLS_PIN
FLYNN_AUTH_KEY=$FLYNN_STAGING_AUTH_KEY
SLACK_WEBHOOK_URL=$SLACK_STAGING_WEBHOOK_URL
git fetch --unshallow || true;
echo $DEPLOY_REVISION > REVISION;
git add REVISION;
git config user.email "admins+circleci@example.com";
git config user.name "CircleCI Build";
git commit -m "Add revision";
~/flynn cluster add -p $FLYNN_TLS_PIN flynncluster $FLYNN_CLUSTER_NAME $FLYNN_AUTH_KEY;
~/flynn -c flynncluster -a $FLYNN_APP_NAME remote add flynn;
git push --force flynn HEAD:refs/heads/master;
~/flynn -c flynncluster -a $FLYNN_APP_NAME run rake db:migrate DB_STATEMENT_TIMEOUT_MS=300000;
circleci_deployment_notify_slack -a "$FRIENDLY_APP_NAME" -u $SLACK_WEBHOOK_URL;
~/flynn run newrelic deployments "--user=$CIRCLE_USERNAME" --revision=$DEPLOY_REVISION;
deploy-to-production:
working_directory: ~/mink
docker:
- image: ruby:2.4.0
environment:
DATABASE_URL: postgresql://ubuntu@localhost/mink_test?sslmode=disable
RAILS_ENV: test
RACK_ENV: test
steps:
- checkout
- run:
command: gem install circleci_deployment_notifier
- run:
name: install flynn CLI
command: L=~/flynn && curl -sSL -A "`uname -sp`" https://dl.flynn.io/cli | zcat >$L && chmod +x $L
- run:
name: deploy to flynn
command: |
DEPLOY_REVISION=$CIRCLE_TAG
FLYNN_CLUSTER_NAME=$FLYNN_PRODUCTION_CLUSTER
FLYNN_APP_NAME=mink-production
FRIENDLY_APP_NAME="Mink Production"
FLYNN_TLS_PIN=$FLYNN_PRODUCTION_TLS_PIN
FLYNN_AUTH_KEY=$FLYNN_PRODUCTION_AUTH_KEY
SLACK_WEBHOOK_URL=$SLACK_PRODUCTION_WEBHOOK_URL
git fetch --unshallow || true;
echo $DEPLOY_REVISION > REVISION;
git add REVISION;
git config user.email "admins+circleci@example.com";
git config user.name "CircleCI Build";
git commit -m "Add revision";
~/flynn cluster add -p $FLYNN_TLS_PIN flynncluster $FLYNN_CLUSTER_NAME $FLYNN_AUTH_KEY;
~/flynn -c flynncluster -a $FLYNN_APP_NAME remote add flynn;
git push --force flynn HEAD:refs/heads/master;
~/flynn -c flynncluster -a $FLYNN_APP_NAME run rake db:migrate DB_STATEMENT_TIMEOUT_MS=300000;
circleci_deployment_notify_slack -a "$FRIENDLY_APP_NAME" -u $SLACK_WEBHOOK_URL;
~/flynn run newrelic deployments "--user=$CIRCLE_USERNAME" --revision=$DEPLOY_REVISION;
workflows:
version: 2
test-and-deploy:
jobs:
- test:
filters:
tags:
only: '/^v[0-9]+\.[0-9]+\.[0-9]+$/'
- deploy-to-staging:
filters:
branches:
only:
- master
tags:
ignore: /.*/
requires:
- test
- deploy-to-production:
filters:
tags:
only: '/^v[0-9]+\.[0-9]+\.[0-9]+$/'
branches:
ignore: /.*/
requires:
- test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment