-
Notifications
You must be signed in to change notification settings - Fork 1
271 lines (262 loc) · 10.7 KB
/
build-and-push.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
name: Build services
on:
push:
paths:
- "go.mod"
- ".github/workflows/**"
- "build/**"
- "database/**"
- "pkg/**"
- "internal/**"
- "cmd/**"
# https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/control-the-concurrency-of-workflows-and-jobs#example-only-cancel-in-progress-jobs-on-specific-branches
# https://stackoverflow.com/questions/66335225/how-to-cancel-previous-runs-in-the-pr-when-you-push-new-commitsupdate-the-curre
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
jobs:
check-test-names:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Detect tests not matching naming pattern
id: matching
# https://unix.stackexchange.com/questions/330660/preventing-grep-from-causing-premature-termination-of-bash-e-script
run: |
grep -rh 'func Test' --include *_test.go > all_tests.txt
sed -i 's/func //g' all_tests.txt
cat all_tests.txt | cut -d'(' -f1 > test_names.txt
grep -v TestMain test_names.txt > test_without_main.txt
grep -vE '^Test(Unit|IT|)_' test_without_main.txt > not_matching.txt || [[ $? == 1 ]]
FAILURES=$(wc -l < not_matching.txt)
if [[ ${FAILURES} -ne 0 ]]; then
echo 'failed=true' >> $GITHUB_OUTPUT
fi
- name: Fail if some do not match
if: ${{ steps.matching.outputs.failed == 'true' }}
# https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#example-setting-an-error-message
run: |
echo "::error ::Detected at least one test not matching the naming convention!"
exit 1
tests:
runs-on: ubuntu-latest
needs: [check-test-names]
steps:
- uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: "1.23.2"
- name: Run tests with coverage
run: go test -run='^TestUnit_.*' ./... -coverpkg=./... -race -covermode=atomic -coverprofile=coverage.out
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v5
with:
fail_ci_if_error: true
files: ./coverage.out
token: ${{ secrets.CODECOV_TOKEN }}
it-tests:
runs-on: ubuntu-latest
needs: [check-test-names]
services:
postgres:
image: postgres:15
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: "1.23.2"
- name: Install migrate tool
run: go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
- name: Setup galactic-sovereign database
run: |
psql \
postgresql://postgres@localhost \
-v admin_password=$ADMIN_PASSWORD \
-v manager_password=$MANAGER_PASSWORD \
-v user_password=$USER_PASSWORD \
-f galactic-sovereign/db_user_create.sql
psql \
postgresql://postgres@localhost \
-f galactic-sovereign/db_create.sql
working-directory: ./database
env:
PGPASSWORD: postgres
ADMIN_PASSWORD: admin_password
MANAGER_PASSWORD: manager_password
USER_PASSWORD: user_password
- name: Migrate schema up
run: |
migrate \
-path galactic-sovereign/migrations \
-database postgresql://galactic_sovereign_admin@localhost/db_galactic_sovereign?sslmode=disable \
up
working-directory: ./database
env:
PGPASSWORD: admin_password
- name: Run tests with coverage
run: go test -run='^TestIT_.*' ./... -coverpkg=./... -race -covermode=atomic -coverprofile=coverage.out
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v5
with:
fail_ci_if_error: true
files: ./coverage.out
token: ${{ secrets.CODECOV_TOKEN }}
extract-service-tag:
runs-on: ubuntu-latest
# https://docs.github.com/en/actions/using-jobs/defining-outputs-for-jobs
outputs:
version: ${{ steps.service-version.outputs.tag }}
steps:
- uses: actions/checkout@v4
- name: Extract git commit hash
id: service-version
# https://stackoverflow.com/questions/58886293/getting-current-branch-and-commit-hash-in-github-action
run: echo "tag=$(git rev-parse --short $GITHUB_SHA)" >> $GITHUB_OUTPUT
build-and-push-docker-image:
runs-on: ubuntu-latest
needs: [extract-service-tag, tests, it-tests]
steps:
- uses: actions/checkout@v4
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
# https://docs.github.com/en/actions/publishing-packages/publishing-docker-images#publishing-images-to-docker-hub
- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
file: ./build/galactic-sovereign-service/Dockerfile
build-args: GIT_COMMIT_HASH=${{ needs.extract-service-tag.outputs.version }}
push: true
tags: totocorpsoftwareinc/galactic-sovereign-service:${{ needs.extract-service-tag.outputs.version }}
e2e-tests:
runs-on: ubuntu-latest
needs: [build-and-push-docker-image, extract-service-tag]
services:
postgres:
image: postgres:15
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: "1.23.2"
- name: Install migrate tool
run: go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
- name: Setup galactic-sovereign database
run: |
psql \
postgresql://postgres@localhost \
-v admin_password=$ADMIN_PASSWORD \
-v manager_password=$MANAGER_PASSWORD \
-v user_password=$USER_PASSWORD \
-f galactic-sovereign/db_user_create.sql
psql \
postgresql://postgres@localhost \
-f galactic-sovereign/db_create.sql
working-directory: ./database
env:
PGPASSWORD: postgres
ADMIN_PASSWORD: admin_password
MANAGER_PASSWORD: manager_password
USER_PASSWORD: user_password
- name: Migrate schema up
run: |
migrate \
-path galactic-sovereign/migrations \
-database postgresql://galactic_sovereign_admin@localhost/db_galactic_sovereign?sslmode=disable \
up
working-directory: ./database
env:
PGPASSWORD: admin_password
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Pull latest docker image
run: |
docker pull totocorpsoftwareinc/galactic-sovereign-service:${{ needs.extract-service-tag.outputs.version }}
- name: Test docker image
# https://superuser.com/questions/272265/getting-curl-to-output-http-status-code
# https://stackoverflow.com/questions/24254064/how-to-get-curl-to-output-only-http-response-body-json-and-no-other-headers-et
# https://docs.github.com/en/actions/use-cases-and-examples/using-containerized-services/about-service-containers#running-jobs-on-the-runner-machine
run: |
docker run \
-d \
--name galactic-sovereign-service-test \
-e ENV_SERVER_PORT=$PORT \
-e ENV_DATABASE_PASSWORD=$DATABASE_PASSWORD \
-p 1234:1234 \
totocorpsoftwareinc/galactic-sovereign-service:${{ needs.extract-service-tag.outputs.version }}
echo "status_code=$(curl -s -o /dev/null -w %{http_code} localhost:1234/v1/galactic-sovereign/universes)" >> $GITHUB_OUTPUT
echo "response_body=$(curl -s localhost:1234/v1/galactic-sovereign/universes)" >> $GITHUB_OUTPUT
docker stop galactic-sovereign-service-test
id: test-results
env:
PORT: 1234
DATABASE_PASSWORD: manager_password
- name: Print test results
run: |
echo ${{ steps.test-results.outputs.status_code }}
echo ${{ steps.test-results.outputs.response_body }}
- name: Verify status code
run: |
if [[ ${{ steps.test-results.outputs.status_code }} != '200' ]]; then
echo "::error ::Expected http status code to be 200 but it was ${{ steps.test-results.outputs.status_code }}!"
exit 1
fi
- name: Verify response body
# https://stackoverflow.com/questions/58862864/github-actions-ci-conditional-regex
# https://stackoverflow.com/questions/4542732/how-do-i-negate-a-test-with-regular-expressions-in-a-bash-script
run: |
if ! [[ '${{ steps.test-results.outputs.response_body }}' =~ ^\{\"requestId\":\"[0-9a-f-]+\",\"status\":\"SUCCESS\",\"details\":\[[^]]+\]\}$ ]]; then
echo "::error ::Expected http response to match expected syntax but it did not: ${{ steps.test-results.outputs.response_body }}!"
exit 1
fi
update-deployment:
runs-on: ubuntu-latest
needs: [extract-service-tag, e2e-tests]
if: github.ref == 'refs/heads/master'
steps:
- uses: actions/checkout@v4
with:
repository: "Knoblauchpilze/ec2-deployment"
# https://stackoverflow.com/questions/64374179/how-to-push-to-another-repository-in-github-actions
token: ${{ secrets.DEPLOYMENT_TOKEN }}
- name: Update service tag
run: |
echo "${{ needs.extract-service-tag.outputs.version }}" > ./build/galactic-sovereign-service/version.txt
- name: "Commit changes"
run: |
git pull
git config --global user.name 'totocorpbot'
git config --global user.email '[email protected]'
git commit -am "infra: Bumped galactic-sovereign-service version to ${{ needs.extract-service-tag.outputs.version }}"
git push