-
-
Notifications
You must be signed in to change notification settings - Fork 277
230 lines (228 loc) · 8.96 KB
/
production-deploy.yaml
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
name: Deploy Production
on:
push:
tags:
- 'v*'
workflow_call:
inputs:
version:
type: string
required: true
chart_version:
type: string
required: true
default: current
description: Set to 'current' to use currently deployed chart version or '0.x.y' to specify chart
jobs:
# Note - build occurs again here, even though this image has been built in continuous.
# This is because in continuous, the images are pushed to the dev registry
# Logging in to dev and prod to push to both in continuous isn't really catered for
# Since the build strategy is now a push instead of a pull, all images could live in a single registry, at which point this action would use cache and effectively just perform a tag
workflow-check:
runs-on: ubuntu-latest
outputs:
ref: ${{ steps.check.outputs.ref }}
steps:
- id: check
run: |
if [[ "${{ inputs.version }}" == "" ]]; then
echo "ref=${{ github.ref_name }}" >> $GITHUB_OUTPUT
else
if [[ ${{ inputs.version }} == v* ]]; then
echo "ref=${{ inputs.version }}" >> $GITHUB_OUTPUT
else
echo "ref=v${{ inputs.version }}" >> $GITHUB_OUTPUT
fi
fi
build-generic:
needs: workflow-check
runs-on: ubuntu-latest
permissions:
contents: 'read'
id-token: 'write'
strategy:
matrix:
app: [ web, node ]
steps:
- uses: actions/checkout@v2
with:
ref: ${{ needs.workflow-check.outputs.ref }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- id: auth
name: Authenticate to Google Cloud
uses: google-github-actions/auth@v2
with:
token_format: 'access_token'
workload_identity_provider: 'projects/${{ secrets.PROD_GKE_PROJECT_ID}}/locations/global/workloadIdentityPools/github/providers/github'
service_account: '${{ secrets.PROD_GKE_SA }}'
- name: Login to GCR
uses: docker/login-action@v1
with:
registry: gcr.io
username: oauth2accesstoken
password: '${{ steps.auth.outputs.access_token }}'
- name: Login to GAR
uses: docker/login-action@v1
with:
registry: us-east1-docker.pkg.dev
username: oauth2accesstoken
password: '${{ steps.auth.outputs.access_token }}'
- name: Get current date
id: date
run: echo "date=$(date +'%Y%m%d%H%M')" >> $GITHUB_OUTPUT
- name: Generate image metadata
id: meta
uses: docker/metadata-action@v3
with:
images: |
gcr.io/${{ secrets.PROD_GKE_PROJECT }}/${{ secrets.IMAGE_NAME }}-${{ matrix.app }}
us-east1-docker.pkg.dev/${{ secrets.PROD_GKE_PROJECT }}/containers/${{ secrets.IMAGE_NAME }}-${{ matrix.app }}
# generate Docker tags based on the following events/attributes
tags: |
type=ref,event=tag
type=sha,enable=true,priority=100,prefix=sha-,suffix=-${{ steps.date.outputs.date }},format=short
type=sha
type=semver,pattern={{raw}},value=${{needs.workflow-check.outputs.ref}}
flavor: |
latest=true
- name: build and push
uses: docker/build-push-action@v3
with:
# cache-from: type=registry,ref=$gcr.io/${{ secrets.PROD_GKE_PROJECT_PROJECT }}/{{ secrets.IMAGE_NAME }}-${{ matrix.app }}/cache
# cache-to: type=registry,ref=$gcr.io/${{ secrets.PROD_GKE_PROJECT }}/{{ secrets.IMAGE_NAME }}-${{ matrix.app }}/cache,mode=max
context: .
push: true
build-args: |
TYPE=build-prod
file: ./build/${{ matrix.app }}/Dockerfile
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-derived:
runs-on: ubuntu-latest
permissions:
contents: 'read'
id-token: 'write'
needs:
- build-generic
- workflow-check
strategy:
matrix:
app: [ asset, linker ]
steps:
- uses: actions/checkout@v2
with:
ref: ${{ needs.workflow-check.outputs.ref }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- id: auth
name: Authenticate to Google Cloud
uses: google-github-actions/auth@v2
with:
token_format: 'access_token'
workload_identity_provider: 'projects/${{ secrets.PROD_GKE_PROJECT_ID}}/locations/global/workloadIdentityPools/github/providers/github'
service_account: '${{ secrets.PROD_GKE_SA }}'
- name: Login to GCR
uses: docker/login-action@v1
with:
registry: gcr.io
username: oauth2accesstoken
password: '${{ steps.auth.outputs.access_token }}'
- name: Login to GAR
uses: docker/login-action@v1
with:
registry: us-east1-docker.pkg.dev
username: oauth2accesstoken
password: '${{ steps.auth.outputs.access_token }}'
- name: Get current date
id: date
run: echo "date=$(date +'%Y%m%d%H%M')" >> $GITHUB_OUTPUT
- name: Generate image metadata
id: meta
uses: docker/metadata-action@v3
with:
images: |
gcr.io/${{ secrets.PROD_GKE_PROJECT }}/${{ secrets.IMAGE_NAME }}-${{ matrix.app }}
us-east1-docker.pkg.dev/${{ secrets.PROD_GKE_PROJECT }}/containers/${{ secrets.IMAGE_NAME }}-${{ matrix.app }}
# generate Docker tags based on the following events/attributes
tags: |
type=ref,event=tag
type=sha,enable=true,priority=100,prefix=sha-,suffix=-${{ steps.date.outputs.date }},format=short
type=sha
type=semver,pattern={{raw}},value=${{needs.workflow-check.outputs.ref}}
flavor: |
latest=true
- name: build and push
uses: docker/build-push-action@v3
with:
# cache-from: type=registry,ref=${{ secrets.IMAGE_NAME }}-${{ matrix.app }}/cache
# cache-to: type=registry,ref=${{ secrets.IMAGE_NAME }}-${{ matrix.app }}/cache,mode=max
context: .
push: true
build-args: |
SRC_IMG=us-east1-docker.pkg.dev/${{ secrets.PROD_GKE_PROJECT }}/containers/${{ secrets.IMAGE_NAME }}-web:${{ needs.workflow-check.outputs.ref }}
file: ./build/${{ matrix.app }}/Dockerfile
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
prod-deploy:
runs-on: ubuntu-latest
permissions:
contents: 'read'
id-token: 'write'
needs:
- build-derived
- workflow-check
steps:
- name: Checkout Code
uses: actions/checkout@v2
with:
ref: ${{ needs.workflow-check.outputs.ref }}
- id: auth
name: Authenticate to Google Cloud
uses: google-github-actions/auth@v2
with:
token_format: 'access_token'
workload_identity_provider: 'projects/${{ secrets.PROD_GKE_PROJECT_ID}}/locations/global/workloadIdentityPools/github/providers/github'
service_account: '${{ secrets.PROD_GKE_SA }}'
- name: Setup GCloud
uses: google-github-actions/setup-gcloud@v2
with:
project_id: ${{ secrets.PROD_GKE_PROJECT }}
install_components: 'gke-gcloud-auth-plugin'
- name: Authenticate GHA Runner To Target Cluster
uses: google-github-actions/get-gke-credentials@v2
with:
cluster_name: ${{secrets.PROD_GKE_CLUSTER}}
location: ${{secrets.PROD_GKE_REGION}}
project_id: ${{secrets.PROD_GKE_PROJECT}}
- name: Set up yq
uses: frenck/action-setup-yq@v1
- name: Determine chart version
id: chart_version
run: |
if [[ "${{ inputs.chart_version }}" == "current" ]]; then
echo "chart_version=${{ vars.CHART_VERSION }}" >> $GITHUB_OUTPUT
else
echo "chart_version=${{ inputs.chart_version }}" >> $GITHUB_OUTPUT
fi
- name: Deploy Production
run: ./build/ci/production-helm-deploy.sh build/ci/production-values.yaml
env:
GIT_COMMIT: "${{ needs.workflow-check.outputs.ref }}"
PROJECT_ID: "${{ secrets.PROD_GKE_PROJECT }}"
NAMESPACE: "${{secrets.PROD_GKE_NAMESPACE}}"
IMAGE_NAME: "${{ secrets.IMAGE_NAME }}"
CHART_VERSION: "${{ steps.chart_version.outputs.chart_version }}"
- name: Update workflow default chart
run: >
curl -L
-X PATCH
-H "Accept: application/vnd.github+json"
-H "Authorization: Bearer ${{ secrets.WORKFLOW_AUTOMATION_TOKEN }}"
-H "X-GitHub-Api-Version: 2022-11-28"
https://api.github.com/repos/Sefaria/Sefaria-Project/actions/variables/CHART_VERSION
-d '{"name":"CHART_VERSION","value":"${{ steps.chart_version.outputs.chart_version }}"}'