事前準備
啟用 Cloud SQL Admin API 立即啟用 或使用指令介面:
1 2 3 4 5 6 7 $ gcloud projects list $ gcloud config set project <project_id> $ gcloud services list --available $ gcloud services enable sqladmin.googleapis.com
建立 Cloud SQL Instance 和 Database
安裝 gem 1 2 $ bundle add pg $ bundle add appengine
設定資料庫連線 1 2 $ gcloud sql instances describe <instance_name>
1 2 3 4 5 6 7 8 9 10 production: adapter: postgresql encoding: unicode pool: 5 timeout: 5000 username: "[YOUR_POSTGRES_USERNAME]" password: "[YOUR_POSTGRES_PASSWORD]" database: [your_database_name_production ] host: "/cloudsql/[YOUR_INSTANCE_CONNECTION_NAME]"
您也可以一併在 app.yaml
中加入 env_variables
。
Active Storage 設定 1 2 3 4 5 6 google: service: GCS project: [YOUR_PROJECT_ID ] credentials: <%= ENV['GOOGLE_APPLICATION_CREDENTIALS' ] %> bucket: [YOUR_BUCKET_NAME ]
如果您的圖片權限異常 SignedUrlUnavailable
可以取得 credential.json
再試試。
控制台 > APIs & Services > Credentials > Create credentials > Service account key > App engine default service account > JSON > Create 。
1 2 3 4 5 6 google: service: GCS project: website credentials: <%= Rails.root.join("your/path.json" ) %> bucket: [YOUR_BUCKET_NAME ]
設定 app.yaml 部署至 App Engine App Engine 彈性環境使用 app.yaml
描述部署環境設定,在 Rails 專案根目錄加入 app.yaml
1 2 3 4 5 6 7 8 9 10 entrypoint: bundle exec rackup --port $PORT env: flex runtime: ruby env_variables: RAILS_MASTER_KEY: [MASTER_KEY ] beta_settings: cloud_sql_instances: [YOUR_INSTANCE_CONNECTION_NAME ]
設定完成後執行下列指令:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 $ gcloud app create $ RAILS_ENV=production bundle exec rails assets:precompile $ gcloud app deploy $ gcloud info $ gcloud app browse $ gcloud app browse -s <service_name> $ gcloud app logs read $ gcloud app logs tail $ gcloud app logs tail -s <service_name>
授予 appengine RubyGem 權限,執行 Migration 1 2 3 4 5 6 $ gcloud projects list $ gcloud projects add-iam-policy-binding [YOUR-PROJECT-ID] \ --member=serviceAccount:[PROJECT_NUMBER]@cloudbuild.gserviceaccount.com \ --role=roles/editor] $ bundle exec rake appengine:exec -- bundle exec rake db:migrate
其他資源與參考 1 2 3 4 5 6 7 8 9 10 $ gcloud auth application-default login Rails.application.configure do |config| config.google_cloud.logging.project_id = "YOUR-PROJECT-ID" config.google_cloud.logging.keyfile = "/path/to/service-account.json" end