Skip to content

CompareScreenshot

CompareScreenshot #3

# ref: https://github.com/kosenda/hiragana-converter/blob/develop/REFERENCE.md
name: CompareScreenshot
on:
workflow_dispatch:
inputs:
pr_branch_name:
description: 'PR branch name'
type: string
required: true
pr_number:
description: 'PR number'
type: string
required: true
base_branch_name:
description: 'Base branch name'
type: string
required: true
permissions:
contents: write
actions: read
pull-requests: write
jobs:
compare-screnshots:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v4
- name: Check exist screenshots branch
id: check-exist-screenshots
run: |
echo is_exist=$(
git fetch origin screenshots_${{ inputs.base_branch_name }} &&
echo true || echo false
) >> "$GITHUB_OUTPUT"
- name: Checkout screenshots branch
if: steps.check-exist-screenshots.outputs.is_exist == 'true'
uses: actions/checkout@v4
with:
ref: screenshots_${{ inputs.base_branch_name }}
- name: Make screenshots directory
run: mkdir -p ./app/build/outputs/roborazzi
- name: Upload screenshots
if: steps.check-exist-screenshots.outputs.is_exist == 'true'
uses: actions/upload-artifact@v4
with:
name: screenshots
path: ./app/build/outputs/roborazzi/
retention-days: 1
- name: Checkout pr branch
if: steps.check-exist-screenshots.outputs.is_exist == 'true'
uses: actions/checkout@v4
with:
ref: ${{ inputs.pr_branch_name }}
- name: Setup
uses: ./.github/actions/setup
with:
api-key: ${{ secrets.API_KEY }}
- name: Download screenshots from PR number directory
continue-on-error: true
uses: actions/download-artifact@v4
with:
name: screenshots
path: ./app/build/outputs/roborazzi/
- name: Compare screenshots
run: ./gradlew compareRoborazziProdDebug --stacktrace
- name: Check screenshot diff
id: check-screenshot-diff
run: |
# To avoid "No such file or directory"
mkdir -p ./app/build/outputs/roborazzi
file_size=$(find ./app/build/outputs/roborazzi -type f | grep -e '.*_compare.png' | wc -l | sed -e 's/[^0-9]//g')
is_exist_screenshot_diff="false"
if [ $file_size -ne 0 ]; then
echo "file_size=$file_size"
is_exist_screenshot_diff="true"
fi
echo "is_exist=${is_exist_screenshot_diff}" >> "$GITHUB_OUTPUT"
- name: Make PR number directory for saving screenshots
if: steps.check-screenshot-diff.outputs.is_exist == 'true'
run: mkdir -p ./pr/${{ inputs.pr_number }}
- name: Move screenshots to PR number directory
if: steps.check-screenshot-diff.outputs.is_exist == 'true'
run: |
# delete except *_compare.png
find ./app/build/outputs/roborazzi -type f | grep -v -e '.*_compare.png' | xargs rm -rf
mv ./app/build/outputs/roborazzi ./pr/${{ inputs.pr_number }}/
ls ./pr/${{ inputs.pr_number }}
- name: Save screenshots
if: steps.check-screenshot-diff.outputs.is_exist == 'true'
uses: actions/upload-artifact@v4
with:
name: pr directory
path: pr/${{ inputs.pr_number }}/
- name: Save pr number and branch name
run: |
echo "COMPANION_BRANCH_NAME=companion_${{ inputs.pr_branch_name }}" >> $GITHUB_ENV
- name: Checkout main branch
if: steps.check-screenshot-diff.outputs.is_exist == 'true'
uses: actions/checkout@v4
with:
ref: main
- name: Checkout companion branch
if: steps.check-screenshot-diff.outputs.is_exist == 'true'
run: |
git branch -D ${{ env.COMPANION_BRANCH_NAME }} || true
git checkout --orphan ${{ env.COMPANION_BRANCH_NAME }}
git rm -rf .
- name: Download screenshots from PR number directory
if: steps.check-screenshot-diff.outputs.is_exist == 'true'
uses: actions/download-artifact@v4
with:
name: pr directory
- name: Push screenshots to companion branch
if: steps.check-screenshot-diff.outputs.is_exist == 'true'
id: push-screenshot-diff
run: |
files_to_add=$(find . -type f -name "*_compare.png")
for file in $files_to_add; do
if [[ $file =~ ^[a-zA-Z0-9_./-]+$ ]]; then
git add $file
fi
done
git config --global user.name ScreenshotBot
git config --global user.email 41898282+github-actions[bot]@users.noreply.github.com
git commit -m "Add screenshot diff"
git push origin HEAD:${{ env.COMPANION_BRANCH_NAME }} -f
- name: Create comments
if: steps.check-screenshot-diff.outputs.is_exist == 'true'
id: create-comments
run: |
files=$(find . -type f -name "*_compare.png")
delimiter="$(openssl rand -hex 8)"
{
echo "reports<<${delimiter}"
echo "Snapshot diff report"
echo "| File name | Image |"
echo "|-------|-------|"
} >> "$GITHUB_OUTPUT"
for file in $files; do
fileName=$(basename "$file" | sed -r 's/(.{20})/\1<br>/g')
echo "| [$fileName](https://github.com/${{ github.repository }}/blob/${{ env.COMPANION_BRANCH_NAME }}/$file) | ![](https://github.com/${{ github.repository }}/blob/${{ env.COMPANION_BRANCH_NAME }}/$file?raw=true) |" >> "$GITHUB_OUTPUT"
done
echo "${delimiter}" >> "$GITHUB_OUTPUT"
- name: Find comment
continue-on-error: true
uses: peter-evans/find-comment@v3
id: fc
with:
issue-number: ${{ inputs.pr_number }}
comment-author: 'github-actions[bot]'
body-includes: Snapshot diff report
- name: Add or update comment on PR (exist screenshot diff)
if: steps.check-screenshot-diff.outputs.is_exist == 'true'
uses: peter-evans/create-or-update-comment@v4
with:
comment-id: ${{ steps.fc.outputs.comment-id }}
issue-number: ${{ inputs.pr_number }}
body: ${{ steps.create-comments.outputs.reports }}
edit-mode: replace
- name: Add or update comment on PR (not exist screenshot diff)
if: steps.check-screenshot-diff.outputs.is_exist == 'false'
uses: peter-evans/create-or-update-comment@v4
with:
comment-id: ${{ steps.fc.outputs.comment-id }}
issue-number: ${{ inputs.pr_number }}
body: Snapshot diff report
edit-mode: replace
- name: Cleanup outdated companion or screenshots branches
run: |
git branch -r --format="%(refname:lstrip=3)" | grep -e companion_ -e screenshots_ | while read -r branch; do
last_commit_date_timestamp=$(git log -1 --format=%ct "origin/$branch")
now_timestamp=$(date +%s)
maximum_seconds_past=2592000 # 30 days
if [ $((now_timestamp - last_commit_date_timestamp)) -gt maximum_seconds_past ]; then
git push origin --delete "$branch"
fi
done
- name: Checkout pr branch
uses: actions/checkout@v4
with:
ref: ${{ inputs.pr_branch_name }}