Google App EngineでRailsを動かしてみる – Qiita とか、個人開発でAppEngineを使う4つの理由 – Qiitaとか見て、興味を持ったのでトライしてみます。基本的には、
How to run Hello World – Ruby — Google Cloud Platform
をベースに進めます。
コンソールからプロジェクトを作成する
https://console.cloud.google.com/
結構丁寧。
無料試用に登録すると60日間無料で300ドルのクーポンがもらえるということで登録してみました。あとで忘れるドキドキ。
と、思ったけど自動的に課金が始まるわけではない様子。
Google Cloud SDKをインストール
$ curl https://sdk.cloud.google.com | bash $ exec -l $SHELL $ gcloud init
作業用ディレクトリでgcloud init
対話形式で進めてくれる。gcloud init
をしたら、Cloud Source Repositories を使うかと聞かれたので、使うと答えてみました。これはあとで調べよう。
This project has one or more associated Git repositories. Pick Git repository to clone to your local machine: [1] [default] [2] Do not clone Please enter your numeric choice:
こんなことも聞かれた。関連のリポジトリがある?作ったばかりのプロジェクトなのに?Do not cloneを選んでみた。これで設定完了。
とりあえず今までのところを振り返ると、今は、作業用の~/Desktop/gaerailssample
の中で作業していて、そこで、gcloud init`をした。
railsを作成
$ cd ~/Desktop/gaerailssample $ rails new .
helloworldを出力するようにします。ここは、とほぼ同じコードです。
# /app/controllers/hello_world_controller.rb class HelloWorldController < ApplicationController def index # Say hello! render text: "Hello, world!" end end # /config/routes.rb root 'hello_world#index'
rails s
で動作確認をします。
デプロイ
デプロイしてみます。
$ gcloud preview app deploy WARNING: The `gcloud preview app` surface is rapidly improving. Look out for changing flags and new commands before the transition out of the `preview` component. These changes will be documented in the Cloud SDK release notes <https://goo.gl/X8apDJ> and via deprecation notices for changing commands. If you would like to avoid changing behavior, please pin to a fixed version of the Google Cloud SDK as described under the "Alternative Methods" section of the Cloud SDK web site: <https://cloud.google.com/sdk/#alternative>. Deployment to Google App Engine requires an app.yaml file. This command will run `gcloud preview app gen-config` to generate an app.yaml file for you in the current directory (if the current directory does not contain an App Engine module, please answer "no"). Do you want to continue (Y/n)?
こんなのが出たのですが、Yにしました。
This looks like a Ruby application. Proceed to configure deployment for Ruby? (Y/n)? Y
Ruby applicationと推定してくれました。これもYで。
NOTICE: We will deploy your application using a recent version of the standard "MRI" Ruby runtime by default. If you want to use a specific Ruby runtime, you can create a ".ruby-version" file in this directory. (For best performance, we recommend MRI version 2.3.0.) Please enter the command to run this Ruby app in production, or leave blank to accept the default: [bundle exec rackup -p 8080 -E deployment config.ru]
と出ました。.ruby-version
作ってなかった。この選択肢はblankで。
app.yml
は対話形式で作ってくれました。https://gaerailssample.appspot.com
で見れるようになるとコンソールに出ました。
ちょっと待つと、billingでエラーに><
ERROR: (gcloud.preview.app.deploy) Server responded with code [400]: Bad Request Unexpected HTTP status 400. Failed Project Preparation (app_id='s~gaerailssample'). Out of retries. Last error: TEMPORARY_ERROR: Operation does not satisfy the following requirements: billing-enabled {Billing must be enabled for activation of service '' in project 'gaerailssample' to proceed., https://console.developers.google.com/project/gaerailssample/settings}
https://console.developers.google.com/project/gaerailssample/settings に行って解消。
次にこんなエラーが。
ERROR: (gcloud.preview.app.deploy) There is a Dockerfile in the current directory, and the runtime field in /Users/moritanaoki/Desktop/gaerailssample/app.yaml is currently set to [runtime: ruby]. To use your Dockerfile to build a custom runtime, set the runtime field in /Users/moritanaoki/Desktop/gaerailssample/app.yaml to [runtime: custom]. To continue using the [ruby] runtime, please omit the Dockerfile from this directory.
そこで、app.yml
を下記のように書き換えて、勝手に生成されてたDockerfileを削除して再度トライ。
runtime: custom vm: true entrypoint: bundle exec rackup -p 8080 -E production config.ru env_variables: SECRET_KEY_BASE: 'hogehoge'
productionとenv_variablesはGoogle App EngineでRailsを動かしてみる – Qiitaを参考にしました。
https://gaerailssample.appspot.com でHello, world!が見れました。
こんなイメージらしい。
んー、Dockerfileを削除しないでやりたかったけどうまくいかなかった。Dockerもっと慣れていかなきゃ。
Tutorialは続く
Tutorial App – Ruby — Google Cloud Platform
ということで、チュートリアルはまだ続きます。Google シンプルでわかりやすいチュートリアル作ってくるんだなぁと思いました。