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 run: | find . -type f -name "*.png" ! -path "./.git/*" -exec optipng -o7 -nc {} + - 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