-
Notifications
You must be signed in to change notification settings - Fork 1
258 lines (254 loc) · 8.82 KB
/
python.yaml
File metadata and controls
258 lines (254 loc) · 8.82 KB
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
# Copyright 2024 Zaphiro Technologies
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
name: Python Lint & Test
concurrency:
group: ${{github.repository}}-${{ github.ref_name }}-python
cancel-in-progress: true
# Permissions needed
# permissions:
# contents: read
# packages: write
# pull-requests: read
# id-token: write
on:
workflow_call:
inputs:
code-scan:
required: false
default: true
type: boolean
description: False to skip code-scan exec
vulnerability-scan:
required: false
default: true
type: boolean
description: False to skip vulnerability-scan exec
vulnerability-scan-skip-dirs:
required: false
default: "vendor,.github"
type: string
description: Comma separated list of directories where vulnerability check is skipped
pull-dvc-datasets:
required: false
default: false
type: boolean
description: True to install and configure dvc
upload-artefacts:
required: false
default: false
type: boolean
description: True to upload files build by test
upload-artefacts-dir:
required: false
default: "output"
type: string
description: Directory with the artifacts to upload
repositories:
required: false
type: string
default: ""
description: Comma or newline-separated list of repositories to grant access to during the golang build.
jobs:
changes:
runs-on: ubuntu-latest
outputs:
files_changed: ${{ steps.filter.outputs.python == 'true' }}
steps:
- uses: actions/checkout@v6
- uses: dorny/paths-filter@v4
id: filter
with:
filters: |
python:
- '*.py'
- '**/*.py'
- '.github/workflows/python.yaml'
- '.docker/*.yml'
- '.docker/*.yaml'
- 'poetry.lock'
- 'pyproject.toml'
lint:
# Check if the PR is not from a fork
runs-on: ubuntu-latest
needs: changes
if: ${{ needs.changes.outputs.files_changed == 'true' && github.event.pull_request && github.event.pull_request.head.repo.full_name == github.repository }}
steps:
- uses: actions/create-github-app-token@v3
id: generate-token
with:
app-id: ${{ secrets.APP_ID }}
private-key: ${{ secrets.APP_SECRET }}
repositories: ${{ github.event.repository.name }}${{ inputs.repositories && format(',{0}', inputs.repositories) || '' }}
permission-contents: write
- uses: actions/checkout@v6
with:
token: ${{ steps.generate-token.outputs.token }}
ref: ${{github.event.pull_request.head.ref}}
- name: Set up python
id: setup-python
uses: actions/setup-python@v6
with:
python-version-file: "pyproject.toml"
- name: Install poetry
run: pipx install poetry==2.3.2 || pipx upgrade poetry
env:
PIPX_DEFAULT_PYTHON: ${{ steps.setup-python.outputs.python-path }}
- name: Cache Poetry artifacts
uses: actions/cache@v5
with:
path: |
~/.cache/pypoetry/artifacts
~/.cache/pip
key: poetry-${{ runner.os }}-${{ hashFiles('poetry.lock') }}
restore-keys: |
poetry-${{ runner.os }}-
- name: Install packages
run: |
make lint-setup
- name: Lint
run: |
make lint
- name: Commit lint changes
run: |
git config --global user.name 'Bot'
git config --global user.email 'bot@zaphiro.ch'
git commit -am "Automated lint fixes [dependabot skip]" || echo "No changes to commit"
git push
test:
runs-on: ubuntu-latest
needs: changes
if: ${{ needs.changes.outputs.files_changed == 'true' }}
steps:
- uses: actions/create-github-app-token@v3
id: generate-token
with:
app-id: ${{ secrets.APP_ID }}
private-key: ${{ secrets.APP_SECRET }}
repositories: ${{ github.event.repository.name }}${{ inputs.repositories && format(',{0}', inputs.repositories) || '' }}
permission-contents: read
- name: Login to GitHub Container Registry
uses: docker/login-action@v4
# we log in the registry to code check also images
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: actions/checkout@v6
with:
token: ${{ steps.generate-token.outputs.token }}
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up python
id: setup-python
uses: actions/setup-python@v6
with:
python-version-file: "pyproject.toml"
- name: Install poetry
run: pipx install poetry==2.3.2 || pipx upgrade poetry
env:
PIPX_DEFAULT_PYTHON: ${{ steps.setup-python.outputs.python-path }}
- name: Cache Poetry artifacts
uses: actions/cache@v5
with:
path: |
~/.cache/pypoetry/artifacts
~/.cache/pip
key: poetry-${{ runner.os }}-${{ hashFiles('poetry.lock') }}
restore-keys: |
poetry-${{ runner.os }}-
- name: Install packages
run: |
make dev-setup
- name: Configure AWS Credentials
if: ${{ inputs.pull-dvc-datasets }}
id: creds
uses: aws-actions/configure-aws-credentials@v6
with:
aws-region: eu-south-1
role-to-assume: arn:aws:iam::058264335177:role/zaphiro-github-role-demo
output-credentials: true
- name: Install DVC
if: ${{ inputs.pull-dvc-datasets }}
run: |
pipx install 'dvc[s3]==3.67.0'
- name: Set-up credentials
if: ${{ inputs.pull-dvc-datasets }}
env:
GH_TOKEN: ${{ steps.generate-token.outputs.token }}
run: |
gh auth setup-git
git config --global credential.helper '!gh auth git-credential'
- name: Run tests
env:
GH_TOKEN: ${{ steps.generate-token.outputs.token }}
run: |
make ci-test
- if: ${{ inputs.upload-artefacts }}
name: Upload artefacts
uses: actions/upload-artifact@v7
with:
name: artefacts
path: ${{ inputs.upload-artefacts-dir }}
if-no-files-found: error
- name: SonarCloud Code Scan
if: ${{ inputs.code-scan }}
uses: SonarSource/sonarqube-scan-action@v8.0.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
vulnerability:
needs: changes
if: ${{ inputs.vulnerability-scan}}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- name: Run Trivy vulnerability scanner (for summary)
# trivy-action v0.35.0 (safe version)
uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25
with:
scan-type: fs
scan-ref: .
skip-dirs: ${{ inputs.vulnerability-scan-skip-dirs }}
scanners: "vuln,secret,misconfig"
hide-progress: true
output: trivy.txt
env:
TRIVY_SKIP_DB_UPDATE: true
TRIVY_SKIP_JAVA_DB_UPDATE: true
- name: Publish Trivy Output to Summary
run: |
if [[ -s trivy.txt ]]; then
{
echo "### Trivy Security Output"
echo ""
echo '```text'
cat trivy.txt
echo '```'
} >> $GITHUB_STEP_SUMMARY
fi
# this comes as last or scan results won't be uploaded
- name: Run Trivy vulnerability scanner (security treshold)
# trivy-action v0.35.0 (safe version)
uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25
with:
scan-type: fs
scan-ref: .
skip-dirs: ${{ inputs.vulnerability-scan-skip-dirs }}
scanners: "vuln,secret,misconfig"
exit-code: "1"
severity: MEDIUM,HIGH,CRITICAL
# On a subsequent call to the action we know trivy is already installed so can skip this
skip-setup-trivy: true
env:
TRIVY_SKIP_DB_UPDATE: true
TRIVY_SKIP_JAVA_DB_UPDATE: true