diff --git a/.github/workflows/optimize-images.yml b/.github/workflows/optimize-images.yml new file mode 100644 index 0000000..2eb1aae --- /dev/null +++ b/.github/workflows/optimize-images.yml @@ -0,0 +1,55 @@ +name: Check for image compression + +on: + workflow_call: + +jobs: + compress: + name: Compress and make a PR if needed + runs-on: ubuntu-24.04 + + steps: + - name: Checkout Repo + uses: actions/checkout@v4 + + - name: Compress Images + id: compress-images + uses: stellasoftio/image-optimizer-action@v1 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + compress-png: true + compress-svg: false + compress-jpg: false + compress-webp: false + compress-avif: false + + - name: Fail if image optimization was needed + if: steps.compress-images.outputs.markdown_report != '' + run: | + echo "Unoptimized image(s) have been detected." + exit 1 + + + - name: Create Pull Request if Needed + if: steps.compress-images.outputs.markdown_report != '' + if: ${{ failure() && github.event_name == 'pull_request' && !github.event.pull_request.draft }} + run: | + git config user.name "GitHub GTNH Actions" + git config user.email "<>" + git switch -c "${FIXED_BRANCH}" + git commit -am "optimizing images" + git push --force-with-lease origin "${FIXED_BRANCH}" + gh pr create \ + --head "${FIXED_BRANCH}" \ + --base "${PR_BRANCH}" \ + --title "Optimising images contained in ${PR_BRANCH} for #${{ github.event.pull_request.number }}" \ + --body "Automatic image compression, applies to PR #${{ github.event.pull_request.number }}\n\n${{ steps.compress-images.outputs.markdown_report }}" \ + 2>&1 | tee pr-message.log || true + gh pr comment "${PR_BRANCH}" -F pr-message.log || true + shell: + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + PR_BRANCH: ${{ github.head_ref }} + FIXED_BRANCH: ${{ github.head_ref }}-image-compression + + \ No newline at end of file