-
Notifications
You must be signed in to change notification settings - Fork 0
302 lines (273 loc) · 9.84 KB
/
Copy pathreusable-python-ci.yml
File metadata and controls
302 lines (273 loc) · 9.84 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
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
name: Reusable Python CI
on:
workflow_call:
inputs:
project_name:
required: true
type: string
working_directory:
required: false
type: string
default: "."
lint_path:
required: false
type: string
default: "src"
apt_deps:
required: false
type: string
default: ""
python_versions:
required: false
type: string
default: '["3.9", "3.10", "3.11"]'
run_pytest:
required: false
type: boolean
default: true
local_dependency_paths:
required: false
type: string
default: "[]"
secrets:
SLACK_BOT_TOKEN:
required: false
SLACK_CHANNEL_ID:
required: false
DISCORD_WEBHOOK_URL_CI:
required: false
permissions:
contents: read
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ inputs.project_name }}
cancel-in-progress: true
jobs:
setup:
runs-on: ubuntu-latest
outputs:
start_unix: ${{ steps.start.outputs.start_unix }}
steps:
- name: Mark start time
id: start
run: echo "start_unix=$(date +%s)" >> "$GITHUB_OUTPUT"
lint-and-test:
needs: [setup]
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ${{ fromJson(inputs.python_versions) }}
defaults:
run:
working-directory: ${{ inputs.working_directory }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install system dependencies (apt)
if: ${{ inputs.apt_deps != '' }}
run: |
sudo apt-get update
sudo apt-get install -y ${{ inputs.apt_deps }}
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install local sibling dependencies
env:
LOCAL_DEPENDENCY_PATHS: ${{ inputs.local_dependency_paths }}
run: |
python -m pip install --upgrade pip
python - << 'PY'
import json
import os
import subprocess
import sys
dep_paths = json.loads(os.environ["LOCAL_DEPENDENCY_PATHS"])
for dep_path in dep_paths:
resolved_path = os.path.abspath(
os.path.expandvars(os.path.expanduser(dep_path))
)
if not os.path.exists(resolved_path):
raise FileNotFoundError(
f"Local dependency path does not exist: {dep_path} -> "
f"{resolved_path}"
)
print(f"Installing local dependency: {resolved_path}")
subprocess.run(
[sys.executable, "-m", "pip", "install", "-e", resolved_path],
check=True,
)
PY
- name: Install current package
run: |
python -m pip install --upgrade pip
pip install -e .[dev]
- name: Run isort
run: isort --check-only .
- name: Run black
run: black --check .
- name: Run pylint
run: pylint ${{ inputs.lint_path }}
- name: Run pytest
if: ${{ inputs.run_pytest }}
run: pytest
notify:
needs: [setup, lint-and-test]
runs-on: ubuntu-latest
if: always()
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
SLACK_CHANNEL_ID: ${{ secrets.SLACK_CHANNEL_ID }}
DISCORD_WEBHOOK_URL_CI: ${{ secrets.DISCORD_WEBHOOK_URL_CI }}
steps:
- name: Calculate pipeline duration
id: duration
run: |
end=$(date +%s)
seconds=$((end - ${{ needs.setup.outputs.start_unix }}))
if [ "$seconds" -lt 60 ]; then
formatted="${seconds}s"
elif [ "$seconds" -lt 3600 ]; then
minutes=$((seconds / 60)); secs=$((seconds % 60))
formatted="${minutes}m"; [ "$secs" -ne 0 ] && formatted="${formatted} ${secs}s"
else
hours=$((seconds / 3600)); minutes=$(((seconds % 3600) / 60))
formatted="${hours}h"; [ "$minutes" -ne 0 ] && formatted="${formatted} ${minutes}m"
fi
echo "time=${formatted}" >> "$GITHUB_OUTPUT"
- name: Set pipeline status icon
id: icon
run: |
if [[ "${{ needs.lint-and-test.result }}" != "success" ]]; then
echo "icon=x" >> "$GITHUB_OUTPUT"
else
echo "icon=white_check_mark" >> "$GITHUB_OUTPUT"
fi
- name: Set pipeline status word
id: word
run: |
if [[ "${{ needs.lint-and-test.result }}" != "success" ]]; then
echo "word=failed" >> "$GITHUB_OUTPUT"
else
echo "word=passed" >> "$GITHUB_OUTPUT"
fi
- name: Get GitHub user full name
id: ghname
run: |
name=$(curl -s "https://api.github.com/users/${{ github.actor }}" | jq -r '.name // empty')
echo "name=${name}" >> "$GITHUB_OUTPUT"
- name: Build payload JSON
id: payload
env:
PROJECT: ${{ inputs.project_name }}
SERVER_URL: ${{ github.server_url }}
REPO: ${{ github.repository }}
RUN_ID: ${{ github.run_id }}
ACTOR: ${{ github.actor }}
BRANCH: ${{ github.head_ref || github.ref_name }}
COMMIT: ${{ github.sha }}
COMMIT_MSG: ${{ github.event.head_commit.message || github.event.pull_request.title || 'Commit message not available' }}
START_UNIX: ${{ needs.setup.outputs.start_unix }}
DURATION: ${{ steps.duration.outputs.time }}
ICON: ${{ steps.icon.outputs.icon }}
WORD: ${{ steps.word.outputs.word }}
NAME: ${{ steps.ghname.outputs.name }}
run: |
blocks=$(jq -cn \
--arg icon "$ICON" \
--arg name "$NAME" \
--arg server "$SERVER_URL" \
--arg repo "$REPO" \
--arg runid "$RUN_ID" \
--arg actor "$ACTOR" \
--arg branch "$BRANCH" \
--arg commit "$COMMIT" \
--arg cmsg "$COMMIT_MSG" \
--arg dur "$DURATION" \
--arg start "$START_UNIX" \
--arg word "$WORD" \
--arg project "$PROJECT" '
[
{
"type":"section",
"text":{
"type":"mrkdwn",
"text": (":\($icon): *CI \($project) by \((if ($name|length)>0 then $name else $actor end))* (<\($server)/\($actor)|@\($actor)>)\n" +
"Pipeline <\($server)/\($repo)/actions/runs/\($runid)|#\($runid)> has *\($word)* in `\($dur)`.")
}
},
{
"type":"section",
"fields":[
{ "type":"mrkdwn", "text":"*Branch:*\n<\($server)/\($repo)/tree/\($branch)|`\($branch)`>" },
{ "type":"mrkdwn", "text":"*Commit:*\n<\($server)/\($repo)/commit/\($commit)|`\($commit)`> - `\($cmsg)`" }
]
},
{
"type":"context",
"elements":[
{ "type":"mrkdwn", "text":"<\($server)/\($repo)|\($repo)> · <!date^\($start)^{date_short_pretty} at {time}|Run time unavailable>" }
]
}
]')
payload=$(jq -cn \
--arg icon "$ICON" \
--arg server "$SERVER_URL" \
--arg repo "$REPO" \
--arg runid "$RUN_ID" \
--arg word "$WORD" \
--arg dur "$DURATION" \
--arg project "$PROJECT" \
--argjson blocks "$blocks" '
{
"text": (":\($icon): CI \($project) <\($server)/\($repo)/actions/runs/\($runid)|#\($runid)> has *\($word)* in `\($dur)`."),
"blocks": $blocks
}')
echo "payload=$(echo "$payload" | jq -c .)" >> "$GITHUB_OUTPUT"
- name: Build Discord payload JSON
id: discord_payload
env:
PROJECT: ${{ inputs.project_name }}
SERVER_URL: ${{ github.server_url }}
REPO: ${{ github.repository }}
RUN_ID: ${{ github.run_id }}
BRANCH: ${{ github.head_ref || github.ref_name }}
COMMIT: ${{ github.sha }}
WORD: ${{ steps.word.outputs.word }}
DURATION: ${{ steps.duration.outputs.time }}
run: |
payload=$(jq -cn \
--arg project "$PROJECT" \
--arg server "$SERVER_URL" \
--arg repo "$REPO" \
--arg runid "$RUN_ID" \
--arg branch "$BRANCH" \
--arg commit "$COMMIT" \
--arg word "$WORD" \
--arg dur "$DURATION" '
{
"content": ("CI `" + $project + "` " + $word + " in `" + $dur + "`\n" +
"Run: " + $server + "/" + $repo + "/actions/runs/" + $runid + "\n" +
"Branch: `" + $branch + "`\n" +
"Commit: `" + $commit + "`")
}')
echo "payload=$(echo "$payload" | jq -c .)" >> "$GITHUB_OUTPUT"
- name: Slack Notification
if: env.SLACK_BOT_TOKEN != '' && env.SLACK_CHANNEL_ID != ''
uses: slackapi/slack-github-action@v1.24.0
with:
channel-id: ${{ env.SLACK_CHANNEL_ID }}
payload: ${{ steps.payload.outputs.payload }}
env:
SLACK_BOT_TOKEN: ${{ env.SLACK_BOT_TOKEN }}
- name: Discord Notification
if: env.DISCORD_WEBHOOK_URL_CI != ''
env:
DISCORD_PAYLOAD: ${{ steps.discord_payload.outputs.payload }}
run: |
curl -sS -X POST \
-H "Content-Type: application/json" \
-d "$DISCORD_PAYLOAD" \
"$DISCORD_WEBHOOK_URL_CI"