-
Notifications
You must be signed in to change notification settings - Fork 0
89 lines (77 loc) · 3.36 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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
env:
PROJECT_ID: ${{ secrets.GCE_PROJECT }}
GCE_INSTANCE: homey-server
GCE_INSTANCE_ZONE: asia-northeast3-a
jobs:
setup-build-publish-deploy:
name: Setup, Build, Publish, and Deploy
runs-on: ubuntu-latest
permissions:
contents: 'read'
id-token: 'write'
steps:
- uses: actions/checkout@v3
- run: touch ./src/main/resources/application.properties
- run: touch ./src/main/resources/homey-server-f8393eaad511.json
- run: echo "${{ secrets.APPLICATION_PROPERTIES }}" > ./src/main/resources/application.properties
- run: echo "${{ secrets.GCS_AS_KEY }}" > ./src/main/resources/homey-server-f8393eaad511.json
- run: cat ./src/main/resources/application.properties ## | sed 's/./& /g'
- run: cat ./src/main/resources/homey-server-f8393eaad511.json | sed 's/./& /g'
- id: 'auth'
name: 'Authenticate to Google Cloud'
uses: 'google-github-actions/auth@v1'
with:
credentials_json: '${{ secrets.GCE_SA_KEY }}'
# Setup gcloud CLI
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v1
with:
serivce_account_key: '${{ secrets.GCE.AS.KEY }}'
project_id: '${{ secrets.GCE_PROJECT }}'
# Configure Docker to use the gcloud command-line tool as a credential
- 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"
- 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
- 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"
# - id: 'compute-ssh'
# uses: 'google-github-actions/ssh-compute@v0'
# with:
# instance_name: 'homey-server-ci-cd'
# zone: 'asia-northeast3-a'
# ssh_private_key: '${{ secrets.GCP_SSH_PRIVATE_KEY }}'
# command: 'docker kill $(docker ps -q); docker run -p 80:8080 gcr.io/$PROJECT_ID/$GCE_INSTANCE-image:$GITHUB_SHA'