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

Pre-commit action #269

Merged
merged 13 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/pre_commit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Pre-commit CI

on:
pull_request:
push:
branches: [main]

jobs:
pre-commit:
name: Check pre-commit
runs-on: ubuntu-latest

permissions:
contents: write

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.11

- name: Install pre-commit
uses: pre-commit/[email protected]
continue-on-error: true

- name: List modified files
run: |
git diff --name-only

- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: Pre-commit fixes
commit_options: '--no-verify'
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Documentation
mkdocs-material>=9.5.16
mkdocs-material>=9.5.28
mkdocs-glightbox>=0.3.2
mkdocs-material-extensions>=1.3.1
pymdown-extensions>=1.1.1
mkdocs-same-dir>=0.1.2
mkdocs-git-revision-date-localized-plugin>=1.2.0
mkdocs-git-revision-date-localized-plugin>=1.2.6
mkdocs-exclude>=1.0.2

# Developer stuff
Expand Down
21 changes: 21 additions & 0 deletions s5_continuous_integration/pre_commit.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,26 @@ this will make sure that the file is automatically executed whenever we run `git

10. Finally, figure out how to disable `pre-commit` again (if you get tired of it).

11. Assuming you have completed the [module on GitHub Actions](github_actions.md), lets try to add a
`pre-commit` workflow that automatically runs your `pre-commit` checks every time you push to your repository and
then automatically commits those changes to your repository. We recommend that you make use of

* this [pre-commit action](https://github.com/pre-commit/action) for installing and running `pre-commit`
* this [commit action](https://github.com/stefanzweifel/git-auto-commit-action) to automatically commit the
changes that `pre-commit` makes.

As an alternative you configure the [CI tool](https://pre-commit.ci/) provided by the creators of `pre-commit`.

??? success "Solution"

The workflow first uses the `pre-commit` action to install and run the `pre-commit` checks. Importantly we run
it with `continue-on-error: true` to make sure that the workflow does not fail if the checks fail. Next, we use
`git diff` to list the changes that `pre-commit` has made and then we use the `git-auto-commit-action` to commit
those changes.

```yaml linenums="1" title=".github/workflows/pre_commit.yaml"
--8<-- ".github/workflows/pre_commit.yaml"
```

That was all about how `pre-commit` can be used to automate tasks. If you want to deep dive more into the topic you
can checkout this [page](https://pre-commit.com/#python) on how to define your own `pre-commit` hooks.
Loading