Skip to content
# This workflow will build a docker container, publish it to Google Container Registry, and deploy it to Cloud Run when a release is created
name: Build and Deploy to Cloud Run
on:
release:
types: [created]
# Environment variables available to all jobs and steps in this workflow
env:
GOOGLE_PROJECT: ${{ secrets.GOOGLE_PROJECT }}
GOOGLE_SERVICE_ACCOUNT_KEY: ${{ secrets.GOOGLE_SERVICE_ACCOUNT_KEY }}
CLOUD_RUN_SERVICE: ${{ secrets.CLOUD_RUN_SERVICE }}
GITHUB_SHA: ${{ github.sha }}
GITHUB_RELEASE_TAG: ${{ github.event.release.tag_name }}
IMAGE: harborbot
REGISTRY_HOSTNAME: us.gcr.io
jobs:
setup-build-publish-deploy:
name: Setup, Build, Publish, and Deploy
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- id: 'auth'
uses: 'google-github-actions/auth@v1'
with:
project_id: ${{ secrets.GOOGLE_PROJECT }}
credentials_json: '${{ secrets.GOOGLE_SERVICE_ACCOUNT_KEY }}'
- name: 'Set up Cloud SDK'
uses: 'google-github-actions/setup-gcloud@v1'
# Configure docker to use the gcloud command-line tool as a credential helper
- run: |
# Set up docker to authenticate
# via gcloud command-line tool.
gcloud auth configure-docker
# Build the Docker image
- name: Build
run: |
docker build -t "$REGISTRY_HOSTNAME/$GOOGLE_PROJECT/$IMAGE:$GITHUB_SHA" \
-t "$REGISTRY_HOSTNAME/$GOOGLE_PROJECT/$IMAGE:$GITHUB_RELEASE_TAG" \
-t "$REGISTRY_HOSTNAME/$GOOGLE_PROJECT/$IMAGE:latest" \
--build-arg GITHUB_SHA="$GITHUB_SHA" \
--build-arg GITHUB_REF="$GITHUB_REF" .
# Push the Docker image to Google Container Registry
- name: Publish
run: |
docker push --all-tags $REGISTRY_HOSTNAME/$GOOGLE_PROJECT/$IMAGE
# Deploy the Docker image to the Cloud Run service
- name: Deploy API
run: |
gcloud run deploy $CLOUD_RUN_SERVICE --image $REGISTRY_HOSTNAME/$GOOGLE_PROJECT/$IMAGE:${GITHUB_SHA} --platform managed --project $GOOGLE_PROJECT --region us-central1 --allow-unauthenticated