image-actions icon indicating copy to clipboard operation
image-actions copied to clipboard

feature / idea: allow to use as a gating action (fail on non-compressed images)

Open ahmadnassri opened this issue 3 years ago • 3 comments

What:

allow flags to exit as failure if the found images compress at a higher level than the declared threshold, thus indicating that the committed images were not pre-optimized.

Why:

  • large images in git will inflate the total size of the repository and make cloning slow
  • preventing large images from being stored in a git repo at the PR level

How:

  • add an input flag run-as-test (or better wording) which exists with a failure signal if the resulting compression is bigger than the defined quality setting
  • the resulting comment text should also reflect the language of "testing" vs. "results", e.g.
some images did not match the compression requirements:
| Filename        | Variance |
| --------------- | --------- |
| /path/file.jpg  | 20%       |

ahmadnassri avatar Aug 27 '20 15:08 ahmadnassri

@ahmadnassri, this sounds like a helpful and useful addition, but at the moment a change like this would represent a pretty significant change to image-actions.

We've some ideas for alternative run modes and configurable options, but without some work there, we're a while away from being able to add such a feature.

benschwarz avatar Sep 14 '20 04:09 benschwarz

@benschwarz this could be an alternative way to handle forks, if people didn't want to merge uncompressed images followed by a PR to compress them like I added in #54

In fact you could do this after #54 is released with something like this:

name: Compress images
on:
  pull_request:
    # Run image-actions when jpg, jpeg, png or webp files are added or changed
    # See https://help.github.com/en/actions/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#onpushpull_requestpaths
    paths:
      - "**.jpg"
      - "**.jpeg"
      - "**.png"
      - "**.webp"
jobs:
  build:
    name: calibreapp/image-actions
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repo
        uses: actions/checkout@master
      - name: Compress Images
        uses: calibreapp/image-actions@master
        with:
          githubToken: ${{ secrets.GITHUB_TOKEN }}
          compressOnly: true
      - name: Comment on PR if branch contains uncompressed images
        uses: actions/[email protected]
        if: steps.calibre.outputs.markdown != ""
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            const { issue: { number: issue_number }, repo: { owner, repo }  } = context;
            github.issues.createComment({ issue_number, owner, repo, body: 'Warning: Images not compressed.' });

We could also fail the Action in case a comment is missed which is probably better. Imagine this would be fairly easy with one new option (failOnUncompressedImages or something snappier?). Happy to open a PR for this if you like.

tunetheweb avatar Sep 14 '20 13:09 tunetheweb

Actually, thinking about this some more, you could do this already with the following workflow:

name: Check Images are Compressed
on:
  pull_request:
    # Run image-actions when jpg, jpeg, png or webp files are added or changed
    paths:
      - "**.jpg"
      - "**.jpeg"
      - "**.png"
      - "**.webp"
jobs:
  build:
    name: calibreapp/image-actions
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repo
        uses: actions/checkout@master
      - name: Compress Images
        uses: calibreapp/image-actions@master
        with:
          githubToken: ${{ secrets.GITHUB_TOKEN }}
          compressOnly: true
      - name: Fail If Uncompressed Images Exist
        if: steps.calibre.outputs.markdown != ""
        run: exit 1

Not so sure it's necessary (or even a good idea) to actually add this to the Calibre code further than this, but probably would want to be able to specify the significantCompressionPercent value as discussed in #34

tunetheweb avatar Sep 22 '20 11:09 tunetheweb