Skip to content

Commit 7ce05e5

Browse files
committed
Add workflows for screenshot tests
1 parent 8470bb9 commit 7ce05e5

File tree

3 files changed

+287
-0
lines changed

3 files changed

+287
-0
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
name: screenshot-comparison-comment
2+
3+
on:
4+
workflow_run:
5+
workflows:
6+
- screenshot-comparison
7+
types:
8+
- completed
9+
10+
concurrency:
11+
group: ${{ github.workflow }}-${{ github.head_ref }}-${{ github.event.workflow_run.id }}
12+
cancel-in-progress: true
13+
14+
jobs:
15+
screenshot-comparison-comment:
16+
if: >
17+
github.event.workflow_run.event == 'pull_request' &&
18+
github.event.workflow_run.conclusion == 'success'
19+
20+
timeout-minutes: 2
21+
22+
permissions:
23+
actions: read # for downloading artifacts
24+
contents: write # for pushing screenshot-diff to companion branch
25+
pull-requests: write # for creating a comment on pull requests
26+
27+
runs-on: ubuntu-latest
28+
29+
steps:
30+
- uses: dawidd6/action-download-artifact@v11
31+
with:
32+
name: pr
33+
run_id: ${{ github.event.workflow_run.id }}
34+
- id: get-pull-request-number
35+
name: Get pull request number
36+
shell: bash
37+
run: |
38+
echo "pull_request_number=$(cat NR)" >> "$GITHUB_OUTPUT"
39+
- name: main checkout
40+
id: checkout-main
41+
uses: actions/checkout@v4
42+
with:
43+
ref: main
44+
- id: switch-companion-branch
45+
env:
46+
BRANCH_NAME: companion_${{ github.event.workflow_run.head_branch }}
47+
run: |
48+
# orphan means it will create no history branch
49+
git branch -D "$BRANCH_NAME" || true
50+
git checkout --orphan "$BRANCH_NAME"
51+
git rm -rf .
52+
- uses: dawidd6/action-download-artifact@v11
53+
with:
54+
run_id: ${{ github.event.workflow_run.id }}
55+
name: screenshot-diff-reports
56+
path: screenshot-diff-reports
57+
- id: check-if-there-are-valid-files
58+
name: Check if there are valid files
59+
shell: bash
60+
run: |
61+
# Find all the files ending with _compare.png
62+
mapfile -d '' -t files_to_add < <(find . -type f -name "*_compare.png" -print0)
63+
64+
# Check for invalid file names and add only valid ones
65+
exist_valid_files="false"
66+
for file in "${files_to_add[@]}"; do
67+
if [[ $file =~ ^([-0-9A-Z_a-z.\ \/]|\[|\])+$ ]]; then
68+
exist_valid_files="true"
69+
break
70+
fi
71+
done
72+
echo "exist_valid_files=$exist_valid_files" >> "$GITHUB_OUTPUT"
73+
- id: push-screenshot-diff
74+
shell: bash
75+
if: steps.check-if-there-are-valid-files.outputs.exist_valid_files == 'true'
76+
env:
77+
BRANCH_NAME: companion_${{ github.event.workflow_run.head_branch }}
78+
run: |
79+
# Find all the files ending with _compare.png
80+
mapfile -d '' -t files_to_add < <(find . -type f -name "*_compare.png" -print0)
81+
82+
# Check for invalid file names and add only valid ones
83+
for file in "${files_to_add[@]}"; do
84+
if [[ "$file" =~ ^([-0-9A-Z_a-z.\ \/]|\[|\])+$ ]]; then
85+
git add "$file"
86+
fi
87+
done
88+
git config --global user.name ScreenshotBot
89+
git config --global user.email 41898282+github-actions[bot]@users.noreply.github.com
90+
git commit -m "Add screenshot diff"
91+
git push origin HEAD:"$BRANCH_NAME" -f
92+
- id: generate-diff-reports
93+
name: Generate diff reports
94+
if: steps.check-if-there-are-valid-files.outputs.exist_valid_files == 'true'
95+
env:
96+
BRANCH_NAME: companion_${{ github.event.workflow_run.head_branch }}
97+
shell: bash
98+
run: |
99+
# Find all the files ending with _compare.png in roborazzi folder
100+
mapfile -d '' -t files < <(find . -type f -name "*_compare.png" -print0)
101+
delimiter="$(openssl rand -hex 8)"
102+
{
103+
echo "reports<<${delimiter}"
104+
105+
# Create markdown table header
106+
echo "Snapshot diff report"
107+
echo "| File name | Image |"
108+
echo "|-------|-------|"
109+
} >> "$GITHUB_OUTPUT"
110+
111+
# Iterate over the files and create table rows
112+
for file in "${files[@]}"; do
113+
# Get the file name and insert newlines every 20 characters
114+
fileName=$(basename "$file" | sed -r 's/(.{20})/\1<br>/g')
115+
urlPart="${BRANCH_NAME//#/%23}/${file//#/%23}"
116+
urlPart="${urlPart//[/%5B}"
117+
urlPart="${urlPart//]/%5D}"
118+
urlPart="${urlPart// /%20}"
119+
echo "| [$fileName](https://github.com/${{ github.repository }}/blob/$urlPart) | ![](https://github.com/${{ github.repository }}/blob/$urlPart?raw=true) |" >> "$GITHUB_OUTPUT"
120+
done
121+
echo "${delimiter}" >> "$GITHUB_OUTPUT"
122+
- name: Find Comment
123+
uses: peter-evans/find-comment@3eae4d37986fb5a8592848f6a574fdf654e61f9e # v3.1.0
124+
id: fc
125+
if: steps.generate-diff-reports.outputs.reports != ''
126+
with:
127+
issue-number: ${{ steps.get-pull-request-number.outputs.pull_request_number }}
128+
comment-author: 'github-actions[bot]'
129+
body-includes: Snapshot diff report
130+
131+
- name: Add or update comment on PR
132+
uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4.0.0
133+
if: steps.generate-diff-reports.outputs.reports != ''
134+
with:
135+
comment-id: ${{ steps.fc.outputs.comment-id }}
136+
issue-number: ${{ steps.get-pull-request-number.outputs.pull_request_number }}
137+
body: ${{ steps.generate-diff-reports.outputs.reports }}
138+
edit-mode: replace
139+
140+
- name: Cleanup outdated companion branches
141+
run: |
142+
# Find outdated companion branches with last commit date
143+
git branch -r --format="%(refname:lstrip=3)" | grep companion_ | while read -r branch; do
144+
last_commit_date_timestamp=$(git log -1 --format=%ct "origin/$branch")
145+
now_timestamp=$(date +%s)
146+
# Delete branch if it's older than 1 month
147+
# if [ $((now_timestamp - last_commit_date_timestamp)) -gt 2592000 ]; then
148+
# For testing purpose, delete branch if it's older than 1 second
149+
echo "branch: $branch now_timestamp: $now_timestamp last_commit_date_timestamp: $last_commit_date_timestamp"
150+
if [ $((now_timestamp - last_commit_date_timestamp)) -gt 1 ]; then
151+
# Comment out for demonstration purpose
152+
echo "Deleting $branch"
153+
154+
# git push origin --delete "$branch"
155+
fi
156+
done
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
name: screenshot-comparison
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
paths-ignore:
8+
- '**.md'
9+
- 'docs/**'
10+
pull_request:
11+
paths-ignore:
12+
- '**.md'
13+
- 'docs/**'
14+
15+
concurrency:
16+
group: ${{ github.workflow }}-${{ github.head_ref }}
17+
cancel-in-progress: true
18+
19+
jobs:
20+
screenshot-comparison:
21+
runs-on: ubuntu-latest
22+
timeout-minutes: 30
23+
24+
permissions:
25+
contents: read # for clone
26+
actions: write # for upload-artifact
27+
checks: read # for wait-on-check-action
28+
29+
steps:
30+
- name: Checkout for getting base branch HEAD commit
31+
uses: actions/checkout@v4
32+
with:
33+
ref: ${{ github.base_ref }}
34+
35+
- name: Get base branch HEAD commit for comparison
36+
id: get_base_branch_head
37+
run: echo "sha=$(git rev-parse origin/${{ github.base_ref }})" >> "$GITHUB_OUTPUT"
38+
39+
- name: Checkout merged commit with base and pull-request branch
40+
uses: actions/checkout@v4
41+
42+
- name: Copy CI gradle.properties
43+
run: mkdir -p ~/.gradle ; cp .github/ci-gradle.properties ~/.gradle/gradle.properties
44+
45+
- name: Setup java
46+
uses: actions/setup-java@v4
47+
with:
48+
distribution: 'zulu'
49+
java-version: 17
50+
51+
- name: Setup Gradle
52+
uses: gradle/actions/setup-gradle@v4
53+
54+
- name: Download screenshot artifact from base branch for comparison
55+
uses: dawidd6/action-download-artifact@v11
56+
continue-on-error: true
57+
with:
58+
name: test-reports
59+
workflow: unit-test.yml
60+
commit: ${{ steps.get_base_branch_head.outputs.sha }}
61+
if_no_artifact_found: fail
62+
63+
- run: ./gradlew compareRoborazziDebug --stacktrace
64+
65+
- uses: actions/upload-artifact@v4
66+
if: always()
67+
with:
68+
name: screenshot-diff-reports
69+
path: |
70+
**/reports
71+
**/build/outputs/roborazzi
72+
retention-days: 30
73+
74+
- name: Save PR number
75+
if: ${{ github.event_name == 'pull_request' }}
76+
run: |
77+
mkdir -p ./pr
78+
echo ${{ github.event.number }} > ./pr/NR
79+
- uses: actions/upload-artifact@v4
80+
with:
81+
name: pr
82+
path: pr/

