Merge pull request #198 from splaplapla/upgrade-rails #72
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy To Production | |
on: | |
push: | |
branches: | |
- master | |
workflow_dispatch: | |
env: | |
PROJECT_ID: jiikko | |
REGISTRY_HOST: asia-northeast1-docker.pkg.dev | |
SERVICE_NAME: pbm-cloud | |
REPOSITORY_NAME: pbm-cloud | |
jobs: | |
create_deployment: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: chrnorm/deployment-action@v2 | |
name: Create GitHub deployment | |
id: deployment | |
with: | |
token: '${{ github.token }}' | |
environment-url: https://pbm-cloud-dev.jiikko.com/ | |
environment: production | |
outputs: | |
deployment_id: ${{ steps.deployment.outputs.deployment_id }} | |
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: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
- name: Cache Docker layers | |
uses: actions/cache@v3 | |
with: | |
path: /tmp/.buildx-cache | |
key: ${{ runner.os }}-buildx-${{ github.sha }} | |
restore-keys: | | |
${{ runner.os }}-buildx- | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v4 | |
with: | |
context: . | |
push: true | |
target: production | |
tags: ${{ env.REGISTRY_HOST }}/${{ env.PROJECT_ID }}/${{ env.REPOSITORY_NAME }}/web:master-${{ github.sha }} , ${{ env.REGISTRY_HOST }}/${{ env.PROJECT_ID }}/${{ env.REPOSITORY_NAME }}/web:master | |
cache-from: type=local,src=/tmp/.buildx-cache | |
cache-to: type=local,dest=/tmp/.buildx-cache | |
deploy: | |
needs: build | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Add SHORT_SHA env property with commit short sha | |
run: echo "SHORT_SHA=$(echo "${GITHUB_SHA}" | cut -c1-8)" >> "$GITHUB_ENV" | |
- 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 }} | |
REDISCLOUD_URL: ${{ secrets.REDISCLOUD_URL }} | |
GOOGLE_OAUTH2_CLIENT_ID: ${{ secrets.GOOGLE_OAUTH2_CLIENT_ID }} | |
GOOGLE_OAUTH2_SECRET: ${{ secrets.GOOGLE_OAUTH2_SECRET }} | |
TWITCH_OAUTH2_CLIENT_ID: ${{ secrets.TWITCH_OAUTH2_CLIENT_ID }} | |
TWITCH_OAUTH2_SECRET: ${{ secrets.TWITCH_OAUTH2_SECRET }} | |
ACTION_CABLE_URL: ${{ vars.ACTION_CABLE_URL }} | |
run: | | |
gcloud run deploy "$SERVICE_NAME" \ | |
--project="$PROJECT_ID" \ | |
--platform=managed \ | |
--image="${REGISTRY_HOST}/$PROJECT_ID/$REPOSITORY_NAME/web:master-${GITHUB_SHA}" \ | |
--region="asia-northeast1" \ | |
--tag="master-${SHORT_SHA}" \ | |
--set-env-vars="DATABASE_URL2=${DATABASE_URL}" \ | |
--set-env-vars="SECRET_KEY_BASE=${SECRET_KEY_BASE}" \ | |
--set-env-vars="REDISCLOUD_URL=${REDISCLOUD_URL}" \ | |
--set-env-vars="RAILS_ENV=production" \ | |
--set-env-vars="NEW_RELIC_LOG=stdout" \ | |
--set-env-vars="GOOGLE_OAUTH2_CLIENT_ID=${GOOGLE_OAUTH2_CLIENT_ID}" \ | |
--set-env-vars="GOOGLE_OAUTH2_SECRET=${GOOGLE_OAUTH2_SECRET}" \ | |
--set-env-vars="TWITCH_OAUTH2_CLIENT_ID=${TWITCH_OAUTH2_CLIENT_ID}" \ | |
--set-env-vars="TWITCH_OAUTH2_SECRET=${TWITCH_OAUTH2_SECRET}" \ | |
--set-env-vars="ACTION_CABLE_URL=${ACTION_CABLE_URL}" \ | |
--set-env-vars="DISABLE_ACTION_CABLE=1" \ | |
--allow-unauthenticated | |
promote_new_revision: | |
needs: [build, deploy] | |
runs-on: ubuntu-latest | |
steps: | |
- name: Add SHORT_SHA env property with commit short sha | |
run: echo "SHORT_SHA=$(echo "${GITHUB_SHA}" | cut -c1-8)" >> "$GITHUB_ENV" | |
- id: "auth" | |
uses: "google-github-actions/auth@v0" | |
with: | |
credentials_json: "${{ secrets.GCP_CREDENTIALS }}" | |
- name: Update traffic | |
# NOTE: デプロイした直後だと、トラフィックの移行先が見えないことがあるのでretryする、と思ったけどneedsの指定が間違っていただけの可能性がある | |
uses: nick-fields/retry@v2 | |
with: | |
max_attempts: 5 | |
timeout_minutes: 5 | |
command: | | |
gcloud beta run services update-traffic "$SERVICE_NAME" \ | |
--project="$PROJECT_ID" \ | |
--platform=managed \ | |
--to-tags="master-${SHORT_SHA}=100" \ | |
--region="asia-northeast1" | |
update_deployment_status: | |
needs: [create_deployment, build, deploy, promote_new_revision] | |
if: always() | |
runs-on: ubuntu-latest | |
steps: | |
- name: Update deployment status (failure) | |
if: "needs.create_deployment.result == 'failure' || needs.build.result == 'failure' || needs.deploy.result == 'failure' || needs.promote_new_revision.result == 'failure'" | |
uses: chrnorm/deployment-status@v2 | |
with: | |
token: ${{ github.token }} | |
environment-url: https://pbm-cloud-dev.jiikko.com/ | |
state: 'failure' | |
deployment-id: ${{ needs.create_deployment.outputs.deployment_id }} | |
- name: Update deployment status (success) | |
if: "needs.create_deployment.result == 'success' && needs.build.result == 'success' && needs.deploy.result == 'success' && needs.promote_new_revision.result == 'success'" | |
uses: chrnorm/deployment-status@v2 | |
with: | |
token: ${{ github.token }} | |
environment-url: https://pbm-cloud-dev.jiikko.com/ | |
state: 'success' | |
deployment-id: ${{ needs.create_deployment.outputs.deployment_id }} |