Skip to content

Commit

Permalink
Merge pull request #914 from carstingaxion/try/auto-compress-screenshots
Browse files Browse the repository at this point in the history
Try/auto compress screenshots
  • Loading branch information
carstingaxion authored Sep 25, 2024
2 parents bbf99c1 + a8eeec2 commit 4b89474
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 11 deletions.
35 changes: 28 additions & 7 deletions .github/workflows/compress-images.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Image Actions will run in the following scenarios:
# - on Pull Requests containing images (not including forks)
# - on demand (https://github.blog/changelog/2020-07-06-github-actions-manual-triggers-with-workflow_dispatch/)
# For Pull Requests, the images are added to the PR.
# For other scenarios, a new PR will be opened if any images are compressed.
name: Compress Images
on:
push:
branches:
- 'fix/wp-org-screenshots-**'
workflow_dispatch:
pull_request:
# Run Image Actions when JPG, JPEG, PNG or WebP files are added or changed.
# See https://help.github.com/en/actions/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#onpushpull_requestpaths for reference.
Expand All @@ -12,9 +15,12 @@ on:
- '**.png'
- '**.webp'
jobs:
build:
# Only run on Pull Requests within the same repository, and not from forks.
#if: github.event.pull_request.head.repo.full_name == github.repository
compress-images:
# Only run on main repo on and PRs that match the main repo.
if: |
github.repository == 'GatherPress/gatherpress' &&
(github.event_name != 'pull_request' ||
github.event.pull_request.head.repo.full_name == github.repository)
name: calibreapp/image-actions
permissions: write-all
runs-on: ubuntu-latest
Expand All @@ -24,11 +30,26 @@ jobs:
uses: actions/checkout@v4

- name: Compress Images
id: compress-images
uses: calibreapp/image-actions@main #main is regularly updated (until 09/2024 so far), while the latest tagged version is from 2020
with:
# Configuration could be added
# https://github.com/marketplace/actions/image-actions#%EF%B8%8F-configuration
#
# The `GITHUB_TOKEN` is automatically generated by GitHub and scoped only to the repository that is currently running the action. By default, the action can’t update Pull Requests initiated from forked repositories.
# See https://docs.github.com/en/actions/reference/authentication-in-a-workflow and https://help.github.com/en/articles/virtual-environments-for-github-actions#token-permissions
githubToken: ${{ secrets.GITHUB_TOKEN }}
githubToken: ${{ secrets.GITHUB_TOKEN }}
# For non-Pull Requests, run in compressOnly mode and we'll PR after.
compressOnly: ${{ github.event_name != 'pull_request' }}

- name: Create Pull Request
# If it's not a Pull Request then commit any changes as a new PR.
if: |
github.event_name != 'pull_request' &&
steps.compress-images.outputs.markdown != ''
uses: peter-evans/create-pull-request@v4
with:
title: Auto Compress Images
branch-suffix: timestamp
commit-message: Compress Images
body: ${{ steps.compress-images.outputs.markdown }}
37 changes: 33 additions & 4 deletions .github/workflows/wordpress-org-screenshots.yml
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,47 @@ jobs:
# DEBUG=pw:api,pw:webserver \
npm run screenshots:wporg -- --update-snapshots
- name: Commit updated screenshots
- name: Checkout new branch
# Using "continue-on-error:true" results in 'conclusion' being a success in any case, while the 'outcome' can differ.
# And, as you already guessed, if: failure() looks at 'conclusion'.
if: ${{ github.event.inputs.updateAllSnapshots || steps.screenshot-tests.outcome == 'failure' }}
run: |
# Remove untracked, temporary file
rm -f ./localized_blueprint.json
git checkout -b fix/wp-org-screenshots-${{ matrix.locale }}-${{ github.sha }}
- name: Compress Images
if: ${{ github.event.inputs.updateAllSnapshots || steps.screenshot-tests.outcome == 'failure' }}
id: compress-images
uses: calibreapp/image-actions@main
with:
githubToken: ${{ secrets.GITHUB_TOKEN }}
# Use the compressOnly option with true value to skip the commit and summary comment.
compressOnly: true
# ignorePaths accepts a comma-separated string with globbing support # https://www.npmjs.com/package/glob
ignorePaths: 'artifacts,build,node_modules/**'

- name: Commit updated screenshots
# Using "continue-on-error:true" results in 'conclusion' being a success in any case, while the 'outcome' can differ.
# And, as you already guessed, if: failure() looks at 'conclusion'.
if: ${{ github.event.inputs.updateAllSnapshots || steps.screenshot-tests.outcome == 'failure' }}
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
# Remove untracked, temporary file
rm -f ./localized_blueprint.json
git add -A
git commit -m "Screenshots for ${{ matrix.locale }} updated!"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Push new branch to origin
# Using "continue-on-error:true" results in 'conclusion' being a success in any case, while the 'outcome' can differ.
# And, as you already guessed, if: failure() looks at 'conclusion'.
if: ${{ github.event.inputs.updateAllSnapshots || steps.screenshot-tests.outcome == 'failure' }}
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git push origin fix/wp-org-screenshots-${{ matrix.locale }}-${{ github.sha }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand All @@ -191,6 +220,6 @@ jobs:
# Using GitHub CLI in Workflows
# https://docs.github.com/en/actions/using-workflows/using-github-cli-in-workflows
# https://cli.github.com/manual/gh_pr_create
run: gh pr create -B main -H fix/wp-org-screenshots-${{ matrix.locale }}-${{ github.sha }} --title 'Update ${{ matrix.locale }} screenshots for wordpress.org' --body 'Created with ❤️ by WordPress Playground, Playwright & GitHub action'
run: gh pr create -B main -H fix/wp-org-screenshots-${{ matrix.locale }}-${{ github.sha }} --title 'Update ${{ matrix.locale }} screenshots for wordpress.org' --body 'Created with ❤️ by WordPress Playground, Playwright & GitHub action <br /><br />${{ steps.compress-images.outputs.markdown }}'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 comments on commit 4b89474

Please sign in to comment.