Last active
March 17, 2023 13:30
-
-
Save hoangtrung99/025fb354589bf173109203f2fca6e337 to your computer and use it in GitHub Desktop.
CD google cloud run
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
# 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