Skip to content
# Copyright 2020 Google, LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
name: Build and Deploy to Google Compute Engine
on:
push:
branches:
- main
- cloud-deploy
env:
PROJECT_ID: ${{ secrets.GCE_PROJECT }}
GCE_INSTANCE: instance-1 # TODO: update to instance name
GCE_INSTANCE_ZONE: us-west1-b # TODO: update to instance zone
jobs:
setup-build-publish-deploy:
name: Setup, Build, Publish, and Deploy
runs-on: ubuntu-latest
steps:

Check failure on line 33 in .github/workflows/deploy-to-gce.yaml

View workflow run for this annotation

GitHub Actions / .github/workflows/deploy-to-gce.yaml

Invalid workflow file

You have an error in your yaml syntax on line 33
- name: Checkout
uses: actions/checkout@v2
- name: Setup python
uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: Export gcloud related env variable
run: export CLOUDSDK_PYTHON="/usr/bin/python3"
# Setup gcloud CLI
- uses: google-github-actions/setup-gcloud@v0
with:
version: "290.0.1"
service_account_key: ${{ secrets.GCE_SA_KEY }}
project_id: ${{ secrets.GCE_PROJECT }}
# Configure Docker to use the gcloud command-line tool as a credential
# helper for authentication
- run: |-
gcloud --quiet auth configure-docker
# Build the Docker image
- name: Build
run: |-
docker build --tag "gcr.io/$PROJECT_ID/$GCE_INSTANCE-image:$GITHUB_SHA" .
# Push the Docker image to Google Container Registry
- name: Publish
run: |-
docker push "gcr.io/$PROJECT_ID/$GCE_INSTANCE-image:$GITHUB_SHA"
# Add pruning and IP address update to VM startup script
- name: Update startup script to prune and update IP address
run: |-
gcloud compute instances add-metadata $GCE_INSTANCE \
--zone "$GCE_INSTANCE_ZONE" \
--metadata=startup-script="#! /bin/bash
docker image prune -af
curl --location --request GET 'domains.google.com/nic/update?hostname=<your domain name here>' \
--header 'User-Agent: VM' \
--header 'Authorization: Basic $DDNS_AUTH_STRING'"
- name: Deploy
run: |-
gcloud compute instances update-container "$GCE_INSTANCE" \
--zone "$GCE_INSTANCE_ZONE" \
--container-image "gcr.io/$PROJECT_ID/$GCE_INSTANCE-image:$GITHUB_SHA"
# Purge old images from GCR (not latest)
- name: Purge GCR images
run: |-
gcloud container images list-tags gcr.io/$PROJECT_ID/$GCE_INSTANCE-image \
--format="get(digest)" --filter="NOT tags=$GITHUB_SHA" | \
awk -v image_path="gcr.io/$PROJECT_ID/$GCE_INSTANCE-image@" '{print image_path $1}' | \
xargs -r gcloud container images delete --force-delete-tags --quiet