.github/workflows/unit-test.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: Unit test
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
paths-ignore:
8+
- '**.md'
9+
- 'docs/**'
10+
pull_request:
11+
paths-ignore:
12+
- '**.md'
13+
- 'docs/**'
14+
15+
jobs:
16+
unit-test:
17+
needs: build
18+
runs-on: ubuntu-latest
19+
timeout-minutes: 30
20+
21+
permissions:
22+
contents: read # for clone
23+
actions: write # for upload-artifact
24+
25+
steps:
26+
- uses: actions/checkout@v4
27+
28+
- name: Copy CI gradle.properties
29+
run: mkdir -p ~/.gradle ; cp .github/ci-gradle.properties ~/.gradle/gradle.properties
30+
31+
- name: Setup java
32+
uses: actions/setup-java@v4
33+
with:
34+
distribution: 'zulu'
35+
java-version: 17
36+
37+
- name: Setup Gradle
38+
uses: gradle/actions/setup-gradle@v4
39+
40+
- name: Build tests & Create screenshots
41+
run: ./gradlew testDebugUnitTest --stacktrace
42+
43+
- uses: actions/upload-artifact@v4
44+
if: always()
45+
with:
46+
name: test-reports
47+
path: |
48+
**/reports
49+
**/build/outputs/roborazzi

0 commit comments

Comments
 (0)