Skip to content

Commit 1df94c3

Browse files
authored
✨ feat(action): add uv-install input and auto-detect uvx (#24)
1 parent 90369ff commit 1df94c3

3 files changed

Lines changed: 75 additions & 35 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: 44 additions & 31 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,52 +22,67 @@ 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:
3230
3331
- clones the code
34-
- installs uv
32+
- installs uv (if not already available)
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+
### controlling uv installation
39+
40+
By default (`uv-install: 'auto'`), the action detects whether uv is already available and only installs it if needed.
41+
You can override this with `'true'` (always install) or `'false'` (never install):
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: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@ 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 (true=always, false=never, auto=only if not already available)
10+
required: false
11+
default: 'auto'
812
runs:
913
using: composite
1014
steps:
1115
- name: Check if uv is already installed
16+
if: inputs.uv-install == 'auto'
1217
id: check-uv
1318
run: command -v uv && echo "installed=true" >> "$GITHUB_OUTPUT" || echo "installed=false" >> "$GITHUB_OUTPUT"
1419
shell: bash
1520
- name: Install the latest version of uv
16-
if: steps.check-uv.outputs.installed != 'true'
21+
if: inputs.uv-install == 'true' || (inputs.uv-install == 'auto' && steps.check-uv.outputs.installed != 'true')
1722
uses: astral-sh/setup-uv@v7
1823
with:
1924
enable-cache: true
@@ -24,5 +29,10 @@ runs:
2429
with:
2530
path: ~/.cache/pre-commit
2631
key: pre-commit-3|${{ env.pythonLocation }}|${{ hashFiles('.pre-commit-config.yaml') }}
27-
- run: uv run --no-sync --with pre-commit-uv pre-commit run --show-diff-on-failure --color=always ${{ inputs.extra_args }}
32+
- run: |
33+
if [ -f pyproject.toml ]; then
34+
uv run --no-sync --with pre-commit-uv pre-commit run --show-diff-on-failure --color=always ${{ inputs.extra_args }}
35+
else
36+
uvx --with pre-commit-uv pre-commit run --show-diff-on-failure --color=always ${{ inputs.extra_args }}
37+
fi
2838
shell: bash

0 commit comments

Comments
 (0)