2025-07-24 11:57:43 +02:00

77 lines
2.7 KiB
YAML

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: sudo apt-get update && sudo apt-get install -y optipng
- name: Optimize PNG files
id: optimize_png
run: |
set -euo pipefail
FAILED_FILES=""
while IFS= read -r -d '' file; do
echo "Compressing: $file"
if ! optipng -o7 -nc "$file" >/dev/null 2>&1; then
FAILED_FILES+="$file"$'\n'
fi
done < <(find . -type f -name "*.png" ! -path "./.git/*" -print0)
if [[ -n "$FAILED_FILES" ]]; then
echo "failed_files<<EOF" >> "$GITHUB_OUTPUT"
echo "$FAILED_FILES" >> "$GITHUB_OUTPUT"
echo "EOF" >> "$GITHUB_OUTPUT"
exit 1
fi
- name: Print files that failed to compress
if: failure() && steps.optimize_png.outputs.failed_files != ''
run: |
echo "The following files failed to compress:"
echo "${{ steps.optimize_png.outputs.failed_files }}"
- name: Check for changes
id: check_changes
run: |
if [[ -n "$(git status --porcelain)" ]]; then
echo "needs_compression=true" >> "$GITHUB_OUTPUT"
else
echo "needs_compression=false" >> "$GITHUB_OUTPUT"
fi
- 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