-
-
Notifications
You must be signed in to change notification settings - Fork 2
298 lines (281 loc) · 9.44 KB
/
devops.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
name: DevOps
on:
[push, pull_request]
env:
pythonLastVersion: '3.12'
GHCR_REGISTRY: ghcr.io
IMAGE_NAME: llaumgui/seedboxsync
jobs:
##############################################################################
# Test python application
#
test_python:
runs-on: ubuntu-latest
name: Test Python
strategy:
fail-fast: false
matrix:
python-version: ['3.9', '3.10', '3.11', '3.12', '3.13']
steps:
- name: Git checkout
uses: actions/checkout@v4
- name: Install Dependencies
run: pip install -r requirements-dev.txt
- name: Make all tests with make
run: |
make comply
python -m pytest -v --cov=seedboxsync --cov-report=term --cov-report=xml --capture=sys tests/
- uses: actions/upload-artifact@v4
with:
name: coverage-${{ matrix.python-version }}
path: coverage.xml
##############################################################################
# Markdownlint
#
test_markdownlint:
runs-on: ubuntu-latest
name: MarkdownLint
steps:
- name: Git checkout
uses: actions/checkout@v4
- name: markdownlint-cli
uses: nosborn/[email protected]
with:
files: "*.md docs/*.md"
config_file: ".markdownlint.yaml"
##############################################################################
# Dockerfile tests
#
test_dockerfiles:
if: ${{ github.event.schedule == '' }}
runs-on: ubuntu-latest
name: Linters for Dockerfile
steps:
- name: Git checkout
uses: actions/checkout@v4
- name: hadolint
uses: hadolint/[email protected]
with:
recursive: true
##############################################################################
# SonarCloud job
#
test_sonar:
if: ${{ github.event_name != 'pull_request' && github.actor != 'dependabot[bot]' }}
needs: [
test_python,
test_markdownlint,
test_dockerfiles
]
runs-on: ubuntu-latest
name: SonarCloud analyse
steps:
- name: Git checkout
uses: actions/checkout@v4
with:
fetch-depth: 0 # Disabling shallow clones is recommended for improving the relevancy of reporting
# https://docs.sonarsource.com/sonarqube/latest/project-administration/analysis-scope/#sonarqube-respects-ignored-files
- name: Remove .gitignore because SonarQube respects ignored files
run: rm .gitignore
- uses: actions/download-artifact@v4
with:
name: coverage-${{ env.pythonLastVersion }}
- name: Display structure of downloaded files
run: |
pwd
ls -R
cat coverage.xml
- name: SonarQube Scan
uses: SonarSource/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
#with:
# args: >
# -Dsonar.verbose=true
##############################################################################
# Package job
#
package:
needs: [
test_sonar
]
runs-on: ubuntu-latest
name: Build package
steps:
- name: Git checkout
uses: actions/checkout@v4
- name: Set up Python ${{ env.pythonLastVersion }}
uses: actions/setup-python@v5
with:
python-version: ${{ env.pythonLastVersion }}
cache: 'pip'
- name: Install Dependencies
run: pip install -r requirements-dev.txt
- name: Package
run: make dist
- name: Archive package
uses: actions/upload-artifact@v4
with:
name: seedboxsync-${{ github.sha }}.tar.gz
path: dist/*.tar.gz
##############################################################################
# Build and tests Docker image
#
test_docker:
needs: [
test_sonar
]
runs-on: ubuntu-latest
name: Build and test docker images
steps:
- name: Git checkout
uses: actions/checkout@v4
# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images:
${{ env.IMAGE_NAME }}
# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build Docker image
uses: docker/build-push-action@v6
with:
tags: |
${{ env.IMAGE_NAME }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=registry,ref=${{ env.IMAGE_NAME }}
cache-to: type=inline
# Test with Trivy
# https://github.com/aquasecurity/trivy-action
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
image-ref: ${{ env.IMAGE_NAME }}
format: 'template'
template: '@/contrib/sarif.tpl'
output: 'trivy-results.sarif'
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: 'trivy-results.sarif'
##############################################################################
# Build and deploy job (only on main)
#
docker_build_deploy:
if: ${{ github.event_name != 'pull_request' && ( github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v') ) }}
needs: [
test_docker
]
runs-on: ubuntu-latest
name: Build and deploy docker images
steps:
- name: Git checkout
uses: actions/checkout@v4
# Login against 2 Docker registries except on PR
# https://github.com/docker/login-action
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Log into registry ${{ env.GHCR_REGISTRY }}
uses: docker/login-action@v3
with:
registry: ${{ env.GHCR_REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: |
${{ env.IMAGE_NAME }}
${{ env.GHCR_REGISTRY }}/${{ env.IMAGE_NAME }}
# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
if: ${{ github.ref == 'refs/heads/main' }}
uses: docker/build-push-action@v6
with:
push: ${{ github.event_name != 'pull_request' }}
tags: |
${{ env.IMAGE_NAME }}:main
${{ env.GHCR_REGISTRY }}/${{ env.IMAGE_NAME }}:main
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=registry,ref=${{ env.IMAGE_NAME }}:main
cache-to: type=inline
- name: Set env
if: ${{ startsWith(github.ref, 'refs/tags/v') }}
run: echo "RELEASE_VERSION=${GITHUB_REF:11}" >> $GITHUB_ENV
- name: Build and push Docker image
if: ${{ startsWith(github.ref, 'refs/tags/v') }}
uses: docker/build-push-action@v6
with:
push: ${{ github.event_name != 'pull_request' }}
tags: |
${{ env.IMAGE_NAME }}:${{ env.RELEASE_VERSION }}
${{ env.IMAGE_NAME }}:latest
${{ env.GHCR_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.RELEASE_VERSION }}
${{ env.GHCR_REGISTRY }}/${{ env.IMAGE_NAME }}:latest
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=registry,ref=${{ env.IMAGE_NAME }}:latest
cache-to: type=inline
##############################################################################
# Release job
#
release:
if: ${{ startsWith(github.ref, 'refs/tags/v') }}
needs: [
package,
]
runs-on: ubuntu-latest
name: Release on GitHub and PyPi
steps:
- name: Git checkout
uses: actions/checkout@v4
- name: Set env
run: echo "RELEASE_VERSION=${GITHUB_REF:11}" >> $GITHUB_ENV
- name: Set up Python ${{ env.pythonLastVersion }}
uses: actions/setup-python@v5
with:
python-version: ${{ env.pythonLastVersion }}
cache: 'pip'
- name: Install Dependencies
run: pip install -r requirements-dev.txt
- name: Package
run: make dist
- name: Create GitHub release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
body: |
Changes in this Release
- First Change
- Second Change
draft: true
prerelease: false
- name: Upload asset in GitHub release
id: upload-release-asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps
asset_path: dist/seedboxsync-${{ env.RELEASE_VERSION }}.tar.gz
asset_name: seedboxsync-${{ env.RELEASE_VERSION }}.tar.gz
asset_content_type: application/tar+gzip
- name: Publish package
uses: pypa/gh-action-pypi-publish@master
with:
user: __token__
password: ${{ secrets.PYPI_PASSWORD }}