-
Notifications
You must be signed in to change notification settings - Fork 0
163 lines (156 loc) · 5.95 KB
/
homepage-deployment.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
name: Homepage Deployment
on:
push:
branches:
- 'main'
paths:
- 'homepage/**'
- '!homepage/**.md'
- '.github/workflows/homepage-deployment.yml'
env:
CONTAINER_REGISTRY_LOCATION: europe-west1
CONTAINER_REGISTRY: cr-webapps
CONTAINER_NAME: homepage
CR_LOCATION: europe-west1
CR_SERVICE: homepage
jobs:
lint-typecheck:
name: Lint & Type Check
runs-on: ubuntu-latest
permissions:
checks: write
pull-requests: read
contents: read
defaults:
run:
working-directory: homepage
steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Setup Nodejs Environment
uses: actions/setup-node@v3
with:
node-version: 18
cache: yarn
cache-dependency-path: 'homepage/yarn.lock'
- name: Install Dependencies
run: yarn --frozen-lockfile
- name: Lint
run: yarn lint:nofix --output-file eslint_report.json --format json
continue-on-error: true
- name: Type Check
run: yarn tsc > typescript.log
continue-on-error: true
- name: Annotate Code
uses: DerLev/eslint-annotations@v1
with:
eslint-report: homepage/eslint_report.json
typescript-log: homepage/typescript.log
github-token: ${{ secrets.GITHUB_TOKEN }}
error-on-warn: true
status-check-name: Annotations
fail-in-pr: false
add-notice-with-url: false
delete-outdated-ar:
name: Delete outdated Container Images from Artifact Registry
runs-on: ubuntu-latest
needs: lint-typecheck
permissions:
id-token: write
steps:
- name: Authenticate to Google Cloud
id: auth
uses: google-github-actions/auth@v1
with:
workload_identity_provider: ${{ secrets.GCP_WIF_PROVIDER }}
service_account: ${{ secrets.GCP_SA_EMAIL }}
- name: Cleanup Artifact Registry
uses: docker://europe-docker.pkg.dev/gcr-cleaner/gcr-cleaner/gcr-cleaner-cli
with:
args: >-
-repo=${{ env.CONTAINER_REGISTRY_LOCATION }}-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/${{ env.CONTAINER_REGISTRY }}/${{ env.CONTAINER_NAME }}
-keep=6
-tag-filter-any=.*
delete-outdated-cr:
name: Delete outdated Cloud Run Revisions
runs-on: ubuntu-latest
needs: lint-typecheck
permissions:
id-token: write
steps:
- name: Authenticate to Google Cloud
id: auth
uses: google-github-actions/auth@v1
with:
workload_identity_provider: ${{ secrets.GCP_WIF_PROVIDER }}
service_account: ${{ secrets.GCP_SA_EMAIL }}
token_format: access_token
access_token_lifetime: 60s
- name: Only leave the 2 most recent Revisions
uses: actions/github-script@v6
with:
script: |
const response = await fetch("https://run.googleapis.com/v2/projects/${{ secrets.GCP_PROJECT_ID }}/locations/${{ env.CR_LOCATION }}/services/${{ env.CR_SERVICE }}/revisions", { headers: { 'Authorization': "Bearer ${{ steps.auth.outputs.access_token }}" } })
.then(res => res.json());
const toBeDeleted = response.revisions.sort((a, b) => (new Date(b.createTime) - new Date(a.createTime))).slice(2);
const deletionPromises = [];
for(const revision of toBeDeleted) {
deletionPromises.push(fetch(`https://run.googleapis.com/v2/${revision.name}`, { method: 'DELETE', headers: { 'Authorization': "Bearer ${{ steps.auth.outputs.access_token }}" } }));
}
await Promise.all(deletionPromises);
build-deploy:
name: Build Container & Deploy on Cloud Run
runs-on: ubuntu-latest
needs: [delete-outdated-ar, delete-outdated-cr]
permissions:
contents: read
id-token: write
environment: Production
steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Authenticate to Google Cloud
id: auth
uses: google-github-actions/auth@v1
with:
workload_identity_provider: ${{ secrets.GCP_WIF_PROVIDER }}
service_account: ${{ secrets.GCP_SA_EMAIL }}
token_format: access_token
access_token_lifetime: 600s
- name: Login to GCP Artifact Registry
uses: docker/login-action@v2
with:
registry: ${{ env.CONTAINER_REGISTRY_LOCATION }}-docker.pkg.dev
username: oauth2accesstoken
password: ${{ steps.auth.outputs.access_token }}
- name: Extract Metadata for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.CONTAINER_REGISTRY_LOCATION }}-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/${{ env.CONTAINER_REGISTRY }}/${{ env.CONTAINER_NAME }}
tags: |
type=raw,value=latest
type=sha,prefix=,format=long
- name: Build and Push Docker Image
uses: docker/build-push-action@v4
with:
context: homepage/
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v1
- name: Get short commit SHA
id: commit_sha
uses: actions/github-script@v6
with:
script: |
const sha = "${{ github.sha }}"
const shortSha = sha.substring(0, 7)
core.setOutput('shortSha', shortSha)
- name: Create new Cloud Run Revision
run: gcloud run deploy ${{ env.CR_SERVICE }} --image ${{ env.CONTAINER_REGISTRY_LOCATION }}-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/${{ env.CONTAINER_REGISTRY }}/${{ env.CONTAINER_NAME }}:${{ github.sha }} --region ${{ env.CR_LOCATION }} --tag sha-${{ steps.commit_sha.outputs.shortSha }}