Skip to content

Commit

Permalink
Merge pull request #160 from splaplapla/cloudrun
Browse files Browse the repository at this point in the history
cloudrunへデプロイする
  • Loading branch information
jiikko authored Jan 8, 2023
2 parents de85ca6 + 816fef5 commit 70d49a0
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/actionlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
actionlint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Run actionlint
shell: bash
run: |
Expand Down
64 changes: 64 additions & 0 deletions .github/workflows/review_app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Review App

on: pull_request

env:
PROJECT_ID: jiikko
REGISTRY_HOST: asia-northeast1-docker.pkg.dev
SERVICE_NAME: pbm-cloud
REPOSITORY_NAME: pbm-cloud

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- id: "auth"
uses: "google-github-actions/auth@v0"
with:
credentials_json: "${{ secrets.GCP_CREDENTIALS }}"
- name: "Set up Cloud SDK"
uses: "google-github-actions/setup-gcloud@v0"
- name: "Use gcloud CLI"
run: |
gcloud auth list
gcloud info
- name: Authorize Docker push
run: gcloud auth configure-docker "${REGISTRY_HOST}"
- name: Build Docker image
run: |
echo "${REGISTRY_HOST}/$PROJECT_ID/$REPOSITORY_NAME/web:pr-${{ github.event.number }}"
docker build --target production -t "${REGISTRY_HOST}/$PROJECT_ID/$REPOSITORY_NAME/web:pr-${{ github.event.number }}" .
docker push "${REGISTRY_HOST}/$PROJECT_ID/$REPOSITORY_NAME/web:pr-${{ github.event.number }}"
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- id: "auth"
uses: "google-github-actions/auth@v0"
with:
credentials_json: "${{ secrets.GCP_CREDENTIALS }}"
- name: Deploy to Cloud Run
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
SECRET_KEY_BASE: ${{ secrets.SECRET_KEY_BASE }}
run: |-
gcloud run deploy "$SERVICE_NAME" \
--project="$PROJECT_ID" \
--platform=managed \
--image="${REGISTRY_HOST}/$PROJECT_ID/$REPOSITORY_NAME/web:pr-${{ github.event.number }}" \
--region="asia-northeast1" \
--tag=pr-${{ github.event.number }} \
--set-env-vars="DATABASE_URL=${DATABASE_URL},SECRET_KEY_BASE=${SECRET_KEY_BASE}" \
--no-traffic \
--allow-unauthenticated
- name: Notify the review app url
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
URL: ${{ github.event.pull_request.html_url }}
run: |-
cat << EOF > comments
https://pr-${{ github.event.number }}---pbm-cloud-7hrjn5ej3q-an.a.run.app/ がデプロイされました
EOF
gh pr comment -F ./comments "${URL}"
30 changes: 21 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
# syntax=docker/dockerfile:1
FROM jiikkko/procon_bypass_man_cloud-base:latest

ARG RAILS_ENV=production
FROM jiikkko/procon_bypass_man_cloud-base:latest as base

WORKDIR /app
COPY . .

COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock

RUN yarn

RUN bundle config set --local path '/vendor/bundle'
RUN bundle check || bundle install --jobs 100

# Configure the main process to run when running the image
CMD ["bin/start_server"]

# target dev
FROM base as dev
LABEL image.type=dev
RUN bundle install --jobs 100


# target production
FROM base as production
LABEL image.type=production
ENV RAILS_ENV production

COPY . .
RUN bundle install --jobs 100 --without development,test
RUN yarn
COPY . /app
RUN SECRET_KEY_BASE=dummy SKIP_TO_LOAD_CREDENTIALS=1 bundle exec rails assets:precompile

CMD ["bin/start.sh"]
2 changes: 1 addition & 1 deletion Procfile.dev
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
web: bin/start_server -p 5000 -b 0.0.0.0
web: bin/rails server -p 5000 -b 0.0.0.0
css: yarn build:css --watch
3 changes: 1 addition & 2 deletions bin/start.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/bin/bash

bundle exec rails assets:precompile
bundle exec rails s -p ${PORT:-3000} -e production
bundle exec rails s -p ${PORT:-3000}
4 changes: 0 additions & 4 deletions bin/start_server

This file was deleted.

7 changes: 4 additions & 3 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ test:
<<: *default
database: <%= ENV.fetch('DATABASE_DB', 'pbm_cloud_test') %>

# production:
# <<: *default
# database: pbm_cloud_production
# 本番ではDATABASE_URL経由で取得するのでprecompileを通すために適当に埋める
production:
adapter: mysql2
database: pbm_cloud_production
12 changes: 5 additions & 7 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
config.public_file_server.enabled = true

# Compress CSS using a preprocessor.
# config.assets.css_compressor = :sass
Expand Down Expand Up @@ -90,12 +90,10 @@
# require "syslog/logger"
# config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')

if ENV["RAILS_LOG_TO_STDOUT"].present?
logger = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
config.logger.level = :info
end
logger = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
config.logger.level = :info

# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false
Expand Down
10 changes: 6 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ services:
web:
image: pbm_cloud
container_name: pbm_cloud
build: .
build:
target: dev
context: .
ports:
- "5000:5000"
volumes:
- '.:/app'
- bundle:/vendor/bundle
environment:
- BUNDLE_PATH:/vendor/bundle
- RAILS_ENV:development
command: /bin/sh -c "rm -f tmp/pids/server.pid; bin/dev"
BUNDLE_PATH: /vendor/bundle
RAILS_ENV: development
command: bin/dev
volumes:
bundle:
driver: local
Expand Down
2 changes: 2 additions & 0 deletions heroku.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
build:
docker:
web: Dockerfile
args:
- --target=production
config:
RAILS_ENV: production
run:
Expand Down

0 comments on commit 70d49a0

Please sign in to comment.