LoginSignup
1
1

More than 5 years have passed since last update.

CircleCIでherokuのprebootの設定を自動で行う

Last updated at Posted at 2015-02-07

Motivation

migrationが不要な場合はprebootを有効にしたい

Solution

とりあえず動く版。

理想としては以下2つですが、

  • NEED_MAINTENANCE_MODEをscriptから直に取得する
  • NEED_MAINTENANCE_MODEをGlobalな環境変数とする

circleciのmachine/environmentには動的に値を設定出来ないっぽいので悩んでいます。

注意事項として、testコマンドを利用するとfalseの場合にCircleCIの処理が終了してしまうのでifかcaseを利用しましょう。

script/circleci.sh
#!/bin/bash

# get compare SHA1
compare_url=$CIRCLE_COMPARE_URL
compare_ids=${compare_url#*compare/}

# set NEED_MAINTENANCE_MODE if schema.rb is changed
if git diff --name-only $compare_ids | grep -q 'schema.rb'; then
  echo 1
else
  echo 0
fi
circle.yml
deployment:
  production:
    branch: master
    commands:
      - NEED_MAINTENANCE_MODE=`./script/circleci.sh`; if [ $NEED_MAINTENANCE_MODE -eq 1 ]; then heroku features:disable preboot --app production; else heroku features:enable preboot --app production; fi
      - NEED_MAINTENANCE_MODE=`./script/circleci.sh`; if [ $NEED_MAINTENANCE_MODE -eq 1 ]; then heroku maintenance:on --app production; fi
      - NEED_MAINTENANCE_MODE=`./script/circleci.sh`; if [ $NEED_MAINTENANCE_MODE -eq 1 ]; then heroku scale worker=0 --app production; fi
      - git push -f git@heroku.com:production.git $CIRCLE_SHA1:refs/heads/master
      - NEED_MAINTENANCE_MODE=`./script/circleci.sh`; if [ $NEED_MAINTENANCE_MODE -eq 1 ]; then heroku run 'rake db:migrate' --app production; fi
      - heroku run 'rake db:seed' --app production
      - NEED_MAINTENANCE_MODE=`./script/circleci.sh`; if [ $NEED_MAINTENANCE_MODE -eq 1 ]; then heroku maintenance:off --app production; fi
      - NEED_MAINTENANCE_MODE=`./script/circleci.sh`; if [ $NEED_MAINTENANCE_MODE -eq 1 ]; then heroku features:enable preboot --app production; fi
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1