Last active
June 9, 2021 18:06
-
-
Save ValentaTomas/ffb4e352b268ae57a0c7c1cc19722239 to your computer and use it in GitHub Desktop.
GitHub Action to build and deploy Docker container to Google Compute Engine on master branch push
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
name: Build and Deploy to Google Compute Engine | |
on: | |
push: | |
branches: | |
- master | |
env: | |
PROJECT_ID: ${{ secrets.GCE_PROJECT }} | |
GCE_IMAGE: ${{ secrets.GCE_IMAGE }} | |
GCE_INSTANCE: ${{ secrets.GCE_INSTANCE }} | |
GCE_INSTANCE_ZONE: ${{ secrets.GCE_INSTANCE_ZONE }} | |
jobs: | |
setup-build-push-deploy: | |
name: Setup, Build, Push, and Deploy | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v2 | |
- name: Setup the GCloud CLI | |
uses: google-github-actions/setup-gcloud@master | |
with: | |
version: '290.0.1' | |
service_account_key: ${{ secrets.GCE_SA_KEY }} | |
project_id: ${{ secrets.GCE_PROJECT }} | |
- name: Configure Docker | |
run: |- | |
gcloud --quiet auth configure-docker | |
- name: Build the image | |
run: |- | |
docker build --tag "gcr.io/$PROJECT_ID/$GCE_IMAGE:$GITHUB_SHA" . | |
- name: Push the image to GCR | |
run: |- | |
docker push "gcr.io/$PROJECT_ID/$GCE_IMAGE:$GITHUB_SHA" | |
- name: Delete old docker images from the instance | |
run: |- | |
gcloud compute instances add-metadata "$GCE_INSTANCE" \ | |
--zone "$GCE_INSTANCE_ZONE" \ | |
--metadata startup-script="#! /bin/bash | |
docker image prune -af" | |
- name: Update the server container | |
run: |- | |
gcloud compute instances update-container "$GCE_INSTANCE" \ | |
--zone "$GCE_INSTANCE_ZONE" \ | |
--container-image "gcr.io/$PROJECT_ID/$GCE_IMAGE:$GITHUB_SHA" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment