calibreapp / image-actions

A Github Action that automatically compresses JPEGs, PNGs and WebPs in Pull Requests.
https://calibreapp.com/blog/compress-images-in-prs
GNU General Public License v3.0
1.42k stars 67 forks source link

[Feature Request] Only optimized changed images #92

Open rachmari opened 3 years ago

rachmari commented 3 years ago

Thank you for creating this well-documented action! ๐Ÿ™Œ

Also, please forgive me if I missed any documentation or settings that would solve what I'm requesting. ๐Ÿ˜…

What problem would this feature solve?

It looks like this action will optimize all images in the repo that match any selected paths. If that is true, I'd love the ability to be able to only optimize images that are part of the commit that triggered the workflow. This would speed up a potentially long process and target only changed images.

We have a very large directory of images that takes up to 5 hours to compress, but if we know that the existing images are already compressed, it'd be great to only optimize images that are added or updated.

Describe the solution youโ€™d like to see

An input parameter that allows you to select targeting only images that are part of the commit that triggered the workflow. This would likely only be applicable to pull_request and push events.

If the input parameter is set, the action would retrieve the list of changed files and find any image files. If there are changed image files, those images will be compressed.

benschwarz commented 3 years ago

Hey @rachmari ! ๐Ÿ‘‹

We have a very large directory of images that takes up to 5 hours to compress

Whoa! That's โ€ฆ a lot ๐Ÿ˜…

The current image-actions config (found in README) contains:

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 for reference.
    paths:
      - '**.jpg'
      - '**.jpeg'
      - '**.png'
      - '**.webp'

This config means that the action will only be run when images files have been updatedโ€ฆ but what you're asking is slightly different in that you ONLY want to process the files that have been updated, rather than the whole set.

Is that correct?

rachmari commented 3 years ago

@benschwarz hello! ๐Ÿ‘‹

This config means that the action will only be run when images files have been updatedโ€ฆ but what you're asking is slightly different in that you ONLY want to process the files that have been updated, rather than the whole set.

Yep, exactly. I added the workflow below and then added 3 new images to that branch. I expected the workflow to only process those 3 new images, but the log output appeared to be processing all of the .png images in the repo:


name: Compress images
on:
  pull_request:
    paths:
      - '**.png'
jobs:
  build:
    name: calibreapp/image-actions
    runs-on: ubuntu-latest
    # Only run on main repo on and PRs that match the main repo.
    if: github.repository == 'github/docs-internal'
    steps:
      - name: Checkout Branch
        uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f
      - name: Compress Images
        id: calibre
        uses: calibreapp/image-actions@main
        with:
          githubToken: ${{ secrets.GITHUB_TOKEN }} 
``
Maggi64 commented 3 years ago

Can confirm happens to me as well. All images in the repo are getting processed.

tunetheweb commented 3 years ago

Agree this would be useful. And agree currently it does all files each time - that's working as intended at the moment btw, rather than a bug per se, though can understand why it's not what some would want.

BTW I think this should be optional, so can have some runs use this new functionality if/when it's added (maybe "on" by default for all pull request runs?) but still have the option of doing a full run to use the original (slower) full run (e.g. a timed run could be run weekly to compress everything for example in case anything was missed).

Here's how I've got the list of files for another project btw, using bash rather than JavaScript, but might be helpful as a starting off point:

    # If this is part of pull request then get list of files as those changed
    # Uses similar logic to GitHub Super Linter (https://github.com/github/super-linter/blob/master/lib/functions/buildFileList.sh)
    # First checkout main to get list of differences
    git pull --quiet
    git checkout main
    # Then get the changes, for the files needed:
    CHANGED_FILES=$(git diff --name-only "main...${COMMIT_SHA}" --diff-filter=d | egrep -i '\.jpg|\.jpeg|\.png|\.webp'
    # Then back to the pull request branch
    git checkout --progress --force "${COMMIT_SHA}"
karlhorky commented 3 years ago

@bazzadp thanks for the script! Wonder if this could already be used with the paths configuration of the workflow... ๐Ÿค”

Or, if not, maybe as an extra entry in the job steps below, to abort if there are no image changes in the diff...? That seems plausible...

Ah, and in case the PR has new commits since an optimization commit: ideally the optimization should run only on changes / additions since the last commit of the action.

karlhorky commented 3 years ago

@benschwarz would you have any input for workarounds in the meantime?

Or suggestions for how and where this should be implemented in the action, in case someone had time to work on a pull request for this?

FabianKoestring commented 3 years ago

We having the exact same problem! Any news on this?

benschwarz commented 3 years ago

No news from me on this, we're focused in other areas at the moment.

Leaving notes for future me (or someone else) who implements this:

liss-mouse commented 2 years ago

hi! curious - any updates on it? maybe some workarounds at least ๐Ÿ™ i was pretty much surprised this was reported as a feature request not a bug and still not implemented.. specifying the inclusion list of files instead (or along with) of exclusion sounds like a natural behaviour for any repo a bit larger than a demo 'hello world' one. e.g. we have a monorepo with a lot of projects and despite i like this action a lot it's completely useless in such a setup since it would process infinite number of images every time ๐Ÿ˜ข

benschwarz commented 2 years ago

@liss-mouse No, there are no updates. I'm sorry you feel that this project isn't useful for anything more than a hello world. I disagree.

While your use case is valid, your feedback isn't overly helpful and wasn't well received. If your requirements are such that the library doesn't work for you, please consider submitting a PR to amend the behavior, or search for an alternative that meets your needs. Thanks.

paryanihitesh commented 2 years ago

Hi @benschwarz , Is there any update on this?

benschwarz commented 2 years ago

Hi @benschwarz , Is there any update on this?

Nope! I'd post an update if there was one ๐Ÿ˜‰