95 lines
3.6 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: |
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: |
png_files=$(mktemp) &&
{
if [ "${{ github.event_name }}" = pull_request ] &&
[ "${{ !github.event.pull_request.draft }}" = true ]; then
git fetch origin "$GITHUB_BASE_REF:$GITHUB_BASE_REF" &&
git diff --name-only --diff-filter=AM --no-renames "origin/$GITHUB_BASE_REF" -- '*.png'
else
find "$GITHUB_WORKSPACE" -type f -name "*.png" ! -path "$GITHUB_WORKSPACE/.git/*" -print 2> /dev/null || :
fi
} > "$png_files" &&
printf 'png_files=%s\n' "$png_files" >> "$GITHUB_OUTPUT"
- name: Optimize PNG files
id: optimize_png
run: |
lf="$(printf '\nx')"&&lf=${lf%?}
png_files=${{ steps.get_png_files.outputs.png_files }}
trap 'rm -f -- "$png_files"' EXIT INT
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 < "$png_files"
if [ -n "$failed_files" ]; then
printf 'failed_files<<EOF\n%sEOF\n' "$failed_files" >> "$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 "${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com" &&
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 "${{ github.event.pull_request.number }}" -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