Skip to content

Commit

Permalink
👷 Update CI/CD for cloudrun
Browse files Browse the repository at this point in the history
  • Loading branch information
Shion1305 committed Mar 2, 2024
1 parent 61e84c2 commit 182b0e0
Show file tree
Hide file tree
Showing 7 changed files with 302 additions and 56 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.nuxt
.output
node_modules
46 changes: 46 additions & 0 deletions .github/workflows/deploy-cloudrun-pr-closed.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Delete Cloud Run (PR closed)
on:
pull_request:
branches:
- main
types:
- closed
jobs:
build-and-deploy:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
steps:
- name: Login to Google Cloud
id: auth
uses: google-github-actions/auth@v2
with:
token_format: access_token
workload_identity_provider: 'projects/292061085119/locations/global/workloadIdentityPools/github-pool/providers/provider-github'
service_account: 'gh-actions@ynufes-hp-cloudrun.iam.gserviceaccount.com'

- name: setup gcloud
uses: google-github-actions/setup-gcloud@v2
with:
version: '>= 363.0.0'

- name: Delete Cloud Run
id: delete-cloudrun-service
continue-on-error: true
run: |
gcloud run services delete tokiwa23-stg-pr-${{ github.event.number }} --region=asia-northeast1 --quiet
- name: Delete from Artifact Registry
id: delete-artifact-registry
continue-on-error: true
run: |
gcloud artifacts docker images delete asia-northeast1-docker.pkg.dev/ynufes-hp-cloudrun/staging/tokiwa23-pr-${{ github.event.number }}:latest --quiet
- name: Notify Failure on Pull Request
if: ${{ steps.delete-cloudrun-service.outcome != 'success' || steps.delete-artifact-registry.outcome != 'success' }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh pr comment ${{ github.event.number }} -m "Failed to delete resources, CloudRun: ${{ steps.delete-cloudrun-service.outcome }}, Artifact Registry: ${{ steps.delete-artifact-registry.outcome }}"
exit 1
70 changes: 70 additions & 0 deletions .github/workflows/deploy-cloudrun-pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Deploy to Cloud Run (PR preview)
on:
pull_request:
branches:
- main
types:
- opened
- synchronize
- reopened
jobs:
build-and-deploy:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
pull-requests: write
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to Google Cloud
id: auth
uses: google-github-actions/auth@v2
with:
token_format: access_token
workload_identity_provider: 'projects/292061085119/locations/global/workloadIdentityPools/github-pool/providers/provider-github'
service_account: 'gh-actions@ynufes-hp-cloudrun.iam.gserviceaccount.com'

- name: Set up Cloud SDK
uses: 'google-github-actions/setup-gcloud@v2'
with:
version: '>= 363.0.0'

- name: Authorize Docker
id: docker-auth
uses: docker/login-action@v2
with:
username: 'oauth2accesstoken'
password: ${{ steps.auth.outputs.access_token }}
registry: asia-northeast1-docker.pkg.dev

- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
push: true
tags: asia-northeast1-docker.pkg.dev/ynufes-hp-cloudrun/staging/tokiwa23-pr-${{ github.event.number }}:latest
context: .
file: ./cloudrun/Dockerfile

- name: Deploy to Cloud Run
id: deploy
uses: google-github-actions/deploy-cloudrun@v2
with:
service: tokiwa23-stg-pr-${{ github.event.number }}
region: "asia-northeast1"
image: asia-northeast1-docker.pkg.dev/ynufes-hp-cloudrun/staging/tokiwa23-pr-${{ github.event.number }}:latest

- name: Make CloudRun accessible
run: |
gcloud run services add-iam-policy-binding tokiwa23-stg-pr-${{ github.event.number }} --region=asia-northeast1 --member=allUsers --role=roles/run.invoker
- name: Post comments
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
URL: ${{ github.event.pull_request.html_url }}
run:
gh pr comment -b "Deployed on ${{ steps.deploy.outputs.url }}" ${{ github.event.number }}
73 changes: 73 additions & 0 deletions .github/workflows/deploy-cloudrun-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: Deploy to Cloud Run (Release)
on:
push:
branches:
- release
jobs:
build-and-deploy:
runs-on: ubuntu-latest
permissions:
contents: 'read'
id-token: 'write'
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to Google Cloud
id: auth
uses: google-github-actions/auth@v2
with:
token_format: access_token
workload_identity_provider: 'projects/292061085119/locations/global/workloadIdentityPools/github-pool/providers/provider-github'
service_account: 'gh-actions@ynufes-hp-cloudrun.iam.gserviceaccount.com'

- name: Set up Cloud SDK
uses: 'google-github-actions/setup-gcloud@v2'
with:
version: '>= 363.0.0'

- name: Authorize Docker
id: docker-auth
uses: docker/login-action@v2
with:
username: 'oauth2accesstoken'
password: ${{ steps.auth.outputs.access_token }}
registry: asia-northeast1-docker.pkg.dev

- name: Configure .env
run: |
echo "IS_PRODUCTION=true" >> .env
echo "CONTEST_DATA_URL=${{ secrets.CONTEST_DATA_URL }}" >> .env
- name: Download events.json from external URL
run: |
curl "${{ secrets.EVENTS_COMPLETE_URL }}" -o ./assets/data/events-detail.json
curl "${{ secrets.EVENTS_SUMMARY_URL }}" -o ./assets/data/events.json
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
push: true
tags: asia-northeast1-docker.pkg.dev/ynufes-hp-cloudrun/release/tokiwa23:latest
context: .
file: ./cloudrun/Dockerfile

- name: Download Cloud Run Service YAML
run: |
gcloud run services describe hp-main --format yaml --region asia-northeast1 > ./cloudrun/service-hp-main.yaml
# replace github_sha field to latest commit sha. Changing spec.template is required to deploy new revision.
# reference: https://cloud.google.com/run/docs/deploying?hl=ja#revision -- check yaml tab.
- name: Change some property of service-hp-main.yaml
run: |
sed -i "s/github_sha: .*/github_sha: ${{ github.sha }}/g" ./cloudrun/service-hp-main.yaml
- name: Deploy to Cloud Run
id: deploy
uses: google-github-actions/deploy-cloudrun@v2
with:
region: "asia-northeast1"
metadata: "./cloudrun/service-hp-main.yaml"
76 changes: 76 additions & 0 deletions .github/workflows/deploy-cloudrun-stg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: Deploy to Cloud Run (Staging)
on:
push:
branches:
- main
jobs:
build-and-deploy:
runs-on: ubuntu-latest
permissions:
contents: 'read'
id-token: 'write'
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to Google Cloud
id: auth
uses: google-github-actions/auth@v2
with:
token_format: access_token
workload_identity_provider: 'projects/292061085119/locations/global/workloadIdentityPools/github-pool/providers/provider-github'
service_account: 'gh-actions@ynufes-hp-cloudrun.iam.gserviceaccount.com'

- name: Set up Cloud SDK
uses: 'google-github-actions/setup-gcloud@v2'
with:
version: '>= 363.0.0'

- name: Configure .env
run: |
echo "IS_PRODUCTION=false" >> .env
echo "CONTEST_DATA_URL=${{ secrets.CONTEST_DATA_URL_TEST }}" >> .env
- name: Download events.json from external URL
run: |
curl "${{ secrets.EVENTS_COMPLETE_URL }}" -o ./assets/data/events-detail.json
curl "${{ secrets.EVENTS_SUMMARY_URL }}" -o ./assets/data/events.json
- name: Authorize Docker
id: docker-auth
uses: docker/login-action@v2
with:
username: 'oauth2accesstoken'
password: ${{ steps.auth.outputs.access_token }}
registry: asia-northeast1-docker.pkg.dev

- name: configure url, is_


- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
push: true
tags: asia-northeast1-docker.pkg.dev/ynufes-hp-cloudrun/staging/tokiwa23:latest
context: .
file: ./cloudrun/Dockerfile

- name: Download Cloud Run Service YAML
run: |
gcloud run services describe hp-stg --format yaml --region asia-northeast1 > ./cloudrun/service-hp-stg.yaml
# replace github_sha field to latest commit sha. Changing spec.template is required to deploy new revision.
# reference: https://cloud.google.com/run/docs/deploying?hl=ja#revision -- check yaml tab.
- name: Change some property of service-hp-stg.yaml
run: |
sed -i "s/github_sha: .*/github_sha: ${{ github.sha }}/g" ./cloudrun/service-hp-stg.yaml
- name: Deploy to Cloud Run
id: deploy
uses: google-github-actions/deploy-cloudrun@v2
with:
region: "asia-northeast1"
metadata: "./cloudrun/service-hp-stg.yaml"
56 changes: 0 additions & 56 deletions .github/workflows/deploy-gae.yaml

This file was deleted.

34 changes: 34 additions & 0 deletions cloudrun/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
FROM node:20-slim as builder
#nodeのイメージをベースにする

ENV BASE_URL=/23/tokiwa/

WORKDIR /app
#作業ディレクトリの指定

COPY ./ ./
#ynu-fes...の下を/appの下にコピー

RUN yarn install

RUN yarn build

FROM node:20-slim as production

WORKDIR /app

COPY --from=builder /app/.output ./.output
COPY --from=builder /app/.nuxt ./.nuxt
#最小限のファイルをコピー

ENV HOST=0.0.0.0

ENV BASE_URL=/23/tokiwa/

ENV PORT=8080
#環境変数の設定

CMD ["node" ,"./.output/server/index.mjs"]
#--containerを作成するときに実行されるコマンド

EXPOSE 8080

0 comments on commit 182b0e0

Please sign in to comment.