-
Notifications
You must be signed in to change notification settings - Fork 0
505 lines (430 loc) · 17.1 KB
/
python.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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
name: Python CI
on:
push:
branches:
- "*"
tags:
- '[0-9]+.[0-9]+.[0-9]+'
paths:
- "src/**/*.py"
- "requirements.txt"
- ".dockerignore"
- "./.docker/Dockerfile"
- "docker-compose*.yml"
- ".github/workflows/python.yml"
pull_request:
types: [opened, synchronize]
branches:
- "*"
paths:
- "src/**/*.py"
- "tests/**/*.py"
- "requirements.txt"
- ".dockerignore"
- "./.docker/Dockerfile"
- "docker-compose*.yml"
- ".github/workflows/python.yml"
workflow_dispatch:
env:
MODULE_NAME: "pyramid"
SRC: "./src"
TEST_DIR: "./tests"
REGISTRY_IMAGE: ${{ vars.DOCKERHUB_USERNAME }}/${{ vars.DOCKERHUB_REPO_NAME }}
REGISTRY_IMAGE_PRIVATE: ${{ vars.DOCKER_REGISTRY_PRIVATE_URL }}/${{ github.actor }}/${{ github.event.repository.name }}
jobs:
info:
name: "Informations"
runs-on: ubuntu-latest
outputs:
project_version: ${{ steps.environment.outputs.project_version }}
environment: ${{ steps.environment.outputs.git_environment }}
docker_tag: ${{ steps.environment.outputs.docker_tag }}
commit_id: ${{ steps.environment.outputs.commit_id }}
last_release_ref: ${{ steps.last_release.result.last_release_ref }}
changelog: ${{ steps.changelog.outputs.changelog }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Define build variables
id: environment
uses: actions/github-script@v7
with:
script: |
const refType = '${{ github.ref_type }}';
const ref = context.ref;
const sha = context.sha;
const shortSha = sha.slice(0, 7);
let dockerTag;
let gitEnvironment;
let projectVersion;
if (refType === 'tag') {
dockerTag = 'latest';
gitEnvironment = 'production';
projectVersion = '${{ github.ref_name }}';
} else if (ref === 'refs/heads/main') {
dockerTag = 'pre-prod';
gitEnvironment = 'pre-production';
projectVersion = `${shortSha}`;
} else {
dockerTag = 'dev';
gitEnvironment = 'staging';
projectVersion = `${shortSha}`;
}
const reset = "\x1b[0m";
const textColor = "\x1b[36m"; // Cyan for static text
const varColor = "\x1b[35m"; // Magenta for variables
console.log(`${textColor}${projectVersion} in environment ${varColor}${gitEnvironment}${textColor} with tag ${varColor}${dockerTag}${reset}.`);
core.setOutput("docker_tag", dockerTag);
core.setOutput("git_environment", gitEnvironment);
core.setOutput("commit_id", shortSha);
core.setOutput("project_version", projectVersion);
- name: Get Github last release name
id: last_release
uses: actions/github-script@v7
with:
script: |
const latestRelease = await github.rest.repos.getLatestRelease({
owner: context.repo.owner,
repo: context.repo.repo,
});
const reset = "\x1b[0m";
const textColor = "\x1b[36m"; // Cyan for static text
const varColor = "\x1b[35m"; // Magenta for variables
console.log(`${textColor}The last release is ${varColor}${latestRelease.data.tag_name}${textColor}.${reset}`);
core.setOutput("last_release_ref", latestRelease.data.tag_name);
- name: Generate Changelog
id: changelog
uses: actions/github-script@v7
with:
script: |
const lastRelease = '${{ steps.last_release.outputs.last_release_ref }}';
const currentSha = context.sha;
const pulls = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'closed',
per_page: 100,
});
const mergedPulls = pulls.data.filter(pr =>
pr.merged_at &&
pr.merge_commit_sha >= lastRelease &&
pr.merge_commit_sha <= currentSha
);
let changes = "## What's Changed\n";
let contributorsNames = new Set();
mergedPulls.forEach(pr => {
changes += `* ${pr.title} ${pr.html_url}\n`;
contributorsNames.add(pr.user.login);
});
let contributors = `### Contributors
Thanks to @${Array.from(contributorsNames).join(', @')}.\n`;
const fullChangelog = `**Full Changelog**: https://github.com/${context.repo.owner}/${context.repo.repo}/compare/${lastRelease}...${currentSha}`;
const changelog = changes + '\n' + (contributorsNames.size ? contributors + '\n' : '') + fullChangelog;
const reset = "\x1b[0m";
const textColor = "\x1b[36m"; // Cyan for static text
const varColor = "\x1b[35m"; // Magenta for variables
console.log(`${textColor}Changelog:${reset}\n${changelog}`);
core.setOutput("changelog", changelog);
- name: Output Changelog
run: echo "${{ steps.changelog.outputs.changelog }}"
docker_image_build:
name: "Build"
needs: ["info"]
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
platform:
- linux/amd64
- linux/arm64
steps:
- name: Prepare
run: |
platform=${{ matrix.platform }}
echo "platform_pair=${platform//\//-}" >> $GITHUB_OUTPUT
id: prepare
- name: Checkout repository
uses: actions/checkout@v4
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY_IMAGE }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Docker image Build
id: build
uses: docker/build-push-action@v6
with:
file: ./.docker/Dockerfile
target: executable
context: .
platforms: ${{ matrix.platform }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
provenance: mode=max
build-args: |
PROJECT_VERSION=${{ needs.info.outputs.project_version }}
outputs: |
type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=true
- name: Export image digest
run: |
echo "Digest: ${{ steps.build.outputs.digest }}"
mkdir -p /tmp/digests
digest="${{ steps.build.outputs.digest }}"
touch "/tmp/digests/${digest#sha256:}"
- name: Upload image digest as artifact
uses: actions/upload-artifact@v4
with:
name: digests-${{ steps.prepare.outputs.platform_pair }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
- name: Export image to tar
uses: docker/build-push-action@v6
with:
file: ./.docker/Dockerfile
target: executable
context: .
platforms: ${{ matrix.platform }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
provenance: mode=max
build-args: |
PROJECT_VERSION=${{ needs.info.outputs.project_version }}
outputs: |
type=oci,dest=./pyramid-oci.tar
- name: Upload image as artifact
uses: actions/upload-artifact@v4
with:
name: image-${{ steps.prepare.outputs.platform_pair }}
path: ./pyramid-oci.tar
if-no-files-found: error
retention-days: 1
security_scanner:
name: "Security scan"
needs: ["docker_image_build"]
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
platform:
- linux/amd64
- linux/arm64
steps:
- name: Prepare
run: |
platform=${{ matrix.platform }}
echo "platform_pair=${platform//\//-}" >> $GITHUB_OUTPUT
id: prepare
- name: Download tests image
uses: actions/download-artifact@v4
with:
name: image-${{ steps.prepare.outputs.platform_pair }}
- name: Untar OCI Image
run: |
mkdir -p ./pyramid-oci
tar -xvf ./pyramid-oci.tar -C ./pyramid-oci
- name: Run Trivy vulnerability scanner
uses: aquasecurity/[email protected]
with:
input: './pyramid-oci'
format: 'github'
output: 'dependency-results.sbom.json'
github-pat: '${{ secrets.GITHUB_TOKEN }}'
env:
TRIVY_DB_REPOSITORY: 'public.ecr.aws/aquasecurity/trivy-db:2'
TRIVY_JAVA_DB_REPOSITORY: 'public.ecr.aws/aquasecurity/trivy-java-db:1'
- name: Print Trivy result in humain format
uses: aquasecurity/[email protected]
with:
input: './pyramid-oci'
format: 'table'
exit-code: '1'
# ignore-unfixed: true
severity: 'UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL'
env:
TRIVY_DB_REPOSITORY: 'public.ecr.aws/aquasecurity/trivy-db:2'
TRIVY_JAVA_DB_REPOSITORY: 'public.ecr.aws/aquasecurity/trivy-java-db:1'
- name: Upload trivy report as a Github artifact
uses: actions/upload-artifact@v4
with:
name: trivy-sbom-${{ steps.prepare.outputs.platform_pair }}-report
path: '${{ github.workspace }}/dependency-results.sbom.json'
retention-days: 90
docker_image_test_build:
name: "Build Tests"
needs: ["info", "docker_image_build"]
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Docker image Build
id: build
uses: docker/build-push-action@v6
with:
file: ./.docker/Dockerfile
target: tests
context: .
tags: pyramid:tests
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
PROJECT_VERSION=${{ needs.info.outputs.project_version }}-tests
push: false
outputs: type=docker
- name: Export tests image
run: docker save pyramid:tests -o "./pyramid-tests.tar"
- name: Upload tests image as artifact
uses: actions/upload-artifact@v4
with:
name: image-tests
path: ./pyramid-tests.tar
if-no-files-found: error
retention-days: 1
tests_in_docker:
name: "Tests"
needs: ["docker_image_test_build"]
runs-on: ubuntu-latest
steps:
- name: Download tests image
uses: actions/download-artifact@v4
with:
name: image-tests
- name: Load Docker image
run: docker load -i "./pyramid-tests.tar"
- name: Run unit tests
run: |
mkdir -p ./coverage && chmod 777 ./coverage
docker run --rm -v ./coverage:/app/coverage -e DEEZER__ARL=${{ secrets.CONFIG_DEEZER_ARL }} -e SPOTIFY__CLIENT_ID=${{ secrets.CONFIG_SPOTIFY_CLIENT_ID }} -e SPOTIFY__CLIENT_SECRET=${{ secrets.CONFIG_SPOTIFY_CLIENT_SECRET }} pyramid:tests
- name: Upload coverage reports to Codecov
uses: codecov/[email protected]
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: tristiisch/PyRamid
files: ./coverage/coverage1.xml
fail_ci_if_error: true
docker_image_push:
name: "Push"
runs-on: ubuntu-latest
needs: ["info", "docker_image_build", "tests_in_docker"]
if: github.event_name == 'push'
steps:
- name: Download digests
uses: actions/download-artifact@v4
with:
path: /tmp/digests
pattern: digests-*
merge-multiple: true
- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Docker meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY_IMAGE }}
tags: |
type=raw,value=${{ needs.info.outputs.docker_tag }}
type=raw,value=${{ needs.info.outputs.project_version }}
- name: Create manifest list and push
working-directory: /tmp/digests
run: docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") $(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *)
docker_image_push_private:
name: "Push Privates"
runs-on: ubuntu-latest
needs: ["info", "docker_image_build", "tests_in_docker"]
if: github.event_name == 'push'
steps:
- name: Download digests
uses: actions/download-artifact@v4
with:
path: /tmp/digests
pattern: digests-*
merge-multiple: true
- name: Login to Github Container Registry
uses: docker/login-action@v3
with:
registry: ${{ vars.DOCKER_REGISTRY_PRIVATE_URL }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Docker meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY_IMAGE_PRIVATE }}
tags: |
type=raw,value=${{ needs.info.outputs.docker_tag }}
type=raw,value=${{ needs.info.outputs.project_version }}
- name: Create manifest list and push
working-directory: /tmp/digests
run: docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") $(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *)
docker_swarm_deploy:
name: "Deploy"
needs: ["info", "docker_image_push"]
runs-on: ubuntu-latest
environment: ${{ needs.info.outputs.environment }}
if: github.event_name == 'push' && (github.ref_type == 'tag' && needs.info.outputs.docker_tag == 'latest') || (github.ref_type == 'branch' && needs.info.outputs.docker_tag != 'latest')
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Deploy ${{ needs.info.outputs.project_version }} to ${{ needs.info.outputs.environment }}
uses: tristiisch/docker-stack-deployment@master
with:
deployment_mode: docker-swarm
remote_docker_host: ${{ secrets.MANAGER_HOST }}
remote_docker_username: ${{ secrets.MANAGER_TO_USER }}
ssh_private_key: ${{ secrets.MANAGER_TO_SSH_PRIVATE_KEY }}
ssh_public_key: ${{ secrets.MANAGER_SSH_PUBLIC_KEY }}
stack_file_path: ${{ vars.DOCKER_COMPOSE_FILENAME }}
stack_name: ${{ vars.DOCKER_STACK_NAME }}
secrets: ${{ vars.DOCKER_COMPOSE_SERVICE}} ${{ vars.DOCKER_STACK_NAME }} DISCORD__TOKEN ${{ secrets.CONFIG_DISCORD_TOKEN }} DEEZER__ARL ${{ secrets.CONFIG_DEEZER_ARL }} SPOTIFY__CLIENT_ID ${{ secrets.CONFIG_SPOTIFY_CLIENT_ID }} SPOTIFY__CLIENT_SECRET ${{ secrets.CONFIG_SPOTIFY_CLIENT_SECRET }}
release_publish:
name: "Release"
needs: ["info", "docker_image_push"]
runs-on: ubuntu-latest
if: github.ref_type == 'tag' && github.event_name == 'push' && needs.info.outputs.docker_tag == 'latest'
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Update Release v${{ needs.info.outputs.project_version }}
run: |
set -eu
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
cat <<$EOF > ./changelog.txt
The latest version of the Discord bot PyRamid#6882 has been successfully deployed.
To start using this updated version, please follow the instructions provided at [PyRamid Usage Guide](https://github.com/tristiisch/PyRamid/#usage).
${{ needs.info.outputs.changelog }}
## Docker
This version is now accessible through various Docker images. Each image creation corresponds to a unique snapshot of this version, while updating the image corresponds to using an updated Docker image tag.
### Images availables
* \`${{ env.REGISTRY_IMAGE }}:${{ needs.info.outputs.docker_tag }}\`
* \`${{ env.REGISTRY_IMAGE }}:${{ needs.info.outputs.project_version }}\`
$EOF
gh release edit "${{ github.ref_name }}" \
--title "Release v${{ needs.info.outputs.project_version }}" \
--draft=false \
--prerelease=false \
--notes-file "./changelog.txt"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}