Skip to content

Continuing to modify workflow/main.yml #35

Continuing to modify workflow/main.yml

Continuing to modify workflow/main.yml #35

Workflow file for this run

name: Build and Deploy to Google Compute Engine
on:
push:
branches: [main]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20.x]
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm ci
# Authenticate to Google Cloud
- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v1
with:
credentials_json: ${{ secrets.GCP_SA_KEY }}
# Setup gcloud CLI
- name: Setup Google Cloud SDK
uses: google-github-actions/setup-gcloud@v2
with:
version: '416.0.0'
project_id: ${{ secrets.GCP_PROJECT_ID }}
# 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/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.GCP_INSTANCE_NAME }}-image:${{ github.sha }}" .
# Push the Docker image to Google Container Registry
- name: Publish
run: |
docker push "gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.GCP_INSTANCE_NAME }}-image:${{ github.sha }}"
# Deploy the Docker container on GCE
- name: Deploy
run: |
# Add container metadata if not already set
gcloud compute instances add-metadata "${{ secrets.GCP_INSTANCE_NAME }}" \
--zone "${{ secrets.GCP_INSTANCE_ZONE }}" \
--metadata gce-container-declaration='{"containers":[{"name":"my-container","image":"gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.GCP_INSTANCE_NAME }}-image:${{ github.sha }}"}]}'
# Update the container
gcloud compute instances update-container "${{ secrets.GCP_INSTANCE_NAME }}" \
--zone "${{ secrets.GCP_INSTANCE_ZONE }}" \
--container-image "gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.GCP_INSTANCE_NAME }}-image:${{ github.sha }}"