From 3af70c4494eaa954fa817aee3b937332d39f5ee6 Mon Sep 17 00:00:00 2001 From: jiikko Date: Sat, 7 Jan 2023 17:31:46 +0900 Subject: [PATCH 1/8] =?UTF-8?q?cloudrun=E3=81=B8=E3=83=87=E3=83=97?= =?UTF-8?q?=E3=83=AD=E3=82=A4=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/actionlint.yml | 2 +- .github/workflows/review_app.yml | 50 ++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/review_app.yml diff --git a/.github/workflows/actionlint.yml b/.github/workflows/actionlint.yml index 416562f7..82342466 100644 --- a/.github/workflows/actionlint.yml +++ b/.github/workflows/actionlint.yml @@ -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: | diff --git a/.github/workflows/review_app.yml b/.github/workflows/review_app.yml new file mode 100644 index 00000000..5baa1c00 --- /dev/null +++ b/.github/workflows/review_app.yml @@ -0,0 +1,50 @@ +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 -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 + 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 }} \ + --allow-unauthenticated From cac0ede1558cb74755aad2d4981bb6ddc764e71e Mon Sep 17 00:00:00 2001 From: jiikko Date: Sat, 7 Jan 2023 19:18:37 +0900 Subject: [PATCH 2/8] PORT --- Dockerfile | 4 ++-- Procfile.dev | 2 +- bin/start.sh | 1 + bin/start_server | 4 ---- docker-compose.yml | 4 ++-- 5 files changed, 6 insertions(+), 9 deletions(-) delete mode 100755 bin/start_server diff --git a/Dockerfile b/Dockerfile index 5829be4a..5b52825c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,6 +4,7 @@ FROM jiikkko/procon_bypass_man_cloud-base:latest ARG RAILS_ENV=production WORKDIR /app + COPY . . COPY Gemfile /app/Gemfile COPY Gemfile.lock /app/Gemfile.lock @@ -13,5 +14,4 @@ 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"] +CMD ["bin/start.sh"] diff --git a/Procfile.dev b/Procfile.dev index 35dce70b..228b857e 100644 --- a/Procfile.dev +++ b/Procfile.dev @@ -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 diff --git a/bin/start.sh b/bin/start.sh index 09fd1a91..1a37a00d 100755 --- a/bin/start.sh +++ b/bin/start.sh @@ -1,4 +1,5 @@ #!/bin/bash +# TODO: precompileはdockerイメージに詰める bundle exec rails assets:precompile bundle exec rails s -p ${PORT:-3000} -e production diff --git a/bin/start_server b/bin/start_server deleted file mode 100755 index f25d8d50..00000000 --- a/bin/start_server +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env bash - -bundle check || bundle install --jobs 100 -bin/rails server -p 5000 -b 0.0.0.0 diff --git a/docker-compose.yml b/docker-compose.yml index d2b3ebce..40de0a2f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -25,8 +25,8 @@ services: - '.:/app' - bundle:/vendor/bundle environment: - - BUNDLE_PATH:/vendor/bundle - - RAILS_ENV:development + - BUNDLE_PATH: /vendor/bundle + - RAILS_ENV: development command: /bin/sh -c "rm -f tmp/pids/server.pid; bin/dev" volumes: bundle: From 17927b771415afd7faa47a0cd24e3da848a57a84 Mon Sep 17 00:00:00 2001 From: jiikko Date: Sat, 7 Jan 2023 19:34:12 +0900 Subject: [PATCH 3/8] =?UTF-8?q?env=E6=AF=8E=E3=81=AB=E4=BD=9C=E6=88=90?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=82=A4=E3=83=A1=E3=83=BC=E3=82=B8=E3=81=AE?= =?UTF-8?q?=E4=B8=AD=E8=BA=AB=E3=82=92=E5=A4=89=E3=81=88=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/review_app.yml | 16 +++++++++++++++- Dockerfile | 26 +++++++++++++++++++------- bin/start.sh | 4 +--- config/database.yml | 6 +++--- docker-compose.yml | 10 ++++++---- 5 files changed, 44 insertions(+), 18 deletions(-) diff --git a/.github/workflows/review_app.yml b/.github/workflows/review_app.yml index 5baa1c00..4002e093 100644 --- a/.github/workflows/review_app.yml +++ b/.github/workflows/review_app.yml @@ -28,7 +28,7 @@ jobs: - name: Build Docker image run: | echo "${REGISTRY_HOST}/$PROJECT_ID/$REPOSITORY_NAME/web:pr-${{ github.event.number }}" - docker build -t "${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 @@ -40,6 +40,9 @@ jobs: 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" \ @@ -47,4 +50,15 @@ jobs: --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}" diff --git a/Dockerfile b/Dockerfile index 5b52825c..281e0a24 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 + + +# 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"] diff --git a/bin/start.sh b/bin/start.sh index 1a37a00d..195bd35a 100755 --- a/bin/start.sh +++ b/bin/start.sh @@ -1,5 +1,3 @@ #!/bin/bash -# TODO: precompileはdockerイメージに詰める -bundle exec rails assets:precompile -bundle exec rails s -p ${PORT:-3000} -e production +bundle exec rails s -p ${PORT:-3000} diff --git a/config/database.yml b/config/database.yml index a1f3542a..2b6550af 100644 --- a/config/database.yml +++ b/config/database.yml @@ -17,6 +17,6 @@ test: <<: *default database: <%= ENV.fetch('DATABASE_DB', 'pbm_cloud_test') %> -# production: -# <<: *default -# database: pbm_cloud_production +production: + <<: *default + database: pbm_cloud_production diff --git a/docker-compose.yml b/docker-compose.yml index 40de0a2f..414ab28e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 From 99e463a267247be951f8ab6eaac2e49a703dd356 Mon Sep 17 00:00:00 2001 From: jiikko Date: Sat, 7 Jan 2023 22:56:12 +0900 Subject: [PATCH 4/8] Serve static files on production --- config/environments/production.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/environments/production.rb b/config/environments/production.rb index ed09aa1b..784c33c5 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -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 From 1f5bf192f6350f4a5f4a40c2d2aeddf670fabf78 Mon Sep 17 00:00:00 2001 From: jiikko Date: Sat, 7 Jan 2023 23:21:32 +0900 Subject: [PATCH 5/8] log to stdout --- config/environments/production.rb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/config/environments/production.rb b/config/environments/production.rb index 784c33c5..42c33f2f 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -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 From bf5071c4c9cde8ab2c89919576758a351b6fdb10 Mon Sep 17 00:00:00 2001 From: jiikko Date: Sun, 8 Jan 2023 09:08:25 +0900 Subject: [PATCH 6/8] database config --- config/database.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/config/database.yml b/config/database.yml index 2b6550af..7d155803 100644 --- a/config/database.yml +++ b/config/database.yml @@ -18,5 +18,4 @@ test: database: <%= ENV.fetch('DATABASE_DB', 'pbm_cloud_test') %> production: - <<: *default database: pbm_cloud_production From 3cf0639cbadf8742dba4e30be04f74a565b2e911 Mon Sep 17 00:00:00 2001 From: jiikko Date: Sun, 8 Jan 2023 11:50:40 +0900 Subject: [PATCH 7/8] dummy --- config/database.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/database.yml b/config/database.yml index 7d155803..5e581da1 100644 --- a/config/database.yml +++ b/config/database.yml @@ -17,5 +17,7 @@ test: <<: *default database: <%= ENV.fetch('DATABASE_DB', 'pbm_cloud_test') %> +# 本番ではDATABASE_URL経由で取得するのでprecompileを通すために適当に埋める production: + adapter: mysql2 database: pbm_cloud_production From 816fef5f90ca839ad8b9ecd6b89cbaa34d2edce7 Mon Sep 17 00:00:00 2001 From: jiikko Date: Sun, 8 Jan 2023 12:26:06 +0900 Subject: [PATCH 8/8] docker build --target production on heroku --- heroku.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/heroku.yml b/heroku.yml index 9bf5f326..9ac6b2c9 100644 --- a/heroku.yml +++ b/heroku.yml @@ -1,6 +1,8 @@ build: docker: web: Dockerfile + args: + - --target=production config: RAILS_ENV: production run: