Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save qlawmarq/a5f070cbbd608d445b4f8f669f9f87de to your computer and use it in GitHub Desktop.
Save qlawmarq/a5f070cbbd608d445b4f8f669f9f87de to your computer and use it in GitHub Desktop.
GitHub Actions: Deployer for Google Cloud Run
#####
#
# GitHub Actions will deploy your app to Cloud Run in Google Cloud Platform (GCP) when you push to master or main branch.
#
# 1. Set up your GCP project and Cloud Run service account
# 2. Ensure your service account has permissions to push images to Artifact Registry and deploy to Cloud Run.
# 3. Create a key.json file for the service account and add it to your GitHub repo as a secret. (In this example, the secret name is GCP_DEPLOYER_KEY)
# 4. Enable the Cloud Run API and Artifact Registry API in your GCP project
# 5. Add this file as .github/workflows/deployer.yml
# 6. Add a Dockerfile to your repo to build your app
# 7. Push to master or main branch, and GitHub Actions will deploy your app to Cloud Run
#
#####
# name: Staging Deployer
# on:
# pull_request:
# branches: [master, main]
name: Production Deployer
on:
push:
branches: [master, main]
env:
PROJECT_ID: example-gcp-project
REPOSITORY_NAME: next-js-landing-page
SERVICE_NAME: frontend
LOCATION_NAME: australia-southeast1
ENVIRONMENT_NAME: production
SERVICE_ACCOUNT: deployer-example-gcp-project@example-gcp-project.iam.gserviceaccount.com
APP_PORT: 3000
jobs:
workflow:
name: Setup, Build, Publish, and Deploy
runs-on: ubuntu-latest
permissions:
contents: "read"
id-token: "write"
deployments: write
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Google cloud auth
uses: "google-github-actions/auth@v1"
with:
# You need to add GCP_CREDENTIALS to your GitHub secrets:
# cat your-service-key.json | base64 | pbcopy
# https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions
credentials_json: ${{ secrets.GCP_DEPLOYER_KEY }}
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v0
- name: Authorize Docker push
run: gcloud auth configure-docker $LOCATION_NAME-docker.pkg.dev
- name: Build frontend Docker image
run: docker build --file prod.Dockerfile -t $LOCATION_NAME-docker.pkg.dev/$PROJECT_ID/$REPOSITORY_NAME/$SERVICE_NAME:$ENVIRONMENT_NAME .
- name: Push frontend Docker Image
run: docker push $LOCATION_NAME-docker.pkg.dev/$PROJECT_ID/$REPOSITORY_NAME/$SERVICE_NAME:$ENVIRONMENT_NAME
- name: Deploy frontend to Cloud Run
run: |-
gcloud run deploy $REPOSITORY_NAME-$SERVICE_NAME-$ENVIRONMENT_NAME \
--project=$PROJECT_ID \
--image=$LOCATION_NAME-docker.pkg.dev/$PROJECT_ID/$REPOSITORY_NAME/$SERVICE_NAME:$ENVIRONMENT_NAME \
--region=$LOCATION_NAME \
--port $APP_PORT \
--service-account=$SERVICE_ACCOUNT \
--allow-unauthenticated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment