-
Notifications
You must be signed in to change notification settings - Fork 116
278 lines (265 loc) · 9.07 KB
/
ci.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
name: CI
on:
push:
# on:
# workflow_dispatch:
# pull_request:
# push:
# branches: [main]
permissions:
pull-requests: write
contents: write
env:
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
CI: true
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
PARALLEL_TEST_FIRST_IS_1: true
jobs:
docker-build:
runs-on: ubuntu-latest
steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build prod Docker image
uses: docker/build-push-action@v6
with:
push: false
cache-from: |
type=gha,scope=prod-${{ github.ref_name }}
type=gha,ref=main
cache-to: type=gha,scope=prod-${{ github.ref_name }},mode=max
file: Dockerfile
docker:
runs-on: ubuntu-latest
steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and export
uses: docker/build-push-action@v6
with:
push: false
tags: api_base/test:latest
cache-from: |
type=gha,scope=${{ github.ref_name }}
type=gha,ref=main
cache-to: type=gha,scope=${{ github.ref_name }},mode=max
outputs: type=docker,dest=/tmp/api_base_test_image.tar
file: Dockerfile.dev
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: api base test image
path: /tmp/api_base_test_image.tar
retention-days: 1
linters:
name: Linters
runs-on: ubuntu-latest
needs: docker
timeout-minutes: 10
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Download artifact
uses: actions/download-artifact@v4
with:
name: api base test image
path: /tmp
- name: Load image
run: |
docker load --input /tmp/api_base_test_image.tar
docker image ls -a
- name: Run Code Analysis
uses: ./.github/actions/docker-run
with:
run: bundle exec rails code:analysis
tests:
name: Tests
needs: docker
runs-on: ubuntu-latest
timeout-minutes: 20
env:
RAILS_ENV: test
POSTGRES_HOST: db
services:
db:
image: postgres:15
env:
POSTGRES_USER: postgres
POSTGRES_HOST_AUTH_METHOD: trust
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
chrome-server:
image: seleniarm/standalone-chromium
ports:
- 4444:4444
- 7900:7900
- 5900:5900
strategy:
fail-fast: false
matrix:
# Set N number of parallel jobs you want to run tests on.
# Use higher number if you have slow tests to split them on more parallel jobs.
# Remember to update ci_node_index below to 0..N-1
ci_node_total: [1]
# set N-1 indexes for parallel jobs
# When you run 2 parallel jobs then first job will have index 0, the second job will have index 1 etc
ci_node_index: [0]
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Download artifact
uses: actions/download-artifact@v4
with:
name: api base test image
path: /tmp
- name: Load image
run: |
docker load --input /tmp/api_base_test_image.tar
docker image ls -a
- name: Set ENV for API Docs
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
run: |
if [ $(git diff ${{ github.event.before }} ${{ github.event.after }} --name-only | grep 'spec/requests/api' | wc -l) -gt 0 ]; then
echo "OPENAPI=true" >> $GITHUB_ENV
fi
- name: Setup Code Climate test-reporter
run: |
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
chmod +x ./cc-test-reporter
./cc-test-reporter before-build
- name: Let Rails generate the secret_key_base
uses: ./.github/actions/docker-run
with:
run: bundle exec rails runner puts Rails.application.secret_key_base
- name: Get CPU info
id: cpu_info
run: echo "cpu_cores=$(nproc)" >> $GITHUB_ENV
- name: Setup Database
env:
PARALLEL_TESTS_CONCURRENCY: ${{ env.cpu_cores }}
uses: ./.github/actions/docker-run
with:
run: bundle exec rake parallel:create parallel:migrate
- name: Check for untracked changes in schema.rb
uses: rootstrap/check_untracked_changes@v1
with:
path: "./db/schema.rb"
- name: Get app assets from running container
run: |
docker run -it -d --name rails_app api_base/test:latest "sleep infinity"
docker container cp rails_app:/src/app/app/assets/builds/. app/assets/builds
docker rm -f rails_app
- name: Run Tests
env:
KNAPSACK_CI_NODE_TOTAL: ${{ matrix.ci_node_total }}
KNAPSACK_CI_NODE_INDEX: ${{ matrix.ci_node_index }}
PARALLEL_TESTS_CONCURRENCY: ${{ env.cpu_cores }}
HEADLESS: true
SELENIUM_BROWSER_HOST: http://chrome-server:4444
SELENIUM_BROWSER: remote
COVERAGE: true
uses: ./.github/actions/docker-run
with:
run: bundle exec parallel_rspec -n $PARALLEL_TESTS_CONCURRENCY -e './bin/parallel_tests'
- name: Check for missing annotations
uses: ./.github/actions/docker-run
with:
run: bundle exec annotate
- name: Check for untracked changes in app and spec directories
uses: rootstrap/check_untracked_changes@v1
with:
path: "./app/ ./spec/"
- name: Run LSs
run: |
ls
ls -a coverage
- name: Report to CodeClimate
run: ./cc-test-reporter format-coverage --output "coverage/coverage.${{ matrix.ci_node_index }}.json"
- name: Upload partial coverage
uses: actions/upload-artifact@v4
with:
name: coverage
path: "coverage/coverage.${{ matrix.ci_node_index }}.json"
- name: Merge API Docs from threads
env:
CI_NODE_INDEX: ${{ matrix.ci_node_index }}
if: ${{ env.OPENAPI }}
run: bundle exec ./bin/merge-api-docs.rb
- name: Upload partial API Docs
if: ${{ env.OPENAPI }}
uses: actions/upload-artifact@v4
with:
name: api_docs
path: "tmp/openapi${{ matrix.ci_node_index }}.yaml"
merge_results:
name: Merge Results
runs-on: ubuntu-latest
needs: tests
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
ssh-key: "${{ secrets.PUSH_KEY }}"
- name: Set ENV for CodeClimate
if: github.event_name == 'pull_request'
run: |
git fetch --no-tags --prune --depth=1 origin +refs/heads/$GITHUB_HEAD_REF:refs/remotes/origin/$GITHUB_HEAD_REF
echo "GIT_BRANCH=$GITHUB_HEAD_REF" >> $GITHUB_ENV
echo "GIT_COMMIT_SHA=$(git rev-parse origin/$GITHUB_HEAD_REF)" >> $GITHUB_ENV
echo "PULL_REQUEST_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV
- name: Set ENV for API Docs
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
run: |
if [ $(git diff ${{ github.event.before }} ${{ github.event.after }} --name-only | grep 'spec/requests/api' | wc -l) -gt 0 ]; then
echo "OPENAPI=true" >> $GITHUB_ENV
fi
- name: Setup Code Climate test-reporter
run: |
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
chmod +x ./cc-test-reporter
- name: Download coverage reports
uses: actions/download-artifact@v4
with:
name: coverage
path: coverage/coverage.*.json
- name: Report coverage
run: |
./cc-test-reporter sum-coverage coverage/**/*.json
./cc-test-reporter upload-coverage
- name: Download artifact
uses: actions/download-artifact@v4
with:
name: api base test image
path: /tmp
- name: Load image
run: |
docker load --input /tmp/api_base_test_image.tar
docker image ls -a
- name: Download API Docs
if: ${{ env.OPENAPI }}
uses: actions/download-artifact@v4
with:
name: api_docs
path: tmp/
- name: Merge API Docs from nodes
env:
MOVE_TMP_FILES: true
if: ${{ env.OPENAPI }}
uses: ./.github/actions/docker-run
with:
run: bundle exec ./bin/merge-api-docs.rb
- name: Commit & push API Docs
if: ${{ env.OPENAPI }}
run: |
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git add "doc/openapi.yaml"
git commit -m "Update API Docs" || echo "No changes to commit"
git push origin main