-
Notifications
You must be signed in to change notification settings - Fork 0
340 lines (301 loc) · 10.7 KB
/
test-tag.yml
File metadata and controls
340 lines (301 loc) · 10.7 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
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
name: Python Tests CI
on:
push:
branches: [master]
pull_request:
branches: [master]
types: [opened, synchronize]
jobs:
test:
name: Runs Tests
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Install dependencies and test tools
run: |
python -m pip install --upgrade pip
pip install -e .
pip install -r requirements-tests.txt
- name: Run tests with coverage
run: pytest --cov=wallhavenapi --cov-report=term-missing --cov-report=xml --cov-report=html tests/
- name: Upload coverage reports artifact
uses: actions/upload-artifact@v4
with:
name: coverage-reports
path: |
coverage.xml
htmlcov/
deploy-coverage:
name: Upload Coverage Report to Codecov and Github Pages
needs: test
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Download coverage report
uses: actions/download-artifact@v4
with:
name: coverage-reports
path: coverage/
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: coverage/coverage.xml
fail_ci_if_error: true
- name: Set deploy folder and info
id: meta
run: |
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
FOLDER="pr-${{ github.event.pull_request.number }}"
else
FOLDER="latest"
fi
echo "folder=$FOLDER" >> $GITHUB_OUTPUT
echo "timestamp=$(date -u '+%Y-%m-%d %H:%M:%S UTC')" >> $GITHUB_OUTPUT
echo "short_sha=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
echo "commit_msg=$(git log -1 --pretty=%s)" >> $GITHUB_OUTPUT
echo "commit_url=https://github.com/${{ github.repository }}/commit/$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
- name: Clone gh-pages branch
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git clone --depth 1 --branch gh-pages https://github.com/${{ github.repository }} gh-pages
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Copy coverage report
run: |
mkdir -p gh-pages/coverage/${{ steps.meta.outputs.folder }}
cp -r coverage/htmlcov/* gh-pages/coverage/${{ steps.meta.outputs.folder }}/
- name: Clean up closed PRs
run: |
cd gh-pages/coverage
for prdir in pr-*; do
prnum=$(echo "$prdir" | sed 's/pr-//')
STATUS=$(gh pr view $prnum --json state -q ".state" || echo "unknown")
if [[ "$STATUS" != "OPEN" ]]; then
echo "Removing $prdir (state: $STATUS)"
rm -rf "$prdir"
fi
done
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Generate index.html
run: |
cd gh-pages/coverage
cat <<EOF > index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Coverage Reports</title>
<style>
:root {
color-scheme: light dark;
}
body {
font-family: system-ui, sans-serif;
margin: 2rem;
background-color: var(--bg, #f9f9f9);
color: var(--text, #111);
transition: background 0.3s, color 0.3s;
}
body.dark {
--bg: #111;
--text: #eee;
}
h1 {
font-size: 2rem;
border-bottom: 2px solid #ccc;
}
h2 {
margin-top: 2rem;
font-size: 1.5rem;
}
ul {
list-style: none;
padding-left: 0;
}
li {
margin: 0.4rem 0;
}
a {
color: #0074d9;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
.meta {
font-size: 0.9rem;
color: #666;
}
.badge {
display: inline-block;
font-size: 0.75rem;
font-weight: bold;
color: white;
padding: 0.15em 0.5em;
border-radius: 0.4em;
margin-left: 0.5em;
}
.badge-green { background: #28a745; }
.badge-blue { background: #0366d6; }
.toggle-btn {
position: absolute;
top: 1rem;
right: 1rem;
font-size: 1rem;
cursor: pointer;
}
</style>
<script>
function toggleTheme() {
const body = document.body;
body.classList.toggle('dark');
const theme = body.classList.contains('dark') ? 'dark' : 'light';
localStorage.setItem('theme', theme);
}
window.addEventListener('DOMContentLoaded', () => {
const saved = localStorage.getItem('theme');
if (saved === 'dark') {
document.body.classList.add('dark');
}
});
</script>
</head>
<body>
<button class="toggle-btn" onclick="toggleTheme()">🌙 / ☀️</button>
<h1>Coverage Reports</h1>
<div class="meta">
<p>Last updated: ${{ steps.meta.outputs.timestamp }}</p>
<p>Commit: <a href="${{ steps.meta.outputs.commit_url }}">${{ steps.meta.outputs.short_sha }}</a> – ${{ steps.meta.outputs.commit_msg }}</p>
</div>
<h2>Latest (main)</h2>
<ul>
<li>
<a href="latest/index.html">View coverage report</a>
<span class="badge badge-green">Latest</span>
</li>
</ul>
<h2>Open Pull Requests</h2>
<ul>
EOF
for prdir in pr-*; do
if [ -f "$prdir/index.html" ]; then
echo "<li><a href=\"$prdir/index.html\">$prdir</a><span class=\"badge badge-blue\">Open PR</span></li>" >> index.html
fi
done
echo "</ul></body></html>" >> index.html
- name: Push to GitHub Pages
run: |
cd gh-pages
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
git add .
git commit -m "Deploy coverage: ${{ steps.meta.outputs.folder }}"
git push origin gh-pages
tag-semver:
name: Tag with Semantic Version
needs: test # Only runs if the 'test' job succeeds
runs-on: ubuntu-latest
if: github.event_name == 'push'
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Required to access all tags
- name: Get latest semantic tag
id: get_tag
run: |
TAG=$(git tag --sort=-v:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | head -n 1 || echo "v0.0.0")
echo "tag=$TAG" >> "$GITHUB_OUTPUT"
- name: Determine bump type from commit message
id: bump_type
run: |
COMMIT_MSG=$(git log -1 --pretty=%B)
if echo "$COMMIT_MSG" | grep -iq "#major"; then
echo "bump=major" >> "$GITHUB_OUTPUT"
elif echo "$COMMIT_MSG" | grep -iq "#minor"; then
echo "bump=minor" >> "$GITHUB_OUTPUT"
else
echo "bump=patch" >> "$GITHUB_OUTPUT"
fi
- name: Bump version
id: bump_version
run: |
OLD_TAG=${{ steps.get_tag.outputs.tag }}
VERSION=${OLD_TAG#v}
IFS='.' read -r MAJOR MINOR PATCH <<< "$VERSION"
case "${{ steps.bump_type.outputs.bump }}" in
major) MAJOR=$((MAJOR + 1)); MINOR=0; PATCH=0;;
minor) MINOR=$((MINOR + 1)); PATCH=0;;
patch) PATCH=$((PATCH + 1));;
esac
NEW_TAG="v$MAJOR.$MINOR.$PATCH"
echo "new_tag=$NEW_TAG" >> "$GITHUB_OUTPUT"
- name: Create and push new tag
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git tag ${{ steps.bump_version.outputs.new_tag }}
git push origin ${{ steps.bump_version.outputs.new_tag }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
tag-open-pr:
name: Tag Open Pull Request (Non-Semantic)
needs: test
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
steps:
- name: Checkout repo
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Tag merged pull request
run: |
PR_NUNBER=${{ github.event.pull_request.number }}
SHORT_SHA=$(git rev-parse --short HEAD)
TAG="pr-${PR_NUMBER}-${SHORT_SHA}"
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git tag "$TAG"
git push origin "$TAG"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
cleanup:
name: Delete Merged PR Tags
needs: test
runs-on: ubuntu-latest
if: github.event_name == 'push'
steps:
- name: Checkout repo
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Delete merged PR tags
run: |
git fetch --tags
git branch --contains HEAD # debug
for tag in $(git tag --list "pr-*"); do
# Get the commit hash the tag points to
tag_commit=$(git rev-list -n 1 "$tag")
# Check if commit is part of main branch history
if git merge-base --is-ancestor "$tag_commit" origin/main; then
echo "Deleting tag $tag (merged into main)"
git tag -d "$tag"
git push origin ":refs/tags/$tag"
else
echo "Keeping tag $tag (not yet merged into main)"
fi
done
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}