ことさら−古都プログラマーの更級日記

京都でお寺を回りながら御朱印集めをしていたり、LoLをしたり試合を見に行ったりしているエンジニアのブログです。技術的なはなしとか日常的なはなし、カメラやLoLや競馬の話も書きます。右メニューに検索やらカテゴリーやらがあるので、見たい記事だけ見てね!

Google Cloud Platform(Google App Engine)にRuby on Railsアプリケーションをデプロイするまでの奮闘

もくじ

Google Cloud Platformに登録

適当にGCPとかでググって「無料トライアル」から登録する。クレジットカードの登録は必須。

Google Cloud Console に飛ばされる

https://console.cloud.google.com/

空のプロジェクトを作成

f:id:yoshiki_utakata:20180524180537p:plain

そのまま流れで

f:id:yoshiki_utakata:20180524180609p:plain

登録したらApp Engineへ

何かここをクリックしたら

f:id:yoshiki_utakata:20180524180636p:plain

なんか「はじめてのアプリへようこそ」と出て来るので「Ruby」を選択

f:id:yoshiki_utakata:20180524180727p:plain

プロジェクトを選択して、課金情報を紐付けるとチュートリアルが始まる

f:id:yoshiki_utakata:20180524180741p:plain

Google Cloud SDKを入れる

インストール

https://cloud.google.com/sdk/

$ cd <ダウンロード先のPATH>
$ tar zxvf google-cloud-sdk-180.0.0-darwin-x86_64.tar.gz
$ mv google-cloud-sdk ~
$ export PATH=$PATH:~/google-cloud-sdk/bin

$ gcloud --version
Google Cloud SDK 180.0.0
bq 2.0.27
core 2017.11.10
gsutil 4.28


Updates are available for some Cloud SDK components.  To install them,
please run:
  $ gcloud components update

初期化

$ cd <アプリケーションディレクトリ>
$ gcloud init

エリアを聞かれるけど、東京は asia-northeast1- 系らしいのでその中から選択。

雑にデプロイ

$ gcloud app deploy                                                                                                                                                                                                                                       [master]
WARNING: Automatic app detection is currently in Beta
Deployment to Google App Engine requires an app.yaml file. This
command will run `gcloud beta 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 service, please answer "no").

Do you want to continue (Y/n)?  Y

This looks like a Ruby application. Please confirm the command to run
as the entrypoint: [bundle exec rackup -p $PORT]: ./bin/rails server
Writing [app.yaml] to [/Users/yoshiyuki_sakamoto/ruby/uma/umagraph].
You are creating an app for project [umagraph-205108].
WARNING: Creating an App Engine application for a project is irreversible and the region
cannot be changed. More information about regions is at
<https://cloud.google.com/appengine/docs/locations>.

Please choose the region where you want your App Engine application
located:

 [1] us-central    (supports standard and flexible)
 [2] europe-west3  (supports standard and flexible)
 [3] europe-west   (supports standard and flexible)
 [4] europe-west2  (supports standard and flexible)
 [5] us-east1      (supports standard and flexible)
 [6] us-east4      (supports standard and flexible)
 [7] asia-northeast1 (supports standard and flexible)
 [8] asia-south1   (supports standard and flexible)
 [9] australia-southeast1 (supports standard and flexible)
 [10] southamerica-east1 (supports standard and flexible)
 [11] northamerica-northeast1 (supports standard and flexible)
 [12] cancel
Please enter your numeric choice:  asia-northeast1
Please enter a value between 1 and 12:  7

Creating App Engine application in project [umagraph-205108] and region [asia-northeast1]....done.
Services to deploy:

descriptor:      [/Users/yoshiyuki_sakamoto/ruby/uma/umagraph/app.yaml]
source:          [/Users/yoshiyuki_sakamoto/ruby/uma/umagraph]
target project:  [umagraph-205108]
target service:  [default]
target version:  [20180524t185835]
target url:      [https://umagraph-205108.appspot.com]


Do you want to continue (Y/n)?  Y

(中略)

Updating service [default] (this may take several minutes)...done.
Waiting for operation [apps/umagraph-205108/operations/cd953ddd-8c7a-4d90-942e-4887e70d7439] to complete...done.
Updating service [default]...done.
Deployed service [default] to [https://umagraph-205108.appspot.com]

You can stream logs from the command line by running:
  $ gcloud app logs tail -s default

To view your application in the web browser run:
  $ gcloud app browse

ページ表示しても表示できず。

https://umagraph-205108.appspot.com

An unhandled lowlevel error occurred. The application logs may have details.

SECRET_KEY_BASE設定

config/secrets.yml 確認

development:
  secret_key_base: e0bade0fb467bc180e520633fb4911a15c0b060c830ea2b5520be0b7ef11d30542bf5827aed5c67e8629085d6fce82419416f12633972926b84c65065aca8f8d

test:
  secret_key_base: a0f27729d7e4164fe836487e39fa7db61ef1c3e0823e94e9570a2b7131ad625def303dcc062ac7d434b0866422845d31605109cda0f30f1c89d6604da4381fef

production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
$ bundle exec rake secret
なんやかんやなんやかんや

$ cat app.yaml                                                                                                                                                                                                                                            [master]
entrypoint: ./bin/rails server
env: flex
runtime: ruby
includes:
- secret.yaml

$ cat secret.yaml                                                                                                                                                                                                                                         [master]
env_variables:
  SECRET_KEY_BASE: "なんやかんやなんやかんや"

もういっかい

gcloud app deploy

とりあえず動いたっぽい。