-
-
Notifications
You must be signed in to change notification settings - Fork 312
267 lines (248 loc) · 9.52 KB
/
deploy-static.yaml
File metadata and controls
267 lines (248 loc) · 9.52 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
name: Deploy Static Environment
on:
push:
branches: [master, preprod, prod]
concurrency:
group: deploy-${{ github.ref_name }}
cancel-in-progress: false
jobs:
detect:
runs-on: ubuntu-latest
outputs:
app_changed: ${{ steps.changes.outputs.app_changed }}
chart_changed: ${{ steps.changes.outputs.chart_changed }}
env: ${{ steps.env.outputs.env }}
env_file: ${{ steps.env.outputs.env_file }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Map branch to environment
id: env
run: |
declare -A branch_to_env=(
["master"]="staging"
["preprod"]="preprod"
["prod"]="prod"
)
branch="${GITHUB_REF_NAME}"
env="${branch_to_env[$branch]:-}"
if [[ -z "$env" ]]; then
echo "ERROR: No environment mapping for branch '${branch}'"
exit 1
fi
env_file="envs/${env}/helmrelease.yaml"
if [[ ! -f "$env_file" ]]; then
echo "ERROR: Environment file '${env_file}' does not exist"
exit 1
fi
echo "env=$env" >> $GITHUB_OUTPUT
echo "env_file=$env_file" >> $GITHUB_OUTPUT
- id: changes
run: |
changed=$(git diff --name-only HEAD~1)
echo "Changed files:"
echo "$changed"
app_changed=false
chart_changed=false
while IFS= read -r file; do
if [[ "$file" == helm-chart/* ]]; then
chart_changed=true
elif [[ "$file" != envs/* ]]; then
app_changed=true
fi
done <<< "$changed"
echo "app_changed=$app_changed" >> $GITHUB_OUTPUT
echo "chart_changed=$chart_changed" >> $GITHUB_OUTPUT
release-chart:
needs: detect
if: needs.detect.outputs.chart_changed == 'true'
runs-on: ubuntu-latest
permissions:
contents: write
outputs:
chart_version: ${{ steps.chart_version.outputs.chart_version }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: fetch head of branch
run: git pull origin $GITHUB_REF
- name: Create release rules
run: helm-chart/release-rules.sh
- uses: cycjimmy/semantic-release-action@v4
id: semantic
with:
working_directory: ./helm-chart
semantic_version: 18.0.1
extra_plugins: |
conventional-changelog-conventionalcommits@6.1.0
semantic-release-monorepo@7.0.5
@semantic-release/commit-analyzer@9.0.2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Get chart version
id: chart_version
run: |
if [[ -n "${{ steps.semantic.outputs.new_release_version }}" ]]; then
echo "chart_version=${{ steps.semantic.outputs.new_release_version }}" >> $GITHUB_OUTPUT
else
tag=$(git tag --points-at HEAD -l 'helm-chart-*' | head -1)
if [[ -n "$tag" ]]; then
echo "chart_version=${tag#helm-chart-}" >> $GITHUB_OUTPUT
else
echo "No chart version available"
exit 1
fi
fi
- name: Setup git
run: |
git config --global user.name "$GITHUB_ACTOR"
git config --global user.email "$GITHUB_ACTOR@users.noreply.github.com"
- name: Get appVersion
id: appVersion
run: |
echo "appVersion=$(git describe --match 'v*' --abbrev=0 HEAD --tags)" >> $GITHUB_OUTPUT
- name: Download yq
run: |
wget -nv -nc -O yq https://github.com/mikefarah/yq/releases/download/v4.20.2/yq_linux_amd64
chmod +x yq
- name: Update chart version
run: ./yq -i e '.version = "${{ steps.chart_version.outputs.chart_version }}"' helm-chart/sefaria/Chart.yaml
- name: Update chart appVersion
run: ./yq -i e '.appVersion = "${{ steps.appVersion.outputs.appVersion }}"' helm-chart/sefaria/Chart.yaml
- name: Publish Helm charts
uses: stefanprodan/helm-gh-pages@master
with:
token: ${{ secrets.GITHUB_TOKEN }}
chart_version: ${{ steps.chart_version.outputs.chart_version }}
app_version: ${{ steps.appVersion.outputs.appVersion }}
charts_dir: helm-chart
release-app:
needs: detect
if: needs.detect.outputs.app_changed == 'true'
runs-on: ubuntu-latest
permissions:
contents: write
id-token: write
outputs:
app_version: ${{ steps.app_version.outputs.app_version }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- 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 GAR
uses: docker/login-action@v3
with:
registry: us-east1-docker.pkg.dev
username: oauth2accesstoken
password: '${{ steps.auth.outputs.access_token }}'
- uses: cycjimmy/semantic-release-action@v4
id: semantic
with:
extra_plugins: |
conventional-changelog-conventionalcommits@8.0.0
@semantic-release/commit-analyzer@10.0.1
@semantic-release/exec
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BUILD_IMAGES: "true"
IMAGE_REGISTRY: us-east1-docker.pkg.dev/${{ secrets.PROD_GKE_PROJECT }}/containers
IMAGE_NAME: ${{ secrets.IMAGE_NAME }}
- name: Get app version
id: app_version
run: |
if [[ -n "${{ steps.semantic.outputs.new_release_version }}" ]]; then
echo "app_version=${{ steps.semantic.outputs.new_release_version }}" >> $GITHUB_OUTPUT
else
tag=$(git tag --points-at HEAD -l 'v*' | head -1)
if [[ -n "$tag" ]]; then
echo "app_version=${tag#v}" >> $GITHUB_OUTPUT
else
echo "No app version available"
exit 1
fi
fi
update-deployment-state:
needs: [detect, release-chart, release-app]
if: always() && !failure() && !cancelled()
runs-on: ubuntu-latest
permissions:
contents: write
outputs:
release_tag: ${{ steps.tag.outputs.release_tag }}
env:
ENV_NAME: ${{ needs.detect.outputs.env }}
ENV_FILE: ${{ needs.detect.outputs.env_file }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.BYPASS_BRANCH_PROTECTION_TOKEN }}
- name: Setup git
run: |
git config user.name "$GITHUB_ACTOR"
git config user.email "$GITHUB_ACTOR@users.noreply.github.com"
- name: Set up yq
uses: frenck/action-setup-yq@v1
- name: Resolve app version
id: app
run: |
if [[ -n "${{ needs.release-app.outputs.app_version }}" ]]; then
echo "version=${{ needs.release-app.outputs.app_version }}" >> $GITHUB_OUTPUT
else
echo "version=$(yq '.spec.values.web.containerImage.tag' "$ENV_FILE" | sed 's/^v//')" >> $GITHUB_OUTPUT
fi
- name: Resolve chart version
id: chart
run: |
if [[ -n "${{ needs.release-chart.outputs.chart_version }}" ]]; then
echo "version=${{ needs.release-chart.outputs.chart_version }}" >> $GITHUB_OUTPUT
else
echo "version=$(yq '.spec.chart.spec.version' "$ENV_FILE")" >> $GITHUB_OUTPUT
fi
- name: Update HelmRelease
run: |
export APP_TAG="v${{ steps.app.outputs.version }}"
export CHART_VERSION="${{ steps.chart.outputs.version }}"
yq -i '
.spec.chart.spec.version = strenv(CHART_VERSION) |
.spec.values.web.containerImage.tag = strenv(APP_TAG) |
.spec.values.nodejs.containerImage.tag = strenv(APP_TAG) |
.spec.values.nginx.containerImage.tag = strenv(APP_TAG) |
.spec.values.monitor.containerImage.tag = strenv(APP_TAG)
' "$ENV_FILE"
- name: Commit and tag
id: tag
run: |
APP_VERSION="${{ steps.app.outputs.version }}"
CHART_VERSION="${{ steps.chart.outputs.version }}"
RELEASE_TAG="${ENV_NAME}/${APP_VERSION}+chart.${CHART_VERSION}"
mkdir -p helm-chart/promotions promotions
echo "${CHART_VERSION}" > helm-chart/promotions/${ENV_NAME}
echo "${APP_VERSION}" > promotions/${ENV_NAME}
git add "$ENV_FILE" helm-chart/promotions/${ENV_NAME} promotions/${ENV_NAME}
git commit -m "deploy(${ENV_NAME}): app=${APP_VERSION} chart=${CHART_VERSION} [skip ci]"
git tag -a "${RELEASE_TAG}" -m "deploy(${ENV_NAME}): app=${APP_VERSION} chart=${CHART_VERSION}"
git push origin "${GITHUB_REF_NAME}" --follow-tags
echo "release_tag=${RELEASE_TAG}" >> $GITHUB_OUTPUT
trigger-deploy:
needs: [detect, update-deployment-state]
if: always() && !failure() && !cancelled()
uses: ./.github/workflows/trigger-infra-deploy.yml
with:
tag: ${{ needs.update-deployment-state.outputs.release_tag }}
environment: ${{ needs.detect.outputs.env }}
secrets:
INFRA_WORKFLOW_TOKEN: ${{ secrets.INFRA_WORKFLOW_TOKEN }}