Skip to content

Instantly share code, notes, and snippets.

@hoangtrung99
Last active March 17, 2023 13:30
Show Gist options
  • Save hoangtrung99/025fb354589bf173109203f2fca6e337 to your computer and use it in GitHub Desktop.
Save hoangtrung99/025fb354589bf173109203f2fca6e337 to your computer and use it in GitHub Desktop.
CD google cloud run
# SETUP
# 1. Enable Google Cloud Run API
# 2. Enable Google Artifact Registry API
# 3. Create a service account with the following roles:
# - Cloud Run Admin
# - Artifact Registry Administrator
# - Cloud Run Service Agent
# - Service Account User
# 4. Setup a GitHub secret called GOOGLE_CREDENTIALS with the service account key
# 5. Create a Google Artifact Registry repository
# 6. Setup environment variables in the workflow file
# Author: https://github.com/hoangtrung99
name: Deploy to Cloud Run
on:
workflow_dispatch:
env:
PROJECT_ID: GCP project id
GAR_LOCATION: GAR location
REPOSITORY: GAR repository
SERVICE: GCR service name
REGION: GCR region
jobs:
deploy:
permissions:
contents: 'read'
id-token: 'write'
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: 'actions/checkout@v3'
# https://github.com/google-github-actions/auth
- name: 'Authenticate to Google Cloud'
id: auth
uses: 'google-github-actions/auth@v1'
with:
token_format: 'access_token'
credentials_json: '${{ secrets.GOOGLE_CREDENTIALS }}'
- name: debug output
run: |
echo ${{ steps.auth.outputs.access_token }}
- name: 'Docker Login'
uses: 'docker/login-action@v2'
with:
registry: '${{ env.GAR_LOCATION }}-docker.pkg.dev'
username: 'oauth2accesstoken'
password: '${{ steps.auth.outputs.access_token }}'
- name: Build and push Docker image
# https://github.com/docker/build-push-action
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: ${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.SERVICE }}:latest
# https://github.com/google-github-actions/setup-gcloud
- name: Set up Google Cloud SDK
uses: google-github-actions/setup-gcloud@v1
- name: Deploy to Cloud Run
run: |
gcloud run deploy nextjs \
--image ${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.SERVICE }}:latest \
--platform managed \
--region ${{ env.REGION }} \
--allow-unauthenticated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment