Skip to content

CloudRunへのデプロイCIの作成 #4

CloudRunへのデプロイCIの作成

CloudRunへのデプロイCIの作成 #4

name: Deploy to Cloud Run - Backend
on:
push:
branches:
- main
pull_request:
branches:
- main
paths:
- '.github/workflows/deploy-cloudrun.yaml'
- 'Dockerfile'
concurrency:
group: deploy-backend
cancel-in-progress: true
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/114263798638/locations/global/workloadIdentityPools/default/providers/provider-github'
service_account: '[email protected]'
- 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@v3
with:
username: 'oauth2accesstoken'
password: ${{ steps.auth.outputs.access_token }}
registry: asia-northeast1-docker.pkg.dev
- uses: actions/cache@v4
id: cache
with:
path: |
go-pkg-mod
root-cache-go-build
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: inject cache into docker
uses: reproducible-containers/[email protected]
with:
cache-map: |
{
"go-pkg-mod": "/go/pkg/mod",
"root-cache-go-build": "/root/.cache/go-build"
}
skip-extraction: ${{ steps.cache.outputs.cache-hit }}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
push: true
tags: asia-northeast1-docker.pkg.dev/gdgoc-ynu-discord-bot/discord-bot/main:latest
context: ./
file: ./Dockerfile
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Download Cloud Run Service YAML
if: ${{ github.event_name != 'pull_request' }}
run: |
gcloud run services describe bot-main --format yaml --region asia-northeast1 > ./bot-service.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 bot-service.yaml
if: ${{ github.event_name != 'pull_request' }}
run: |
sed -i "s/github_sha: .*/github_sha: ${{ github.sha }}/g" ./bot-service.yaml
- name: Deploy to Cloud Run
if: ${{ github.event_name != 'pull_request' }}
id: deploy
uses: google-github-actions/deploy-cloudrun@v2
with:
region: 'asia-northeast1'
metadata: './bot-service.yaml'