Skip to content

Commit e6e88c1

Browse files
committed
✨ feat(action): add uv-install input and auto-detect uvx
The action always installed uv and used `uv run`, which is semantically wrong when no pyproject.toml exists and prevents users who already configured uv externally from skipping redundant setup. Add a `uv-install` input (default true) to let users opt out of the built-in setup-uv step. Auto-detect whether to use `uv run` or `uvx` based on pyproject.toml presence so repos without a project file get the correct isolated tool execution. Add CI jobs covering both paths and the external-uv scenario.
1 parent c10240f commit e6e88c1

3 files changed

Lines changed: 73 additions & 33 deletions

File tree

.github/workflows/main.yml

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,26 @@ permissions:
77
contents: read
88

99
jobs:
10-
main:
10+
main-uvx:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- uses: actions/checkout@v6
14-
- name: self test action
14+
- name: self test action (uvx path)
1515
uses: ./
16+
main-uv-run:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@v6
20+
- run: printf '[project]\nname = "test"\nversion = "0.0.0"\nrequires-python = ">=3.13"\n' > pyproject.toml
21+
shell: bash
22+
- name: self test action (uv run path)
23+
uses: ./
24+
main-external-uv:
25+
runs-on: ubuntu-latest
26+
steps:
27+
- uses: actions/checkout@v6
28+
- uses: astral-sh/setup-uv@v7
29+
- name: self test action (external uv)
30+
uses: ./
31+
with:
32+
uv-install: 'false'

