Skip to content

Instantly share code, notes, and snippets.

@zerolethanh
Created August 1, 2023 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zerolethanh/5c044593610ed2dc3e13aacb8e868a65 to your computer and use it in GitHub Desktop.
Save zerolethanh/5c044593610ed2dc3e13aacb8e868a65 to your computer and use it in GitHub Desktop.
[github actions] CloudRun build & deploy workflows
name: 'CloudRun build production'
on:
push:
branches:
- main # change this if your default branch is named differently
env:
PROJECT_ID: githubtube # TODO: update Google Cloud project id
GAR_LOCATION: asia # TODO: update Artifact Registry location
GCR_LOCATION: asia.gcr.io # TODO: update Container Registry location
GAR_REPOSITORY: githubtube-api # TODO: update Artifact Registry repository name
SERVICE: githubtube-api # TODO: update Cloud Run service name
REGION: 'asia-southeast1' # TODO: update Cloud Run service region
DOCKERFILE: Dockerfile # TODO: update Dockerfile name
jobs:
setup-build-deploy:
name: Setup, Build, and Deploy
runs-on: ubuntu-latest
permissions:
contents: 'read'
id-token: 'write'
pull-requests: write
steps:
- name: Checkout
uses: 'actions/checkout@v3'
- name: Google Auth
id: google-auth
uses: 'google-github-actions/auth@v1'
with:
credentials_json: '${{ secrets.GCP_CREDENTIALS }}'
token_format: 'access_token'
- name: 'Set up Cloud SDK'
uses: google-github-actions/setup-gcloud@v1
with:
project_id: ${{ env.PROJECT_ID }}
- name: Docker Auth
id: docker-auth
uses: 'docker/login-action@v2'
with:
username: 'oauth2accesstoken'
password: '${{ steps.google-auth.outputs.access_token }}'
registry: '${{ env.GAR_LOCATION }}-docker.pkg.dev'
- name: Set vars
id: set-vars
run: |
echo "DOCKER_IMAGE_URL=${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.GCR_LOCATION }}/${{ env.GAR_REPOSITORY }}/${{ env.SERVICE }}:${{ github.sha }}" >> $GITHUB_OUTPUT
echo "SHA_SHORT=${GITHUB_SHA::7}" >> $GITHUB_OUTPUT
- name: Build and Push Container
run: |-
docker build -t "${{ steps.set-vars.outputs.DOCKER_IMAGE_URL }}" -f ${{ env.DOCKERFILE }} ./
docker push "${{ steps.set-vars.outputs.DOCKER_IMAGE_URL }}"
- name: Deploy to Cloud Run
id: deploy-to-cloud-run
uses: google-github-actions/deploy-cloudrun@v1
with:
service: ${{ env.SERVICE }}
region: ${{ env.REGION }}
image: ${{ steps.set-vars.outputs.DOCKER_IMAGE_URL }}
suffix: '${{ steps.set-vars.outputs.SHA_SHORT }}'
- uses: mshick/add-pr-comment@v2
if: always()
with:
message: |
**Deployed to service `${{ env.SERVICE }}`:**
- URL: ${{ steps.deploy-to-cloud-run.outputs.url }}
- Project: `${{ env.PROJECT_ID }}`
- Region: `${{ env.REGION }}`
- Service: `${{ env.SERVICE }}`
- Tag: `${{ steps.set-vars.outputs.SHA_SHORT }}`
- Image: `${{ steps.set-vars.outputs.DOCKER_IMAGE_URL }}`
- Revisions Console: https://console.cloud.google.com/run/detail/${{ env.REGION }}/${{ env.SERVICE }}/revisions?project=${{ env.PROJECT_ID }}
repo-token: ${{ secrets.GITHUB_TOKEN }}
name: 'CloudRun build stg'
on:
push:
branches:
- stg # change this if your default branch is named differently
env:
PROJECT_ID: githubtube # TODO: update Google Cloud project id
GAR_LOCATION: asia # TODO: update Artifact Registry location
GCR_LOCATION: asia.gcr.io # TODO: update Container Registry location
GAR_REPOSITORY: githubtube-api # TODO: update Artifact Registry repository name
SERVICE: githubtube-api-stg # TODO: update Cloud Run service name
REGION: 'asia-southeast1' # TODO: update Cloud Run service region
DOCKERFILE: Dockerfile-stg # TODO: update Dockerfile name
jobs:
setup-build-deploy:
name: Setup, Build, and Deploy
runs-on: ubuntu-latest
permissions:
contents: 'read'
id-token: 'write'
pull-requests: write
steps:
- name: Checkout
uses: 'actions/checkout@v3'
- name: Google Auth
id: google-auth
uses: 'google-github-actions/auth@v1'
with:
credentials_json: '${{ secrets.GCP_CREDENTIALS }}'
token_format: 'access_token'
- name: 'Set up Cloud SDK'
uses: google-github-actions/setup-gcloud@v1
with:
project_id: ${{ env.PROJECT_ID }}
- name: Docker Auth
id: docker-auth
uses: 'docker/login-action@v2'
with:
username: 'oauth2accesstoken'
password: '${{ steps.google-auth.outputs.access_token }}'
registry: '${{ env.GAR_LOCATION }}-docker.pkg.dev'
- name: Set vars
id: set-vars
run: |
echo "DOCKER_IMAGE_URL=${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.GCR_LOCATION }}/${{ env.GAR_REPOSITORY }}/${{ env.SERVICE }}:${{ github.sha }}" >> $GITHUB_OUTPUT
echo "SHA_SHORT=${GITHUB_SHA::7}" >> $GITHUB_OUTPUT
- name: Build and Push Container
run: |-
docker build -t "${{ steps.set-vars.outputs.DOCKER_IMAGE_URL }}" -f ${{ env.DOCKERFILE }} ./
docker push "${{ steps.set-vars.outputs.DOCKER_IMAGE_URL }}"
- name: Deploy to Cloud Run
id: deploy-to-cloud-run
uses: google-github-actions/deploy-cloudrun@v1
with:
service: ${{ env.SERVICE }}
region: ${{ env.REGION }}
image: ${{ steps.set-vars.outputs.DOCKER_IMAGE_URL }}
suffix: '${{ steps.set-vars.outputs.SHA_SHORT }}'
- uses: mshick/add-pr-comment@v2
if: always()
with:
message: |
**Deployed to service `${{ env.SERVICE }}`:**
- URL: ${{ steps.deploy-to-cloud-run.outputs.url }}
- Project: `${{ env.PROJECT_ID }}`
- Region: `${{ env.REGION }}`
- Service: `${{ env.SERVICE }}`
- Tag: `${{ steps.set-vars.outputs.SHA_SHORT }}`
- Image: `${{ steps.set-vars.outputs.DOCKER_IMAGE_URL }}`
- Revisions Console: https://console.cloud.google.com/run/detail/${{ env.REGION }}/${{ env.SERVICE }}/revisions?project=${{ env.PROJECT_ID }}
repo-token: ${{ secrets.GITHUB_TOKEN }}
name: 'CloudRun build dev (on pr changed)'
on:
pull_request:
branches:
- stg # change this if your default branch is named differently
env:
PROJECT_ID: githubtube # TODO: update Google Cloud project id
GAR_LOCATION: asia # TODO: update Artifact Registry location
GCR_LOCATION: asia.gcr.io # TODO: update Container Registry location
GAR_REPOSITORY: githubtube-api # TODO: update Artifact Registry repository name
SERVICE: githubtube-api-stg-dev # TODO: update Cloud Run service name
REGION: 'asia-southeast1' # TODO: update Cloud Run service region
DOCKERFILE: Dockerfile-stg # TODO: update Dockerfile name
jobs:
setup-build-deploy:
name: Setup, Build, and Deploy
runs-on: ubuntu-latest
permissions:
contents: 'read'
id-token: 'write'
pull-requests: write
steps:
- name: Checkout
uses: 'actions/checkout@v3'
- name: Google Auth
id: google-auth
uses: 'google-github-actions/auth@v1'
with:
credentials_json: '${{ secrets.GCP_CREDENTIALS }}'
token_format: 'access_token'
- name: 'Set up Cloud SDK'
uses: google-github-actions/setup-gcloud@v1
with:
project_id: ${{ env.PROJECT_ID }}
- name: Docker Auth
id: docker-auth
uses: 'docker/login-action@v2'
with:
username: 'oauth2accesstoken'
password: '${{ steps.google-auth.outputs.access_token }}'
registry: '${{ env.GAR_LOCATION }}-docker.pkg.dev'
- name: Set vars
id: set-vars
run: |
echo "DOCKER_IMAGE_URL=${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.GCR_LOCATION }}/${{ env.GAR_REPOSITORY }}/${{ env.SERVICE }}:${{ github.sha }}" >> $GITHUB_OUTPUT
echo "SHA_SHORT=${GITHUB_SHA::7}" >> $GITHUB_OUTPUT
- name: Build and Push Container
run: |-
docker build -t "${{ steps.set-vars.outputs.DOCKER_IMAGE_URL }}" -f ${{ env.DOCKERFILE }} ./
docker push "${{ steps.set-vars.outputs.DOCKER_IMAGE_URL }}"
- name: Deploy to Cloud Run
id: deploy-to-cloud-run
uses: google-github-actions/deploy-cloudrun@v1
with:
service: ${{ env.SERVICE }}
region: ${{ env.REGION }}
image: ${{ steps.set-vars.outputs.DOCKER_IMAGE_URL }}
suffix: 'pr${{ github.event.pull_request.number }}-${{ steps.set-vars.outputs.SHA_SHORT }}'
tag: 'pr${{ github.event.pull_request.number }}'
- uses: mshick/add-pr-comment@v2
if: always()
with:
message: |
**Deployed to service `${{ env.SERVICE }}`:**
- URL: ${{ steps.deploy-to-cloud-run.outputs.url }}
- Project: `${{ env.PROJECT_ID }}`
- Region: `${{ env.REGION }}`
- Service: `${{ env.SERVICE }}`
- Tag: `${{ steps.set-vars.outputs.SHA_SHORT }}`
- Image: `${{ steps.set-vars.outputs.DOCKER_IMAGE_URL }}`
- Revisions Console: https://console.cloud.google.com/run/detail/${{ env.REGION }}/${{ env.SERVICE }}/revisions?project=${{ env.PROJECT_ID }}
repo-token: ${{ secrets.GITHUB_TOKEN }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment