Skip to content

Commit

Permalink
Auto activate venv when python-version is set
Browse files Browse the repository at this point in the history
  • Loading branch information
eifinger committed Dec 16, 2024
1 parent 856099c commit ccecdb8
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 19 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/test-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,29 @@ jobs:
uses: ./
- run: uv sync
working-directory: __tests__\fixtures\uv-project
test-python-version:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- name: Install latest version
uses: ./
with:
python-version: 3.13.1t
- name: Verify UV_PYTHON is set to correct version
run: |
Write-Output "$env:UV_PYTHON"
if ($env:UV_PYTHON -ne "3.13.1t") {
exit 1
}
shell: pwsh
- name: Verify packages can be installed
run: uv pip install --python=3.13.1t pip
shell: pwsh
- name: Verify python version is correct
run: |
python --version
$pythonVersion = python --version
if ($pythonVersion -ne "Python 3.13.1") {
exit 1
}
shell: pwsh
21 changes: 14 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,26 @@ jobs:
fi
test-python-version:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.12", "3.13t"]
steps:
- uses: actions/checkout@v4
- name: Install latest version
uses: ./
with:
python-version: ${{ matrix.python-version }}
python-version: 3.13.1t
- name: Verify UV_PYTHON is set to correct version
run: |
if [ "$UV_PYTHON" != "${{ matrix.python-version }}" ]; then
echo "$UV_PYTHON"
if [ "$UV_PYTHON" != "3.13.1t" ]; then
exit 1
fi
- run: uv sync
working-directory: __tests__/fixtures/uv-project
shell: bash
- name: Verify packages can be installed
run: uv pip install --python=3.13.1t pip
shell: bash
- name: Verify python version is correct
run: |
python --version
if [ "$(python --version)" != "Python 3.13.1" ]; then
exit 1
fi
shell: bash
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,16 @@ to install the latest version that satisfies the range.
### Python version
You can use the input `python-version` to set the environment variable `UV_PYTHON` for the rest
of your workflow.
of your workflow and automatically create a new virtual environment with the specified python version.

This will override any python version specifications in `pyproject.toml` and `.python-version`

```yaml
- name: Install the latest version of uv and set the python version to 3.12
- name: Install the latest version of uv and set the python version to ${{ matrix.python-version }}
uses: astral-sh/setup-uv@v4
with:
python-version: "3.12"
python-version: ${{ matrix.python-version }}
- run: uv pip install --python=${{ matrix.python-version }} pip
```

You can combine this with a matrix to test multiple python versions:
Expand Down
3 changes: 1 addition & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ inputs:
required: false
github-token:
description:
"Used to increase the rate limit when retrieving versions and downloading
uv."
"Used to increase the rate limit when retrieving versions and downloading uv."
required: false
default: ${{ github.token }}
enable-cache:
Expand Down
20 changes: 15 additions & 5 deletions dist/setup/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions src/setup-uv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
toolDir,
version,
} from "./utils/inputs";
import * as exec from "@actions/exec";

async function run(): Promise<void> {
const platform = getPlatform();
Expand All @@ -46,7 +47,7 @@ async function run(): Promise<void> {
addUvToPath(setupResult.uvDir);
addToolBinToPath();
setToolDir();
setupPython();
await setupPython();
addMatchers();
setCacheDir(cacheLocalPath);

Expand Down Expand Up @@ -125,10 +126,18 @@ function setToolDir(): void {
}
}

function setupPython(): void {
async function setupPython(): Promise<void> {
if (pythonVersion !== "") {
core.exportVariable("UV_PYTHON", pythonVersion);
core.info(`Set UV_PYTHON to ${pythonVersion}`);
const options: exec.ExecOptions = {
silent: !core.isDebug(),
};
const execArgs = ["venv", "--python", pythonVersion];

core.info("Activating python venv...");
await exec.exec("uv", execArgs, options);
core.addPath(".venv/bin");
}
}

Expand Down

0 comments on commit ccecdb8

Please sign in to comment.