Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting pre-commit GH action to only run for files that have changed #7

Closed
BwL1289 opened this issue Feb 7, 2020 · 13 comments
Closed

Comments

@BwL1289
Copy link

BwL1289 commented Feb 7, 2020

Hi Anthony,

Regarding https://pre-commit.com/#usage-in-continuous-integration, if i only want my action to check files which have changed, how would i incorporate the following?

pre-commit run --origin HEAD --source origin/HEAD

thanks,
Ben

@asottile
Copy link
Member

asottile commented Feb 7, 2020

there isn't an option currently but one might be a good idea. I'm currently trying to rehaul this without GA (due to the limitations) and that's a planned feature there

a PR here would be appreciated if you want to work on such a thing

note that it would have to be an option since there are cases where modifying a file can cause failures in other files (such as with mypy) and often CI is a better guard to run against all files

@BwL1289
Copy link
Author

BwL1289 commented Feb 7, 2020

got it—thanks so much! won't be able to work on a PR (as I don't know JS), but will definitely check back for updates. thanks again for all of your awesome work.

@deepyaman
Copy link
Contributor

deepyaman commented Mar 18, 2020

Hi guys! I didn't see this issue, but I needed this functionality today and found a way around it. #15 is the modified I made to the action, in conjunction with https://github.com/futuratrepadeira/changed-files. See the relevant part of my steps config below:

    - name: Changed Files Exporter
      uses: futuratrepadeira/changed-files@v3.0.0
      id: changed-files
      with:
        repo-token: ${{ github.token }}
    - name: set FILES
      run: |
        echo "::set-env name=FILES::${{ steps.changed-files.outputs.files_created }} ${{ steps.changed-files.outputs.files_updated }} ${{ steps.changed-files.outputs.files_deleted }}"
    - uses: deepyaman/action@v1.0.2  # My modified `pre-commit/action`
      with:
        args: --files ${{ env.FILES }}
        token: ${{ secrets.GITHUB_TOKEN }}

@asottile
Copy link
Member

would args: --from-ref origin/HEAD --to-ref HEAD work as well (instead of needing changed-files)? -- that's mostly what this issue is about

@deepyaman
Copy link
Contributor

would args: --from-ref origin/HEAD --to-ref HEAD work as well (instead of needing changed-files)? -- that's mostly what this issue is about

The args get passed as expected, but the refs aren't found. I can do --to-ref ${{ github.ref }}, which returns remotes/pull/10/merge, but not sure how to get origin/HEAD.

Running git branch -a in a previous step to test doesn't yield any other branches:

Run git branch -a0s
  remotes/pull/10/merge
Run git branch -a
* (HEAD detached at pull/10/merge)
  remotes/pull/10/merge

Maybe you'd have to have another checkout step, but I'm not sure...

@deepyaman
Copy link
Contributor

I'm no expert on GitHub Actions (first tried it yesterday afternoon), but I'm inclined to say you can't access references/branches that naively. I did a lot of googling for finding the list of files that changed in Actions, but didn't find any straightforward git command; e.g.:

I wrote #15 because I wanted to be able to specify different args (not just changed files), and I used it to support the use case in this issue with other actions, but I agree that it would be great to make a more convenient way to run on changed files only as it's a documented common use case.

@jirikuncar
Copy link

@BwL1289 please check #17. I have added job inputs source and origin. There is also an example how you can use it with commit shas.

@asottile
Copy link
Member

#15 provides a way to do this

@dmateusp
Copy link

dmateusp commented Aug 5, 2020

Hey, thought I'd share another example of using this feature:

jobs:
  pre-commit:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
    - uses: actions/setup-python@v1
    - id: file_changes
      uses: trilom/file-changes-action@v1.2.3
      with:
        output: ' '
    - uses: pre-commit/action@v2.0.0
      with:
        extra_args: --files ${{ steps.file_changes.outputs.files}}

@spagh-eddie
Copy link

Hey, thought I'd share another example of using this feature...

thanks! but please note that you should now use this due to trilom/file-changes-action#81

    - id: file_changes
      uses: trilom/file-changes-action@v1.2.4
      with:
        output: 'space'  # no longer ' '

@RayBB
Copy link

RayBB commented Jan 19, 2022

If you want it to run for all files changed for the whole pull request (not just most recent commit) you'll want to follow this:
trilom/file-changes-action#96 (comment)

name: pre-commit
on:
  pull_request
jobs:
  pre-commit:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-python@v2
      with:
        python-version: 3.9
    - id: file_changes
      uses: trilom/file-changes-action@v1.2.4
      with:
        prNumber: ${{ github.event.number }}
        output: ' '
    - uses: pre-commit/action@v2.0.3
      name: mypy
      with: 
        extra_args: mypy --files ${{ steps.file_changes.outputs.files }}

@kuisathaverat
Copy link

This snippet will use the base commit from the target branch (e.g. origin/main) and the latest commit of the PR.

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          # requites to grab the history of the PR
          fetch-depth: 0 
      - uses: actions/setup-python@v3
      - uses: pre-commit/action@v3.0.0
        with:
          extra_args: --color=always --from-ref ${{ github.event.pull_request.base.sha }} --to-ref ${{ github.event.pull_request.head.sha }}

@jpmckinney
Copy link

jpmckinney commented Jun 5, 2023

Given that https://github.com/trilom/file-changes-action seems unmaintained and is using Node 12 and uses set-output, which GitHub will be deprecating and/or disabling, new steps can be:

      - id: changed-files
        uses: tj-actions/changed-files@v36
      - uses: pre-commit/action@v3.0.0
        with:
          extra_args: pip-compile --files ${{ steps.changed-files.outputs.all_changed_files }}

Or whatever your extra_args are.

On push the https://github.com/tj-actions/changed-files action compares to last commit. On pull_request it compares to the base branch (see readme).

https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/
https://github.blog/changelog/2022-09-22-github-actions-all-actions-will-begin-running-on-node16-instead-of-node12/

@pre-commit pre-commit locked as resolved and limited conversation to collaborators Jun 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

9 participants