Automatically update pre-commit hooks with GitHub Actions#

We will introduce a mechanism to automatically execute pre-commit autoupdate periodically and create a hook version update PR via GitHub Actions.

Purpose of introducing automatic updates#

  • Hook versions become outdated unless manually updated

  • I want to incorporate security patches and specification changes

  • Also works for changes made by users who are not using pre-commit locally.

Configuration file example#

Place the following in .github/workflows/pre-commit-autoupdate.yml:

name: pre-commit autoupdate

on:
  schedule:
    - cron: '0 0 1 * *'  # 毎月1日 (UTC)
  workflow_dispatch:

jobs:
  autoupdate:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: actions/setup-python@v5
        with:
          python-version: '3.11'

      - name: Install pre-commit
        run: |
          pip install pre-commit

      - name: Run autoupdate
        run: |
          pre-commit autoupdate
          git diff --exit-code || echo "Hooks updated"

      - name: Create PR
        uses: peter-evans/create-pull-request@v6
        with:
          commit-message: "chore: pre-commit hooks autoupdate"
          title: "chore: Update pre-commit hooks"
          body: |
            This PR updates the pre-commit hook versions.
          branch: chore/pre-commit-autoupdate
          delete-branch: true

Points to note and recommended operations#

  • Manually review and merge generated PRs

  • Check the difference and import if there are no problems

  • It is safe to add consistency checks by CI with pre-commit run –all-files

Conclusion#

By incorporating an automatic update mechanism, you can continuously maintain a clean development environment while lowering operational costs.

Article information

Post date:

2025-05-18

author:

Mr. Takagi