Skip to content

Commit dd57877

Browse files
authored
Auto activate venv when python-version is set (#194)
Closes: #124
1 parent 85aa0bf commit dd57877

File tree

5 files changed

+62
-18
lines changed

5 files changed

+62
-18
lines changed

.github/workflows/test.yml

+15-5
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,27 @@ jobs:
143143
runs-on: ubuntu-latest
144144
strategy:
145145
matrix:
146-
python-version: ["3.12", "3.13t"]
146+
os: [ubuntu-latest, macos-latest, windows-latest]
147147
steps:
148148
- uses: actions/checkout@v4
149149
- name: Install latest version
150150
uses: ./
151151
with:
152-
python-version: ${{ matrix.python-version }}
152+
python-version: 3.13.1t
153153
- name: Verify UV_PYTHON is set to correct version
154154
run: |
155-
if [ "$UV_PYTHON" != "${{ matrix.python-version }}" ]; then
155+
echo "$UV_PYTHON"
156+
if [ "$UV_PYTHON" != "3.13.1t" ]; then
156157
exit 1
157158
fi
158-
- run: uv sync
159-
working-directory: __tests__/fixtures/uv-project
159+
shell: bash
160+
- name: Verify packages can be installed
161+
run: uv pip install --python=3.13.1t pip
162+
shell: bash
163+
- name: Verify python version is correct
164+
run: |
165+
python --version
166+
if [ "$(python --version)" != "Python 3.13.1" ]; then
167+
exit 1
168+
fi
169+
shell: bash

README.md

+9-4
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,20 @@ to install the latest version that satisfies the range.
7272
7373
### Python version
7474
75-
You can use the input `python-version` to set the environment variable `UV_PYTHON` for the rest
76-
of your workflow.
75+
You can use the input `python-version` to
76+
77+
- set the environment variable `UV_PYTHON` for the rest of your workflow
78+
- create a new virtual environment with the specified python version
79+
- activate the virtual environment for the rest of your workflow
80+
7781
This will override any python version specifications in `pyproject.toml` and `.python-version`
7882

7983
```yaml
80-
- name: Install the latest version of uv and set the python version to 3.12
84+
- name: Install the latest version of uv and set the python version to 3.13t
8185
uses: astral-sh/setup-uv@v4
8286
with:
83-
python-version: "3.12"
87+
python-version: 3.13t
88+
- run: uv pip install --python=3.13t pip
8489
```
8590

8691
You can combine this with a matrix to test multiple python versions:

action.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ inputs:
1414
required: false
1515
github-token:
1616
description:
17-
"Used to increase the rate limit when retrieving versions and downloading
18-
uv."
17+
"Used to increase the rate limit when retrieving versions and downloading uv."
1918
required: false
2019
default: ${{ github.token }}
2120
enable-cache:

dist/setup/index.js

+20-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/setup-uv.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
toolDir,
2424
version,
2525
} from "./utils/inputs";
26+
import * as exec from "@actions/exec";
2627

2728
async function run(): Promise<void> {
2829
const platform = getPlatform();
@@ -46,7 +47,7 @@ async function run(): Promise<void> {
4647
addUvToPath(setupResult.uvDir);
4748
addToolBinToPath();
4849
setToolDir();
49-
setupPython();
50+
await setupPython();
5051
addMatchers();
5152
setCacheDir(cacheLocalPath);
5253

@@ -125,10 +126,24 @@ function setToolDir(): void {
125126
}
126127
}
127128

128-
function setupPython(): void {
129+
async function setupPython(): Promise<void> {
129130
if (pythonVersion !== "") {
130131
core.exportVariable("UV_PYTHON", pythonVersion);
131132
core.info(`Set UV_PYTHON to ${pythonVersion}`);
133+
const options: exec.ExecOptions = {
134+
silent: !core.isDebug(),
135+
};
136+
const execArgs = ["venv", "--python", pythonVersion];
137+
138+
core.info("Activating python venv...");
139+
await exec.exec("uv", execArgs, options);
140+
141+
let venvBinPath = ".venv/bin";
142+
if (process.platform === "win32") {
143+
venvBinPath = ".venv/Scripts";
144+
}
145+
core.addPath(venvBinPath);
146+
core.exportVariable("VIRTUAL_ENV", venvBinPath);
132147
}
133148
}
134149

0 commit comments

Comments
 (0)