Play2.0をHerokuにデプロイして開発環境を整えるまでのメモ その2
前回 Play 2.0でHerokuにデプロイして開発環境を整えるまでのメモ その1 - takashabeのブログ 作成したPlayアプリケーションをHerokuにデプロイする手順について書いていきます。
HerokuToolbeltの準備
コンソールからHerokuにpushしたりするためのコマンドを導入します。
まずは Heroku | Cloud Application Platform のSign Upからアドレスを登録してアカウントを作成する。直感的なのですぐ分かるはず。
アカウント登録後、HerokuToolbeltのダウンロードリンクがあるはずなのでそれを落としてインストールする。MacならHomebrewにあるので、brew install heroku-toolbeltで入れても良い。
とりあえずheroku versionで確認してみる。
λ ~/ → heroku version heroku-toolbelt/2.33.0 (x86_64-darwin12.2.0) ruby/1.9.3
Herokuアプリケーションの作成
herokuコマンドを使ってHerokuにデプロイする用のアプリケーションを作っていきます。
heorkuではgitリポジトリをそのままpushしてデプロイする感じになるので、まずはgit initしてcommitまでやっておきます。
ちなみにplay newした時にideaやeclipseも対応してるいい感じの.gitignoreが付いてくるので幸福感があります。
λ ~/work/sandbox/kome/ → λ ~/work/sandbox/kome/ → git init Initialized empty Git repository in /Users/takashabe/work/sandbox/kome/.git/ λ ~/work/sandbox/kome/ → λ git master* → λ ~/work/sandbox/kome/ → λ git master* → git add . λ ~/work/sandbox/kome/ → λ git master* → git commit -am 'first commit' [master (root-commit) 8763f6c] first commit 12 files changed, 139 insertions(+) create mode 100644 .gitignore create mode 100644 README create mode 100644 app/controllers/Application.scala create mode 100644 app/views/index.scala.html create mode 100644 app/views/main.scala.html create mode 100644 conf/application.conf create mode 100644 conf/routes create mode 100644 project/Build.scala create mode 100644 project/build.properties create mode 100644 project/plugins.sbt create mode 100644 public/images/favicon.png create mode 100644 public/javascripts/jquery-1.7.1.min.js create mode 100644 public/stylesheets/main.css λ ~/work/sandbox/kome/ → λ git master →
gitリポジトリを作ったらheroku createでHerokuアプリケーションを作ります。
playをHerokuで動かすときは --stack cedar を付けておきます。これはHerokuでの動作環境を指定するもので引数無しのBambooスタックだとRubyのみ、cedarスタックでScalaとかClojureとかで動きます。
λ ~/work/sandbox/kome/ → λ git master → heroku create --stack cedar Creating quiet-atoll-4971... done, stack is cedar http://quiet-atoll-4971.herokuapp.com/ | git@heroku.com:quiet-atoll-4971.git Git remote heroku added λ ~/work/sandbox/kome/ → λ git master → git remote -v heroku git@heroku.com:quiet-atoll-4971.git (fetch) heroku git@heroku.com:quiet-atoll-4971.git (push) λ ~/work/sandbox/kome/ → λ git master →
heroku createの結果を見て分かるようにremoteリポジトリにherokuが登録されます。
Herokuへのデプロイ
gitのherokuリポジトリに対してごにょごにょしてHerokuにpushすればそれがそのままデプロイになります。
λ ~/work/sandbox/kome/ → λ git master → git push heroku master Counting objects: 24, done. Delta compression using up to 8 threads. Compressing objects: 100% (18/18), done. Writing objects: 100% (24/24), 36.02 KiB, done. Total 24 (delta 0), reused 0 (delta 0) -----> Heroku receiving push -----> Play 2.0 - Scala app detected -----> Installing OpenJDK 1.6...done -----> Building app with sbt 〜色々ダウンロードしてる〜 [success] Total time: 0 s, completed Nov 12, 2012 4:59:31 PM -----> Dropping ivy cache from the slug -----> Discovering process types Procfile declares types -> (none) Default types for Play 2.0 - Scala -> web -----> Compiled slug size: 87.8MB -----> Launching... done, v6 http://quiet-atoll-4971.herokuapp.com deployed to Heroku To git@heroku.com:quiet-atoll-4971.git * [new branch] master -> master λ ~/work/sandbox/kome/ → λ git master →
デプロイが完了したらブラウザからHerokuのDashboardにアクセスしてOpen Applicationするか、コンソールからheroku openでブラウザが起動してデプロイしたアプリケーションにお目にかかれます。
あれ?localでplay runしたときはもっとごちゃごちゃ表示されたんだけど?みたいに思ったかもしれませんがplay runだとDevモードで起動するためにWelcomeページが色々と読み込まれていたわけです。ちなみに本番用のProdモードで動かすときはplay startする。
heroku logsを見ると(Prod)で動いているのがわかります。
λ ~/work/sandbox/kome/ → λ git master → heroku logs 2012-11-12T16:57:42+00:00 heroku[slugc]: Slug compilation started 2012-11-12T16:59:49+00:00 heroku[slugc]: Slug compilation finished 2012-11-12T17:00:01+00:00 heroku[web.1]: Starting process with command `target/start -Dhttp.port=23532 -Xmx384m -Xss512k -XX:+UseCompressedOops` 2012-11-12T17:00:03+00:00 app[web.1]: Play server process ID is 2 2012-11-12T17:00:04+00:00 app[web.1]: [info] play - Application started (Prod) // ←Prod !!! 2012-11-12T17:00:04+00:00 app[web.1]: [info] play - Listening for HTTP on port 23532... 2012-11-12T17:05:23+00:00 heroku[router]: GET quiet-atoll-4971.herokuapp.com/ dyno=web.1 queue=0 wait=0ms service=561ms status=200 bytes=468 2012-11-12T17:05:25+00:00 heroku[router]: GET quiet-atoll-4971.herokuapp.com/assets/images/favicon.png dyno=web.1 queue=0 wait=1ms service=51ms status=200 bytes=687 2012-11-12T17:07:02+00:00 heroku[router]: GET quiet-atoll-4971.herokuapp.com/ dyno=web.1 queue=0 wait=0ms service=16ms status=200 bytes=468 2012-11-12T17:07:02+00:00 heroku[router]: GET quiet-atoll-4971.herokuapp.com/assets/stylesheets/main.css dyno=web.1 queue=0 wait=4ms service=26ms status=404 bytes=0 λ ~/work/sandbox/kome/ → λ git master → λ ~/work/sandbox/kome/ → λ git master → play start [info] Loading global plugins from /Users/takashabe/.sbt/plugins [info] Loading project definition from /Users/takashabe/work/sandbox/kome/project [info] Set current project to kome (in build file:/Users/takashabe/work/sandbox/kome/) (Starting server. Type Ctrl+D to exit logs, the server will remain in background) Play server process ID is 18287 [info] play - Application started (Prod) // ←Prod !!! [info] play - Listening for HTTP on port 9000...
IDEとの連携
最後にIntellij IDEAで開発するための手順を見ていきます。いわゆるsbt-ideaのこと
Playでは最初からidea用のモジュールを内蔵しているので(typesafehub/sbt-idea?)追加でpluginの設定はいりません。っていうか手動で設定すると名前空間が衝突してbuildに失敗します( ꒪⌓꒪)
λ ~/work/sandbox/kome/ → λ git master → play [info] Loading global plugins from /Users/takashabe/.sbt/plugins [info] Loading project definition from /Users/takashabe/work/sandbox/kome/project [info] Set current project to kome (in build file:/Users/takashabe/work/sandbox/kome/) _ _ _ __ | | __ _ _ _| | | '_ \| |/ _' | || |_| | __/|_|\____|\__ (_) |_| |__/ play! 2.0.4, http://www.playframework.org > Type "help play" or "license" for more information. > Type "exit" or use Ctrl+D to leave this console. [kome] $ idea [info] Trying to create an Idea module kome [info] Excluding folder target [info] Created /Users/takashabe/work/sandbox/kome/.idea/IdeaProject.iml [info] Created /Users/takashabe/work/sandbox/kome/.idea [info] Excluding folder /Users/takashabe/work/sandbox/kome/target/scala-2.9.1/cache [info] Excluding folder /Users/takashabe/work/sandbox/kome/target/scala-2.9.1/classes [info] Excluding folder /Users/takashabe/work/sandbox/kome/target/scala-2.9.1/classes_managed [info] Excluding folder /Users/takashabe/work/sandbox/kome/target/streams [info] Created /Users/takashabe/work/sandbox/kome/.idea_modules/kome.iml [info] Created /Users/takashabe/work/sandbox/kome/.idea_modules/kome-build.iml [kome] $
今のところ単純に使うだけだとPlayとHerokuもゆるふわでいい感じです。