|
| 1 | +name: Snapshot Test iOS |
| 2 | +description: A GitHub Actions workflow for running snapshot tests on iOS. |
| 3 | + |
| 4 | +inputs: |
| 5 | + xcworkspacePath: |
| 6 | + description: 'Path to the Xcode workspace file' |
| 7 | + required: true |
| 8 | + |
| 9 | + xcodeSnapshotTestScheme: |
| 10 | + description: 'Scheme to run snapshot tests on' |
| 11 | + required: true |
| 12 | + |
| 13 | +runs: |
| 14 | + using: composite |
| 15 | + |
| 16 | + steps: |
| 17 | + - name: Run snapshot tests on iOS |
| 18 | + id: run-ios-snapshot-tests |
| 19 | + shell: bash |
| 20 | + run: | |
| 21 | + xcodebuild -workspace "${{ inputs.xcworkspacePath }}" \ |
| 22 | + -scheme ${{ inputs.xcodeSnapshotTestScheme }} \ |
| 23 | + -sdk iphonesimulator \ |
| 24 | + -destination 'platform=iOS Simulator,name=iPhone 14,OS=16.4' \ |
| 25 | + test |
| 26 | +
|
| 27 | + - name: Process failed iOS snapshot test |
| 28 | + if: failure() |
| 29 | + id: failed-ios-screenshots |
| 30 | + shell: bash |
| 31 | + run: | |
| 32 | + if [[ -z ${GITHUB_TOKEN} ]]; then |
| 33 | + echo "Missing GITHUB_TOKEN variable" |
| 34 | + exit 1 |
| 35 | + fi |
| 36 | +
|
| 37 | + if [[ -z ${GITHUB_REPOSITORY} ]]; then |
| 38 | + echo "Missing GITHUB_REPOSITORY variable" |
| 39 | + exit 1 |
| 40 | + fi |
| 41 | +
|
| 42 | + if [[ -z ${PR_BRANCH} ]]; then |
| 43 | + echo "Missing PR_BRANCH variable" |
| 44 | + exit 1 |
| 45 | + fi |
| 46 | +
|
| 47 | + FAILED_SNAPSHOTS="$(find . -name "__FailedSnapshots__" -type d)" |
| 48 | + SNAPSHOTS="$(find . -name "__Snapshots__" -type d)" |
| 49 | + PR_NUMBER="${GITHUB_REF#refs/pull/}" |
| 50 | + PR_NUMBER="${PR_NUMBER/\/merge/}" |
| 51 | + NEW_BRANCH_NAME="snapshots/pr-${PR_NUMBER}" |
| 52 | + |
| 53 | + cp -a ${FAILED_SNAPSHOTS}/* ${SNAPSHOTS} |
| 54 | + rm -rf ${FAILED_SNAPSHOTS} |
| 55 | + echo "PR_NUMBER=${PR_NUMBER}" >> "${GITHUB_OUTPUT}" |
| 56 | +
|
| 57 | + git config user.name "${GITHUB_ACTOR}" |
| 58 | + git config user.email "${GITHUB_ACTOR}@users.noreply.github.com" |
| 59 | + git fetch origin "${PR_BRANCH}" |
| 60 | + git checkout --track "origin/${PR_BRANCH}" |
| 61 | + git checkout -b "${NEW_BRANCH_NAME}" |
| 62 | + git add ${SNAPSHOTS} |
| 63 | + git commit -m "test(snapshots): Update screenshots" |
| 64 | + git push --force-with-lease --set-upstream origin "${NEW_BRANCH_NAME}":"${NEW_BRANCH_NAME}" |
| 65 | + echo "PR_COMMENT=\"Screenshot tests failed.\n\n[See differences](https://github.com/${GITHUB_REPOSITORY}/compare/${PR_BRANCH}...${NEW_BRANCH_NAME})\n\nMerge the branch if it's an intentional change.\"" >> "${GITHUB_OUTPUT}" |
| 66 | +
|
| 67 | + unset FAILED_SNAPSHOTS |
| 68 | + unset SNAPSHOTS |
| 69 | + unset PR_NUMBER |
| 70 | + unset NEW_BRANCH_NAME |
| 71 | + env: |
| 72 | + GITHUB_TOKEN: ${{ github.token }} |
| 73 | + GITHUB_REPOSITORY: ${{ github.repository }} |
| 74 | + PR_BRANCH: ${{ github.head_ref }} |
| 75 | + GITHUB_ACTOR: ${{ github.actor }} |
| 76 | + |
| 77 | + - name: Comment PR if iOS snapshot tests failed |
| 78 | + |
| 79 | + if: always() && steps.failed-ios-screenshots.outputs.PR_COMMENT |
| 80 | + with: |
| 81 | + route: POST /repos/:repo/issues/:issue_number/comments |
| 82 | + repo: ${{ github.repository }} |
| 83 | + issue_number: ${{ steps.failed-ios-screenshots.outputs.PR_NUMBER }} |
| 84 | + body: ${{ steps.failed-ios-screenshots.outputs.PR_COMMENT }} |
| 85 | + env: |
| 86 | + GITHUB_TOKEN: ${{ github.token }} |
0 commit comments