diff --git a/.github/workflows/config.yml b/.github/workflows/config.yml index 963703b0..d78a4e66 100644 --- a/.github/workflows/config.yml +++ b/.github/workflows/config.yml @@ -1,4 +1,4 @@ -name: Suikoden Election 2018 +name: 幻水総選挙2018 on: push: @@ -8,41 +8,76 @@ on: - 'v*.*.*' workflow_dispatch: +env: + # TODO: Git のタグを取得して、イメージのタグにする + DOCKER_IMAGE: asia-northeast1-docker.pkg.dev/${{ secrets.CLOUD_RUN_PROJECT }}/cloud-run-source-deploy/gensosenkyo-2018 + GCP_REGION: ${{ secrets.CLOUD_RUN_REGION }} + GCP_AUTH_HOST: asia-northeast1-docker.pkg.dev + CLOUD_RUN_SERVICE_NAME: gensosenkyo-2018 + RAILS_ENV: test + PG_HOST_TEST: localhost + PG_PORT_TEST: 5432 + PG_USERNAME_TEST: postgres_user + PG_PASSWORD_TEST: postgres_password + jobs: - suikoden_election_2018_github_actions: - name: Suikoden Election 2018 CI + rails_app_test: + name: Rails アプリのテスト runs-on: ubuntu-latest - strategy: - matrix: - ruby-version: ["3.1.3"] + services: + postgres: + image: postgres:15.1 + ports: + - 5432:5432 + env: + POSTGRES_USER: postgres_user + POSTGRES_PASSWORD: postgres_password + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 steps: - - name: コードをチェックアウトする - uses: actions/checkout@v3 - - name: Ruby のセットアップを行う - uses: ruby/setup-ruby@v1 - with: - ruby-version: ${{ matrix.ruby-version }} - bundler-cache: true - - name: Node.js のインストールを行う - # https://github.com/nodesource/distributions/blob/master/README.md#debmanual - run: | - KEYRING=/usr/share/keyrings/nodesource.gpg - curl -fsSL https://deb.nodesource.com/gpgkey/nodesource.gpg.key | gpg --dearmor | sudo tee "$KEYRING" >/dev/null - gpg --no-default-keyring --keyring "$KEYRING" --list-keys - - # TODO: ハードコーディングは直したい - VERSION=node_18.x - KEYRING=/usr/share/keyrings/nodesource.gpg - DISTRO="$(lsb_release -s -c)" - echo "deb [signed-by=$KEYRING] https://deb.nodesource.com/$VERSION $DISTRO main" | sudo tee /etc/apt/sources.list.d/nodesource.list - echo "deb-src [signed-by=$KEYRING] https://deb.nodesource.com/$VERSION $DISTRO main" | sudo tee -a /etc/apt/sources.list.d/nodesource.list + - name: コードをチェックアウトする + uses: actions/checkout@v3 + - name: Ruby のセットアップを行う + uses: ruby/setup-ruby@v1 + with: + ruby-version: .ruby-version + bundler-cache: true + - name: データベースをセットアップする + run: | + bin/rails db:create + bin/rails db:migrate + bin/rails db:seed + - name: RSpec を実行する + run: | + bundle exec rspec - sudo apt-get update - sudo apt-get install nodejs - # For Heroku - - name: Install appropriate version's Bundler - run: gem install bundler:2.2.32 - - name: $ bundle install - run: | - bundle config path vendor/bundle - bundle _2.2.32_ install --jobs=4 --retry=3 + deploy_to_cloud_run: + needs: rails_app_test + if: ${{ github.ref_name == 'main' }} + name: Cloud Run に production デプロイする + runs-on: ubuntu-latest + steps: + - name: ソースコードをチェックアウトする + uses: actions/checkout@v3 + - name: GCP の認証を行う + uses: google-github-actions/auth@v1 + with: + credentials_json: ${{ secrets.GCP_SERVICE_ACCOUNT_CREDENTIALS }} + - name: GCP の CLI の認証を行う + run: | + gcloud auth configure-docker ${{ env.GCP_AUTH_HOST }} + - name: Docker イメージ をビルドする + run: | + docker build -t ${{ env.DOCKER_IMAGE }} . + - name: Docker イメージ をプッシュする + run: | + docker push ${{ env.DOCKER_IMAGE }} + - name: Deploy to Cloud Run + uses: google-github-actions/deploy-cloudrun@v0 + with: + service: ${{ env.CLOUD_RUN_SERVICE_NAME }} + image: ${{ env.DOCKER_IMAGE }} + region: ${{ env.GCP_REGION }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..6ea1e701 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,27 @@ +FROM ruby:3.1.3 +ENV LANG C.UTF-8 + +RUN apt update -qq && apt install -y build-essential libpq-dev nodejs +RUN gem install bundler +RUN apt-get update && apt-get install -y curl apt-transport-https wget && \ + curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \ + echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \ + apt-get update && apt-get install -y yarn + +RUN mkdir /myapp +WORKDIR /myapp + +COPY Gemfile /myapp/Gemfile +COPY Gemfile.lock /myapp/Gemfile.lock + +RUN gem install bundler + +# TODO: production ビルド前提なので、開発環境として Docker を利用する場合は Dockerfile を分ける +RUN bundle config set --local without 'test development' +RUN bundle install + +COPY . /myapp + +# Add a script to be executed every time the container starts. +COPY entrypoint.sh /usr/bin/ +ENTRYPOINT ["entrypoint.sh"] diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 00000000..2d95d648 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,16 @@ +#!/bin/bash +set -e + +RAILS_PORT=3000 +if [ -n "$PORT" ]; then + RAILS_PORT=$PORT +fi + +bin/rails assets:precompile +bin/rails db:create +bin/rails db:migrate +bin/rails db:seed + +rm -f tmp/pids/server.pid + +bin/rails s -p $RAILS_PORT -b 0.0.0.0 diff --git a/set_gh_secrets.sh b/set_gh_secrets.sh new file mode 100755 index 00000000..9043fd95 --- /dev/null +++ b/set_gh_secrets.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +set -euxo pipefail + +gh secret set CLOUD_RUN_PROJECT --body "${CLOUD_RUN_PROJECT}" +gh secret set CLOUD_RUN_REGION --body "${CLOUD_RUN_REGION}" +gh secret set GCP_SERVICE_ACCOUNT_CREDENTIALS < /opt/credentials/cloud_run_creds.json