Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

👷 Update CI/CD for cloudrun #113

Merged
merged 1 commit into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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のイメージをベースにする
Comment on lines +1 to +2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment in line 2 is written in Japanese, which might not be universally understood by all team members or contributors. Consider translating comments into English to maintain consistency and ensure that the codebase is accessible to a wider audience.


ENV BASE_URL=/23/tokiwa/

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

COPY ./ ./
#ynu-fes...の下を/appの下にコピー
Comment on lines +9 to +10
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that copying the entire project directory into the Docker image does not inadvertently include sensitive files or unnecessary files that could bloat the image size. It's good practice to use a .dockerignore file to exclude files not needed in the build context, such as local environment configurations, logs, or temporary files.


RUN yarn install

RUN yarn build
Comment on lines +12 to +14
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of yarn install followed by yarn build is standard for Node.js applications. However, consider using yarn install --frozen-lockfile to ensure that the installed dependencies match the versions specified in yarn.lock, which can help prevent accidental updates and ensure consistent builds.


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
Loading