Skip to content

Commit 5653a1e

Browse files
committed
Add image diffs against previous build to CI results
1 parent c1a4279 commit 5653a1e

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

Diff for: .github/workflows/main.yaml

+12
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ jobs:
6262
cheatsheets.pdf
6363
handout-*.pdf
6464
./docs/_build/html/
65+
- uses: actions/upload-artifact@v4
66+
id: diffs-artifact-upload
67+
if: ${{ always() }}
68+
with:
69+
name: diffs
70+
path: |
71+
diffs/
72+
- name: Output artifacts URL
73+
run: |
74+
echo 'Artifact URL:' \
75+
'${{ steps.diffs-artifact-upload.outputs.artifact-url }}' \
76+
>> $GITHUB_STEP_SUMMARY
6577
- name: Publish cheatsheets and handouts
6678
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}
6779
uses: peaceiris/actions-gh-pages@v3

Diff for: Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ check:
5151
./check-num-pages.sh handout-beginner.pdf 1
5252
./check-num-pages.sh handout-intermediate.pdf 1
5353
./check-links.py cheatsheets.pdf
54+
./check-diffs.py
5455

5556
.PHONY: docs
5657
docs:

Diff for: check-diffs.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python
2+
3+
import os
4+
import subprocess
5+
import sys
6+
from pathlib import Path
7+
8+
9+
ROOT_DIR = Path(__file__).parent
10+
11+
if os.environ.get('GITHUB_ACTIONS', '') == '':
12+
print('Not running when not in GitHub Actions.')
13+
sys.exit()
14+
summary_file = os.environ.get('GITHUB_STEP_SUMMARY')
15+
if summary_file is None:
16+
sys.exit('$GITHUB_STEP_SUMMARY is not set')
17+
18+
gh_pages = ROOT_DIR.parent / 'pages'
19+
subprocess.run(['git', 'fetch', 'https://github.com/matplotlib/cheatsheets.git',
20+
'gh-pages:upstream-gh-pages'], check=True)
21+
subprocess.run(['git', 'worktree', 'add', gh_pages, 'upstream-gh-pages'],
22+
check=True)
23+
24+
diff_dir = ROOT_DIR / 'diffs'
25+
diff_dir.mkdir(exist_ok=True)
26+
27+
hashes = {}
28+
for original in gh_pages.glob('*.png'):
29+
result = subprocess.run(
30+
['compare', '-metric', 'PHASH',
31+
original,
32+
ROOT_DIR / 'docs/_build/html' / original.name,
33+
diff_dir / f'{original.stem}-diff.png'],
34+
text=True, stderr=subprocess.PIPE)
35+
if result.returncode == 2: # Some kind of IO or similar error.
36+
hashes[original] = (float('nan'), result.stderr)
37+
elif result.stderr: # Images were different.
38+
hashes[original] = (float(result.stderr), '')
39+
else: # No differences.
40+
hashes[original] = (0.0, '')
41+
42+
with open(summary_file, 'w+') as summary:
43+
print('# Cheatsheet image comparison', file=summary)
44+
print('| Filename | Perceptual Hash Difference | Error message |', file=summary)
45+
print('| -------- | -------------------------- | ------------- |', file=summary)
46+
for filename, (hash, message) in hashes.items():
47+
message = message.replace('\n', ' ').replace('|', '\\|')
48+
print(f'| {filename.name} | {hash:.05f} | {message}', file=summary)
49+
print(file=summary)
50+
51+
subprocess.run(['git', 'worktree', 'remove', gh_pages])

0 commit comments

Comments
 (0)