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..4002e093 --- /dev/null +++ b/.github/workflows/review_app.yml @@ -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}" diff --git a/Dockerfile b/Dockerfile index 5829be4a..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 -# 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"] 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..195bd35a 100755 --- a/bin/start.sh +++ b/bin/start.sh @@ -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} 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/config/database.yml b/config/database.yml index a1f3542a..5e581da1 100644 --- a/config/database.yml +++ b/config/database.yml @@ -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 diff --git a/config/environments/production.rb b/config/environments/production.rb index ed09aa1b..42c33f2f 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 @@ -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 diff --git a/docker-compose.yml b/docker-compose.yml index d2b3ebce..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 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: