From a7dcf25ecb5804cdd871d292628970f91daa76b1 Mon Sep 17 00:00:00 2001 From: boubou19 Date: Sun, 21 Dec 2025 17:15:05 +0100 Subject: [PATCH] Add a reusable workflow for png compression (#49) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Martin Robertz Co-authored-by: Léa Gris --- .github/workflows/optimize-images.yml | 96 +++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 .github/workflows/optimize-images.yml diff --git a/.github/workflows/optimize-images.yml b/.github/workflows/optimize-images.yml new file mode 100644 index 0000000..cbbb4ac --- /dev/null +++ b/.github/workflows/optimize-images.yml @@ -0,0 +1,96 @@ +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 repository + uses: actions/checkout@v4 + + - name: Install optipng + run: | + if ! command -v optipng >/dev/null 2>&1; then + sudo apt-get update && sudo apt-get install -y optipng + fi + + - name: Get PNG files + id: get_png_files + run: | + { + echo 'png_files</dev/null + fi + echo "EOF" + } >> "$GITHUB_OUTPUT" + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + PR_NUMBER: ${{ github.event.pull_request.number }} + + - name: Optimize PNG files + id: optimize_png + run: | + lf="$(printf '\nx')"&&lf=${lf%?} + failed_files="" + while IFS= read -r file; do + [ -n "$file" ] || continue + workspace_file=${file#"$GITHUB_WORKSPACE"} + printf 'Compressing: %s\n' "$workspace_file" + if ! optipng -o7 -nc -quiet "$file" >/dev/null 2>&1; then + failed_files="$failed_files$workspace_file$lf" + fi + done <> "$GITHUB_OUTPUT" + exit 1 + fi + + - name: Print files that failed to compress + if: failure() && steps.optimize_png.outputs.failed_files != '' + run: | + printf 'The following files failed to compress:\n%s\n' "${{ steps.optimize_png.outputs.failed_files }}" + + - name: Check for changes + id: check_changes + run: | + if [ -n "$(git status --porcelain)" ]; then + echo "needs_compression=true" + else + echo "needs_compression=false" + fi >> "$GITHUB_OUTPUT" + + - name: Create Pull Request if Needed + if: ${{ steps.check_changes.outputs.needs_compression == 'true' && 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 }}" \ + 2>&1 | tee pr-message.log || true + gh pr comment "${PR_BRANCH}" -F pr-message.log || true + shell: bash # ensures set -eo pipefail + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + PR_BRANCH: ${{ github.head_ref }} + FIXED_BRANCH: ${{ github.head_ref }}-image-compression + - name: Fail if there was compressed files + if: ${{ steps.check_changes.outputs.needs_compression == 'true' }} + run: | + exit 1