-
Notifications
You must be signed in to change notification settings - Fork 0
354 lines (312 loc) · 11.5 KB
/
Copy pathci.yml
File metadata and controls
354 lines (312 loc) · 11.5 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
name: CI
on:
pull_request:
branches:
- develop
- main
- "release/**"
- "rc/**"
push:
branches:
- develop
- main
- "feature/**"
- "release/**"
- "rc/**"
workflow_dispatch:
concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true
jobs:
validate:
name: validate
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Setup Node
uses: actions/setup-node@v6
with:
node-version: 22
cache: npm
- name: Install dependencies
run: npm ci
- name: Lint
run: npm run lint
- name: Format check
run: npm run format:check
- name: Typecheck
run: npm run typecheck
- name: Test
run: npm run test
- name: Build
run: npm run build
- name: Audit dependencies (block on high and above)
run: npm audit --audit-level=high
dependency-review:
name: dependency-review
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
permissions:
contents: read
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Dependency review (block on high and above)
uses: actions/dependency-review-action@v5
with:
fail-on-severity: high
image-scan:
name: image-scan
runs-on: ubuntu-latest
needs: validate
permissions:
contents: read
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Build image for scanning
uses: docker/build-push-action@v7
with:
context: .
load: true
tags: benchfinity:scan
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Scan image (block on fixable high and critical)
uses: aquasecurity/trivy-action@v0.36.0
with:
image-ref: benchfinity:scan
severity: HIGH,CRITICAL
ignore-unfixed: true
vuln-type: os,library
exit-code: "1"
format: table
docker:
name: docker
runs-on: ubuntu-latest
needs: [validate, image-scan]
if: github.event_name == 'push'
permissions:
contents: read
packages: write
outputs:
image: ${{ steps.meta.outputs.image }}
tags: ${{ steps.meta.outputs.tags }}
version: ${{ steps.meta.outputs.version }}
release_tag: ${{ steps.meta.outputs.release_tag }}
release_name: ${{ steps.meta.outputs.release_name }}
deploy_tag: ${{ steps.meta.outputs.deploy_tag }}
prerelease: ${{ steps.meta.outputs.prerelease }}
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Setup Node
uses: actions/setup-node@v6
with:
node-version: 22
- name: Compute image tags
id: meta
shell: bash
run: |
version="$(node -p "require('./package.json').version")"
short_sha="${GITHUB_SHA::7}"
image="ghcr.io/${GITHUB_REPOSITORY,,}"
branch="${GITHUB_REF_NAME}"
release_tag=""
release_name=""
deploy_tag=""
prerelease="false"
if [[ "${branch}" == "main" ]]; then
# Publish an immutable :<version>-<sha> that production pins to,
# so every release changes CD desired state and ArgoCD rolls it out.
tags="${image}:${version}"$'\n'"${image}:${version}-${short_sha}"
deploy_tag="${version}-${short_sha}"
release_tag="v${version}"
release_name="Benchfinity v${version}"
elif [[ "${branch}" == "develop" ]]; then
tags="${image}:${version}-SNAPSHOT.${short_sha}"$'\n'"${image}:develop"
elif [[ "${branch}" == release/* || "${branch}" == rc/* ]]; then
tags="${image}:${version}-rc.${short_sha}"
release_tag="v${version}-rc.${short_sha}"
release_name="Benchfinity v${version} RC ${short_sha}"
prerelease="true"
elif [[ "${branch}" == feature/* ]]; then
safe_branch="$(printf '%s' "${branch#feature/}" | tr '[:upper:]' '[:lower:]' | sed -E 's#[^a-z0-9_.-]+#-#g; s#-+#-#g; s#(^-|-$)##g')"
tags="${image}:${version}-${safe_branch}-${short_sha}-SNAPSHOT"
else
safe_branch="$(printf '%s' "${branch}" | tr '[:upper:]' '[:lower:]' | sed -E 's#[^a-z0-9_.-]+#-#g; s#-+#-#g; s#(^-|-$)##g')"
tags="${image}:${version}-${safe_branch}-${short_sha}-SNAPSHOT"
fi
{
echo "image=${image}"
echo "version=${version}"
echo "release_tag=${release_tag}"
echo "release_name=${release_name}"
echo "deploy_tag=${deploy_tag}"
echo "prerelease=${prerelease}"
echo "tags<<TAGS"
printf '%s\n' "${tags}"
echo "TAGS"
} >> "${GITHUB_OUTPUT}"
- name: Set up QEMU
uses: docker/setup-qemu-action@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Login to GHCR
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push image
uses: docker/build-push-action@v7
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: |
org.opencontainers.image.title=Benchfinity
org.opencontainers.image.description=Gridfinity-compatible baseplate generator
org.opencontainers.image.source=https://github.com/${{ github.repository }}
org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.version=${{ steps.meta.outputs.version }}
cache-from: type=gha
cache-to: type=gha,mode=max
release:
name: release
runs-on: ubuntu-latest
needs: docker
if: github.event_name == 'push' && needs.docker.outputs.release_tag != ''
permissions:
contents: write
steps:
- name: Publish GitHub Release
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TAG: ${{ needs.docker.outputs.release_tag }}
RELEASE_NAME: ${{ needs.docker.outputs.release_name }}
PRERELEASE: ${{ needs.docker.outputs.prerelease }}
IMAGE: ${{ needs.docker.outputs.image }}
IMAGE_TAGS: ${{ needs.docker.outputs.tags }}
shell: bash
run: |
if gh release view "${TAG}" -R "${GITHUB_REPOSITORY}" >/dev/null 2>&1; then
echo "Release ${TAG} already exists"
exit 0
fi
body="$(printf 'Container image:\n\n%s\n\nCommit: %s\n' "${IMAGE_TAGS}" "${GITHUB_SHA}")"
gh api -X POST "repos/${GITHUB_REPOSITORY}/releases" \
-f tag_name="${TAG}" \
-f target_commitish="${GITHUB_SHA}" \
-f name="${RELEASE_NAME}" \
-f body="${body}" \
-F prerelease="${PRERELEASE}" \
-F generate_release_notes=true
helm:
name: helm
runs-on: ubuntu-latest
needs: validate
if: github.event_name == 'push'
permissions:
contents: read
packages: write
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Setup Helm
uses: azure/setup-helm@v5
- name: Lint chart
run: helm lint deploy/helm/benchfinity
- name: Template chart
run: helm template benchfinity deploy/helm/benchfinity > /dev/null
- name: Compute chart version
id: chart
shell: bash
run: |
base="$(awk '/^version:/ { print $2; exit }' deploy/helm/benchfinity/Chart.yaml)"
short_sha="${GITHUB_SHA::7}"
branch="${GITHUB_REF_NAME}"
publish="false"
if [[ "${branch}" == "main" || "${GITHUB_REF}" == refs/tags/* ]]; then
version="${base}"
publish="true"
elif [[ "${branch}" == "develop" ]]; then
version="${base}-SNAPSHOT.${short_sha}"
publish="true"
else
version="${base}-${short_sha}"
fi
{
echo "version=${version}"
echo "publish=${publish}"
} >> "${GITHUB_OUTPUT}"
- name: Package and push chart to GHCR
if: steps.chart.outputs.publish == 'true'
shell: bash
env:
CHART_VERSION: ${{ steps.chart.outputs.version }}
REGISTRY_USER: ${{ github.actor }}
REGISTRY_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
helm package deploy/helm/benchfinity \
--version "${CHART_VERSION}" \
--app-version "${CHART_VERSION}"
helm registry login ghcr.io -u "${REGISTRY_USER}" -p "${REGISTRY_TOKEN}"
helm push "benchfinity-${CHART_VERSION}.tgz" oci://ghcr.io/benchfinity/charts
deploy:
name: deploy
runs-on: ubuntu-latest
needs: docker
# Only production (main) auto-deploys, once the immutable image is published.
# Bumps the pinned tag in Workbench-CD; ArgoCD (benchfinity-prod) reconciles
# it onto k8s-prod. Uses the SSH deploy key, not GITHUB_TOKEN.
if: github.event_name == 'push' && needs.docker.outputs.deploy_tag != ''
permissions:
contents: read
steps:
- name: Roll production by bumping Workbench-CD image tag
env:
DEPLOY_TAG: ${{ needs.docker.outputs.deploy_tag }}
DEPLOY_KEY: ${{ secrets.WORKBENCH_CD_DEPLOY_KEY }}
SOURCE_SHA: ${{ github.sha }}
CD_REPO: BenchFinity/Workbench-CD
CD_BRANCH: main
OVERLAY: overlays/production/kustomization.yaml
IMAGE_NAME: ghcr.io/benchfinity/workbench
shell: bash
run: |
set -euo pipefail
if [[ -z "${DEPLOY_KEY}" ]]; then
echo "::error::WORKBENCH_CD_DEPLOY_KEY secret is not set; cannot push to ${CD_REPO}." \
"Add a write deploy key to ${CD_REPO} and store its private key as that secret."
exit 1
fi
mkdir -p ~/.ssh
printf '%s\n' "${DEPLOY_KEY}" > ~/.ssh/cd_deploy_key
chmod 600 ~/.ssh/cd_deploy_key
ssh-keyscan -t ed25519,rsa github.com >> ~/.ssh/known_hosts 2>/dev/null
export GIT_SSH_COMMAND="ssh -i ~/.ssh/cd_deploy_key -o IdentitiesOnly=yes"
git clone --depth 1 --branch "${CD_BRANCH}" "git@github.com:${CD_REPO}.git" cd-repo
cd cd-repo
# DEPLOY_TAG and IMAGE_NAME are CI-derived (package.json version + commit
# SHA), not user-controlled input, so they are safe to pass to yq via env.
DEPLOY_TAG="${DEPLOY_TAG}" IMAGE_NAME="${IMAGE_NAME}" yq -i \
'(.images[] | select(.name == strenv(IMAGE_NAME)) | .newTag) = strenv(DEPLOY_TAG)' \
"${OVERLAY}"
if git diff --quiet -- "${OVERLAY}"; then
echo "Production already pinned to ${DEPLOY_TAG}; nothing to deploy."
exit 0
fi
git config user.name "benchfinity-workbench-ci[bot]"
git config user.email "ci@benchfinity.com"
git add "${OVERLAY}"
git commit -m "deploy(workbench): roll production to ${DEPLOY_TAG}" \
-m "Auto-bumped by Workbench CI from BenchFinity/Workbench@${SOURCE_SHA}. ArgoCD reconciles."
git push origin "HEAD:${CD_BRANCH}"
echo "Pushed ${DEPLOY_TAG} to ${CD_REPO}@${CD_BRANCH}; ArgoCD will roll it out."