From ff29395d62f2094bb04b04fd5b86c70e6d02a53b Mon Sep 17 00:00:00 2001 From: Carsten Bach Date: Wed, 25 Sep 2024 12:47:57 +0200 Subject: [PATCH 1/2] Add manual trigger to image-compression workflow --- .github/workflows/compress-images.yml | 35 +++++++++++++++++++++------ 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/.github/workflows/compress-images.yml b/.github/workflows/compress-images.yml index 7fd714d21..d40eeef76 100644 --- a/.github/workflows/compress-images.yml +++ b/.github/workflows/compress-images.yml @@ -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. @@ -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 @@ -24,6 +30,7 @@ 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 @@ -31,4 +38,18 @@ jobs: # # 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 }} \ No newline at end of file + 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 }} \ No newline at end of file From a8eeec280239141b0d13b8f623c279b94bd0ac12 Mon Sep 17 00:00:00 2001 From: Carsten Bach Date: Wed, 25 Sep 2024 12:49:47 +0200 Subject: [PATCH 2/2] Add image-compression (step) to the screenshot-generator workflow --- .../workflows/wordpress-org-screenshots.yml | 37 +++++++++++++++++-- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/.github/workflows/wordpress-org-screenshots.yml b/.github/workflows/wordpress-org-screenshots.yml index c0b7b2000..7113e8a18 100644 --- a/.github/workflows/wordpress-org-screenshots.yml +++ b/.github/workflows/wordpress-org-screenshots.yml @@ -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 }} @@ -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

${{ steps.compress-images.outputs.markdown }}' env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}