From ccecdb8870b92c3a7f473d5082c419fe9dbdebf8 Mon Sep 17 00:00:00 2001 From: Kevin Stillhammer Date: Fri, 13 Dec 2024 21:13:41 +0100 Subject: [PATCH] Auto activate venv when python-version is set --- .github/workflows/test-windows.yml | 26 ++++++++++++++++++++++++++ .github/workflows/test.yml | 21 ++++++++++++++------- README.md | 8 +++++--- action.yml | 3 +-- dist/setup/index.js | 20 +++++++++++++++----- src/setup-uv.ts | 13 +++++++++++-- 6 files changed, 72 insertions(+), 19 deletions(-) diff --git a/.github/workflows/test-windows.yml b/.github/workflows/test-windows.yml index 06be023..52364d3 100644 --- a/.github/workflows/test-windows.yml +++ b/.github/workflows/test-windows.yml @@ -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 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 54b97f3..fcd32b4 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 diff --git a/README.md b/README.md index e108a89..8c44d21 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/action.yml b/action.yml index a545c19..9adec89 100644 --- a/action.yml +++ b/action.yml @@ -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: diff --git a/dist/setup/index.js b/dist/setup/index.js index 96300d5..c1dea81 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -99204,6 +99204,7 @@ const download_version_1 = __nccwpck_require__(8255); const restore_cache_1 = __nccwpck_require__(7772); const platforms_1 = __nccwpck_require__(8361); const inputs_1 = __nccwpck_require__(9612); +const exec = __importStar(__nccwpck_require__(5236)); function run() { return __awaiter(this, void 0, void 0, function* () { const platform = (0, platforms_1.getPlatform)(); @@ -99219,7 +99220,7 @@ function run() { addUvToPath(setupResult.uvDir); addToolBinToPath(); setToolDir(); - setupPython(); + yield setupPython(); addMatchers(); setCacheDir(inputs_1.cacheLocalPath); core.setOutput("uv-version", setupResult.version); @@ -99285,10 +99286,19 @@ function setToolDir() { } } function setupPython() { - if (inputs_1.pythonVersion !== "") { - core.exportVariable("UV_PYTHON", inputs_1.pythonVersion); - core.info(`Set UV_PYTHON to ${inputs_1.pythonVersion}`); - } + return __awaiter(this, void 0, void 0, function* () { + if (inputs_1.pythonVersion !== "") { + core.exportVariable("UV_PYTHON", inputs_1.pythonVersion); + core.info(`Set UV_PYTHON to ${inputs_1.pythonVersion}`); + const options = { + silent: !core.isDebug(), + }; + const execArgs = ["venv", "--python", inputs_1.pythonVersion]; + core.info("Activating python venv..."); + yield exec.exec("uv", execArgs, options); + core.addPath(".venv/bin"); + } + }); } function setCacheDir(cacheLocalPath) { core.exportVariable("UV_CACHE_DIR", cacheLocalPath); diff --git a/src/setup-uv.ts b/src/setup-uv.ts index f95babc..af5671f 100644 --- a/src/setup-uv.ts +++ b/src/setup-uv.ts @@ -23,6 +23,7 @@ import { toolDir, version, } from "./utils/inputs"; +import * as exec from "@actions/exec"; async function run(): Promise { const platform = getPlatform(); @@ -46,7 +47,7 @@ async function run(): Promise { addUvToPath(setupResult.uvDir); addToolBinToPath(); setToolDir(); - setupPython(); + await setupPython(); addMatchers(); setCacheDir(cacheLocalPath); @@ -125,10 +126,18 @@ function setToolDir(): void { } } -function setupPython(): void { +async function setupPython(): Promise { 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"); } }