forked from sourcenetwork/defradb
-
Notifications
You must be signed in to change notification settings - Fork 0
321 lines (266 loc) · 12 KB
/
lint-then-benchmark.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
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
# Copyright 2022 Democratized Data Foundation
#
# Use of this software is governed by the Business Source License
# included in the file licenses/BSL.txt.
#
# As of the Change Date specified in that file, in accordance with
# the Business Source License, use of this software will be governed
# by the Apache License, Version 2.0, included in the file
# licenses/APL.txt.
name: Lint Then Benchmark Workflow
on:
pull_request:
push:
tags:
- 'v[0-9]+.[0-9]+.[0-9]+'
branches:
- master
- develop
## These are the permissions for the lint check job.
permissions:
# Must have a secret (`secrets.ONLY_DEFRADB_REPO_CI_PAT`) setup with the following permissions:
# - Actions: Read-Write
# - Administration: Read-Write
# - Pull Request: Read-Write
# - Contents: Read-Only
# - Metadata: Read-Only
# - Secrets: Read-Only
# Allow read access to pull request (Required for the `only-new-issues` option.)
pull-requests: read
contents: read
env:
# This is the default benchmark type which if no labels are specified will be used.
DEFAULT_BENCHMARK_TYPE: SHORT
jobs:
# ========================================================= Step-1: Run the lint check.
golangci:
name: Lint check job
strategy:
matrix:
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout code into the directory
uses: actions/checkout@v4
# Setting up Go explicitly is required for v3.0.0+ of golangci/golangci-lint-action.
- name: Setup Go environment explicitly
uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
check-latest: true
cache: false
- name: Run the golangci-lint
uses: golangci/golangci-lint-action@v6
with:
# Required: the version of golangci-lint is required.
# Note: The version should not pick the patch version as the latest patch
# version is what will always be used.
version: v1.51
# Optional: working directory, useful for monorepos or if we wanted to run this
# on a non-root directory.
# working-directory: ./
# Optional: golangci-lint command line arguments.
# Note: we can set `--issues-exit-code=0` if we want a successcode always,
# indicating that the linter ran successfully (weather or not linter errors
# exist or not doesn't matter). But the good think is that the annotations
# will still show up. I think this can be useful if we don't want the pipeline
# to stop just because we had some linter errors.
args: --issues-exit-code=1 --config tools/configs/golangci.yaml
# Optional: we can set the below to `true` if we only want to see newly
# introduced linter errors, however I found that in practive that option is a
# bit gimmicky, as it passes the linter check despite having new linter errors
# in some cases. So we opt in for all annotations of linter errors to show up,
# this is actually nicer because we suppress our linter errors manually
# anyways so there shouldn't be any linter errors anyways. The enforces us to
# always have a clean lint state.
only-new-issues: false
# =================== Step-2: Decide what type of benchmarks to run based on label(s).
# This job acts like a switch to simplify our ci control flow later on.
decide-benchmark-type:
name: Decide which benchmarks to run based on flags
strategy:
matrix:
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
outputs:
# Is either 'NONE', 'FULL', or 'SHORT'.
benchmark-type: ${{ steps.set-benchmark-type.outputs.type }}
needs:
- golangci # only run if the linter check passed.
steps:
- name: Check for full benchmark label
if: contains(github.event.pull_request.labels.*.name, 'action/full-benchmark')
run: echo "DEFAULT_BENCHMARK_TYPE=FULL" >> ${GITHUB_ENV}
- name: Check for label that skips the benchmark or non-develop branch
if: |
github.event_name == 'pull_request' &&
github.base_ref != 'develop' ||
contains(github.event.pull_request.labels.*.name, 'action/no-benchmark') ||
github.actor == 'dependabot[bot]'
run: echo "DEFAULT_BENCHMARK_TYPE=NONE" >> ${GITHUB_ENV}
- name: Run full benchmarks if merged PR (push event) on develop
if: |
github.event_name == 'push' &&
github.ref_name == 'develop'
run: echo "DEFAULT_BENCHMARK_TYPE=FULL" >> ${GITHUB_ENV}
- name: Set the output to be the benchmark type
id: set-benchmark-type
run: echo "::set-output name=type::${DEFAULT_BENCHMARK_TYPE}"
# ================== Step-3: Start the runner and get it registered as a github runner.
start-runner:
name: Start self-hosted EC2 runner job
needs:
- golangci # only run if the linter check passed.
- decide-benchmark-type # type of benchmark to run.
if: needs.decide-benchmark-type.outputs.benchmark-type != 'NONE'
runs-on: ubuntu-latest
outputs:
label: ${{ steps.start-ec2-runner.outputs.label }}
ec2-instance-id: ${{ steps.start-ec2-runner.outputs.ec2-instance-id }}
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Start EC2 runner
id: start-ec2-runner
uses: machulav/ec2-github-runner@v2
with:
mode: start
github-token: ${{ secrets.ONLY_DEFRADB_REPO_CI_PAT }}
ec2-image-id: ${{ secrets.EC2_IMAGE_ID }}
ec2-instance-type: t3.xlarge
subnet-id: ${{ secrets.SUBNET_ID }}
security-group-id: ${{ secrets.SECURITY_GROUP_ID }}
## iam-role-name: my-role-name # optional, requires additional permissions
## aws-resource-tags: > # optional, requires additional permissions
## [
## {"Key": "Name", "Value": "ec2-github-runner"},
## {"Key": "GitHubRepository", "Value": "${{ github.repository }}"}
## ]
# =========================== Step-4: Run the benchmarks on the runner we just started.
benchmark-ec2-runner:
name: Run the benchmarks on the started EC2 runner
needs:
- golangci # only run if the linter check passed.
- decide-benchmark-type # type of benchmark to run.
- start-runner # required to start the main job when the runner is ready.
if: needs.decide-benchmark-type.outputs.benchmark-type != 'NONE'
runs-on: ${{ needs.start-runner.outputs.label }} # run the job on the newly created runner
env:
# This is also the same directory as `$GITHUB_WORKSPACE/..`
HOME: /actions-runner/_work
GOPATH: /actions-runner/_work/go
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Run the full bechmarking suite
if: needs.decide-benchmark-type.outputs.benchmark-type == 'FULL'
run: make test:bench -s | tee current.txt
- name: Run only the shorter benchmarks
if: needs.decide-benchmark-type.outputs.benchmark-type == 'SHORT'
run: make test:bench-short -s | tee current.txt
# ------------- Upload Report - If pushed on develop branch
- name: Prepare artifact for uploading for a push on develop
if: |
github.event_name == 'push' &&
github.ref_name == 'develop'
run: cp current.txt bench-artifact-${{ github.sha }}.txt
- name: Upload artifact for push event on develop
if: |
github.event_name == 'push' &&
github.ref_name == 'develop'
uses: actions/upload-artifact@v4
with:
name: bench-artifact-${{ github.sha }}
path: bench-artifact-${{ github.sha }}.txt
retention-days: 90
# ------------- Download & Compare - If PR going into develop
# To find the latest commit on remote develop branch we essentially
# just need to do the following:
# >> git fetch origin develop > /dev/null 2>&1 \
# && git rev-parse origin/develop
# However I wanted to ensure that we get the latest commit on develop
# that has a passing `lint-then-benchmark.yml` workflow as well. To
# ensure that the artifact was successfully uploaded of the latest
# commit that is available on develop. Hence the use of this action.
- name: Find the last successfull commit's sha on remote of develop
if: |
github.event_name == 'pull_request' &&
github.base_ref == 'develop'
uses: nrwl/last-successful-commit-action@v1
id: last_successful_upload_on_develop
with:
branch: develop
workflow_id: lint-then-benchmark.yml
github_token: ${{ secrets.ONLY_DEFRADB_REPO_CI_PAT }}
- name: Download the latest benchmark artifact on develop
if: |
github.event_name == 'pull_request' &&
github.base_ref == 'develop'
uses: dawidd6/action-download-artifact@v6
with:
github_token: ${{ secrets.ONLY_DEFRADB_REPO_CI_PAT }}
workflow: lint-then-benchmark.yml
branch: develop
name: bench-artifact-${{ steps.last_successful_upload_on_develop.outputs.commit_hash }}
repo: ${{ github.repository }}
check_artifacts: false
search_artifacts: false
- name: Prepare benchmark reports for comparisons
if: |
github.event_name == 'pull_request' &&
github.base_ref == 'develop'
run: >
make deps:bench &&
cp bench-artifact-${{ steps.last_successful_upload_on_develop.outputs.commit_hash }}.txt develop.txt &&
sed -i '/^pkg: /s/^pkg:\ .*\/bench\//pkg:\ /g' develop.txt &&
sed -i '/^pkg: /s/^pkg:\ .*\/bench\//pkg:\ /g' current.txt
- name: Run the benchmark comparisons
if: |
github.event_name == 'pull_request' &&
github.base_ref == 'develop'
run: >
${GOPATH}/bin/benchstat -html -alpha 1.1 develop.txt current.txt |
sed -n "/<body>/,/<\/body>/p" > comparison.html &&
./tools/scripts/pretty-benchstat-html.sh comparison.html > pretty-comparison.md
- name: Comment Benchmark Results on PR
if: |
github.event_name == 'pull_request' &&
github.base_ref == 'develop'
uses: machine-learning-apps/pr-comment@master
env:
GITHUB_TOKEN: ${{ secrets.ONLY_DEFRADB_REPO_CI_PAT }}
with:
path: pretty-comparison.md
# =============================== Step-5: Stop the runner once the benchmarks have ran.
stop-runner:
name: Stop self-hosted EC2 runner
needs:
- golangci # only run if the linter check passed.
- decide-benchmark-type # type of benchmark to run.
- start-runner # required to get output from the start-runner job.
- benchmark-ec2-runner # required to wait when the main job is done.
# Stop the runner even if an error happened in the previous jobs. Also ensure that
# if the EC2 runner was actually started, only then we stop it.
if: |
always() &&
needs.start-runner.result == 'success' &&
needs.decide-benchmark-type.outputs.benchmark-type != 'NONE'
runs-on: ubuntu-latest
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Stop EC2 runner
uses: machulav/ec2-github-runner@v2
with:
mode: stop
github-token: ${{ secrets.ONLY_DEFRADB_REPO_CI_PAT }}
label: ${{ needs.start-runner.outputs.label }}
ec2-instance-id: ${{ needs.start-runner.outputs.ec2-instance-id }}