-
Notifications
You must be signed in to change notification settings - Fork 2
67 lines (53 loc) · 2.2 KB
/
main.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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 }}"