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 13, 2024
1 parent 856099c commit 522f9d6
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 14 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,11 @@ jobs:
if [ "$UV_PYTHON" != "${{ matrix.python-version }}" ]; then
exit 1
fi
- run: uv sync
working-directory: __tests__/fixtures/uv-project
- name: Verify packages can be installed
run: uv pip install --python=${{ matrix.python-version }} pip
- name: Verify python version is correct
run: |
python --version
if [ "$(python --version)" != "${{ matrix.python-version }}" ]; then
exit 1
fi
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: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ inputs:
tool-bin-dir:
description: "Custom path to set UV_TOOL_BIN_DIR to."
required: false
python-install-dir:
description: "Custom path where uv will install python interpreters."
required: false
outputs:
uv-version:
description: "The installed uv version. Useful when using latest."
Expand Down
13 changes: 12 additions & 1 deletion dist/save-cache/index.js

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

39 changes: 33 additions & 6 deletions dist/setup/index.js

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

21 changes: 19 additions & 2 deletions src/setup-uv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ import {
checkSum,
enableCache,
githubToken,
pythonInstallDir,
pythonVersion,
toolBinDir,
toolDir,
version,
} from "./utils/inputs";
import * as exec from "@actions/exec";

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

Expand Down Expand Up @@ -98,6 +101,13 @@ function addUvToPath(cachedPath: string): void {
core.info(`Added ${cachedPath} to the path`);
}

function addUvPythonInstallDirToPath(): void {
core.exportVariable("UV_PYTHON_INSTALL_DIR", toolBinDir);
core.info(`Set UV_PYTHON_INSTALL_DIR to ${toolBinDir}`);
core.addPath(pythonInstallDir);
core.info(`Added ${pythonInstallDir} to the path`);
}

function addToolBinToPath(): void {
if (toolBinDir !== undefined) {
core.exportVariable("UV_TOOL_BIN_DIR", toolBinDir);
Expand Down Expand Up @@ -125,10 +135,17 @@ 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);
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/utils/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const ignoreNothingToCache =
core.getInput("ignore-nothing-to-cache") === "true";
export const toolBinDir = getToolBinDir();
export const toolDir = getToolDir();
export const pythonInstallDir = getPythonInstallDir();
export const githubToken = core.getInput("github-token");

function getEnableCache(): boolean {
Expand Down Expand Up @@ -55,6 +56,19 @@ function getToolDir(): string | undefined {
return undefined;
}

function getPythonInstallDir(): string {
const pythonInstallDirInput = core.getInput("python-install-dir");
if (pythonInstallDirInput !== "") {
return expandTilde(pythonInstallDirInput);
}
if (process.env.RUNNER_TEMP !== undefined) {
return `${process.env.RUNNER_TEMP}${path.sep}uv-python-install-dir`;
}
throw Error(
"Could not determine UV_PYTHON_INSTALL_DIR. Please make sure RUNNER_TEMP is set or provide the python-install-dir input",
);
}

function getCacheLocalPath(): string {
const cacheLocalPathInput = core.getInput("cache-local-path");
if (cacheLocalPathInput !== "") {
Expand Down

0 comments on commit 522f9d6

Please sign in to comment.