README.md

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/tox-dev/action-pre-commit-uv/main.svg)](https://results.pre-commit.ci/latest/github/tox-dev/action-pre-commit-uv/main)
22
[![Build Status](https://github.com/tox-dev/action-pre-commit-uv/actions/workflows/main.yml/badge.svg)](https://github.com/tox-dev/action-pre-commit-uv/actions)
33

4-
tox-dev/action-pre-commit-uv
5-
============================
4+
# tox-dev/action-pre-commit-uv
65

7-
A GitHub action to run [pre-commit](https://pre-commit.com)
8-
using [pre-commit-uv](https://github.com/tox-dev/pre-commit-uv).
6+
A GitHub action to run [pre-commit](https://pre-commit.com) using
7+
[pre-commit-uv](https://github.com/tox-dev/pre-commit-uv).
98

109
### using this action
1110

12-
To use this action, make a file `.github/workflows/pre-commit.yml`. Here's a
13-
template to get started:
11+
To use this action, make a file `.github/workflows/pre-commit.yml`. Here's a template to get started:
1412

1513
```yaml
1614
name: pre-commit
@@ -24,8 +22,8 @@ jobs:
2422
pre-commit:
2523
runs-on: ubuntu-latest
2624
steps:
27-
- uses: actions/checkout@v4
28-
- uses: tox-dev/action-pre-commit-uv@v1
25+
- uses: actions/checkout@v4
26+
- uses: tox-dev/action-pre-commit-uv@v1
2927
```
3028
3129
This does a few things:
@@ -34,42 +32,57 @@ This does a few things:
3432
- installs uv
3533
- sets up the `pre-commit` cache
3634

35+
The action auto-detects the right invocation: if a `pyproject.toml` exists it uses `uv run`, otherwise it uses `uvx` for
36+
isolated tool execution.
37+
38+
### using this action with external uv
39+
40+
If you already set up uv in a prior step (e.g. to pin a specific version or configure caching), you can skip the
41+
built-in installation:
42+
43+
```yaml
44+
- uses: astral-sh/setup-uv@v7
45+
with:
46+
version: 0.6.0
47+
- uses: tox-dev/action-pre-commit-uv@v1
48+
with:
49+
uv-install: 'false'
50+
```
51+
3752
### using this action with custom invocations
3853

39-
By default, this action runs all the hooks against all the files. `extra_args`
40-
lets users specify a single hook id and/or options to pass to `pre-commit run`.
54+
By default, this action runs all the hooks against all the files. `extra_args` lets users specify a single hook id
55+
and/or options to pass to `pre-commit run`.
4156

42-
Here's a sample step configuration that only runs the `flake8` hook against all
43-
the files (use the template above except for the `pre-commit` action):
57+
Here's a sample step configuration that only runs the `flake8` hook against all the files (use the template above except
58+
for the `pre-commit` action):
4459

4560
```yaml
46-
- uses: tox-dev/action-pre-commit-uv@v1
47-
with:
48-
extra_args: flake8 --all-files
61+
- uses: tox-dev/action-pre-commit-uv@v1
62+
with:
63+
extra_args: flake8 --all-files
4964
```
5065

5166
### using this action in private repositories
5267

53-
prior to v3.0.0, this action had custom behaviour which pushed changes back to
54-
the pull request when supplied with a `token`.
68+
prior to v3.0.0, this action had custom behaviour which pushed changes back to the pull request when supplied with a
69+
`token`.
5570

5671
this behaviour was removed:
72+
5773
- it required a PAT (didn't work with short-lived `GITHUB_TOKEN`)
58-
- properly hiding this `input` from the installation and execution of hooks
59-
is intractable in github actions (it is readily available as `$INPUT_TOKEN`)
60-
- this meant potentially unvetted code could access the token via the
61-
environment
74+
- properly hiding this `input` from the installation and execution of hooks is intractable in github actions (it is
75+
readily available as `$INPUT_TOKEN`)
76+
- this meant potentially unvetted code could access the token via the environment
6277

63-
you can _likely_ achieve the same thing with an external action such as
64-
[git-auto-commit-action] though you may want to take precautions to clear `git`
65-
hooks or other ways that arbitrary code execution can occur when running
66-
`git commit` / `git push` (for example [core.fsmonitor]).
78+
you can _likely_ achieve the same thing with an external action such as [git-auto-commit-action] though you may want to
79+
take precautions to clear `git` hooks or other ways that arbitrary code execution can occur when running `git commit` /
80+
`git push` (for example [core.fsmonitor]).
6781

68-
while unrelated to this action, [pre-commit.ci] avoids these problems by
69-
installing and executing isolated from the short-lived repository-scoped
70-
[installation access token].
82+
while unrelated to this action, [pre-commit.ci] avoids these problems by installing and executing isolated from the
83+
short-lived repository-scoped [installation access token].
7184

72-
[git-auto-commit-action]: https://github.com/stefanzweifel/git-auto-commit-action
7385
[core.fsmonitor]: https://github.blog/2022-04-12-git-security-vulnerability-announced/
74-
[pre-commit.ci]: https://pre-commit.ci
86+
[git-auto-commit-action]: https://github.com/stefanzweifel/git-auto-commit-action
7587
[installation access token]: https://docs.github.com/en/rest/apps/apps#create-an-installation-access-token-for-an-app
88+
[pre-commit.ci]: https://pre-commit.ci

action.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ inputs:
55
description: options to pass to pre-commit run
66
required: false
77
default: '--all-files'
8+
uv-install:
9+
description: whether to install uv (set to false if uv is already available)
10+
required: false
11+
default: 'true'
812
runs:
913
using: composite
1014
steps:
1115
- name: Install the latest version of uv
16+
if: inputs.uv-install == 'true'
1217
uses: astral-sh/setup-uv@v7
1318
with:
1419
enable-cache: true
@@ -19,5 +24,10 @@ runs:
1924
with:
2025
path: ~/.cache/pre-commit
2126
key: pre-commit-3|${{ env.pythonLocation }}|${{ hashFiles('.pre-commit-config.yaml') }}
22-
- run: uv run --no-sync --with pre-commit-uv pre-commit run --show-diff-on-failure --color=always ${{ inputs.extra_args }}
27+
- run: |
28+
if [ -f pyproject.toml ]; then
29+
uv run --no-sync --with pre-commit-uv pre-commit run --show-diff-on-failure --color=always ${{ inputs.extra_args }}
30+
else
31+
uvx --with pre-commit-uv pre-commit run --show-diff-on-failure --color=always ${{ inputs.extra_args }}
32+
fi
2333
shell: bash

0 commit comments

Comments
 (0)