feature: add a `python-path` output

Expose a `python-path` output containing the chosen Python executable path.
This commit is contained in:
mayeut 2022-05-23 20:59:37 +02:00
parent fff15a21cc
commit ff706563d7
No known key found for this signature in database
GPG Key ID: 8B03CED67D3ABFBA
9 changed files with 84 additions and 4 deletions

View File

@ -37,10 +37,15 @@ jobs:
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: setup-python ${{ matrix.pypy }} - name: setup-python ${{ matrix.pypy }}
id: setup-python
uses: ./ uses: ./
with: with:
python-version: ${{ matrix.pypy }} python-version: ${{ matrix.pypy }}
- name: Check python-path
run: ./__tests__/check-python-path.sh '${{ steps.setup-python.outputs.python-path }}'
shell: bash
- name: PyPy and Python version - name: PyPy and Python version
run: python --version run: python --version

View File

@ -23,9 +23,14 @@ jobs:
- name: Checkout - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: setup default python - name: setup default python
id: setup-python
uses: ./ uses: ./
- name: Check python-path
run: ./__tests__/check-python-path.sh '${{ steps.setup-python.outputs.python-path }}'
shell: bash
- name: Validate version - name: Validate version
run: python --version run: python --version
@ -45,10 +50,15 @@ jobs:
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: setup-python ${{ matrix.python }} - name: setup-python ${{ matrix.python }}
id: setup-python
uses: ./ uses: ./
with: with:
python-version: ${{ matrix.python }} python-version: ${{ matrix.python }}
- name: Check python-path
run: ./__tests__/check-python-path.sh '${{ steps.setup-python.outputs.python-path }}'
shell: bash
- name: Validate version - name: Validate version
run: | run: |
$pythonVersion = (python --version) $pythonVersion = (python --version)
@ -74,10 +84,15 @@ jobs:
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: setup-python 3.9.0-beta.4 - name: setup-python 3.9.0-beta.4
id: setup-python
uses: ./ uses: ./
with: with:
python-version: '3.9.0-beta.4' python-version: '3.9.0-beta.4'
- name: Check python-path
run: ./__tests__/check-python-path.sh '${{ steps.setup-python.outputs.python-path }}'
shell: bash
- name: Validate version - name: Validate version
run: | run: |
$pythonVersion = (python --version) $pythonVersion = (python --version)

View File

@ -89,3 +89,13 @@ jobs:
python-version: 3.8.1 python-version: 3.8.1
- name: Verify 3.8.1 - name: Verify 3.8.1
run: python __tests__/verify-python.py 3.8.1 run: python __tests__/verify-python.py 3.8.1
- name: Run with setup-python 3.10
id: cp310
uses: ./
with:
python-version: "3.10"
- name: Verify 3.10
run: python __tests__/verify-python.py 3.10
- name: Run python-path sample 3.10
run: pipx run --python '${{ steps.cp310.outputs.python-path }}' nox --version

View File

@ -137,6 +137,20 @@ jobs:
``` ```
More details on PyPy syntax and examples of using preview / nightly versions of PyPy can be found in the [Available versions of PyPy](#available-versions-of-pypy) section. More details on PyPy syntax and examples of using preview / nightly versions of PyPy can be found in the [Available versions of PyPy](#available-versions-of-pypy) section.
An output is available with the absolute path of the python interpreter executable if you need it:
```yaml
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
id: cp310
with:
python-version: "3.10"
- run: pipx run --python '${{ steps.cp310.outputs.python-path }}' nox --version
```
# Getting started with Python + Actions # Getting started with Python + Actions
Check out our detailed guide on using [Python with GitHub Actions](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-python-with-github-actions). Check out our detailed guide on using [Python with GitHub Actions](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-python-with-github-actions).

14
__tests__/check-python-path.sh Executable file
View File

@ -0,0 +1,14 @@
#!/bin/bash
set -euo pipefail
PYTHON_PATH="$1"
PATH_EXECUTABLE=$(python -c 'import sys; print(sys.executable)')
PYTHON_PATH_EXECUTABLE=$("${PYTHON_PATH}" -c 'import sys; print(sys.executable)')
if [ "${PATH_EXECUTABLE}" != "${PYTHON_PATH_EXECUTABLE}" ]; then
echo "Executable mismatch."
echo "python in PATH is: ${PATH_EXECUTABLE}"
echo "python-path (${PYTHON_PATH}) is: ${PYTHON_PATH_EXECUTABLE}"
exit 1
fi
echo "python-path: ${PYTHON_PATH}"

View File

@ -21,6 +21,8 @@ outputs:
description: "The installed python version. Useful when given a version range as input." description: "The installed python version. Useful when given a version range as input."
cache-hit: cache-hit:
description: 'A boolean value to indicate a cache entry was found' description: 'A boolean value to indicate a cache entry was found'
python-path:
description: "The absolute path to the Python executable."
runs: runs:
using: 'node16' using: 'node16'
main: 'dist/setup/index.js' main: 'dist/setup/index.js'

9
dist/setup/index.js vendored
View File

@ -52375,12 +52375,15 @@ function findPyPyVersion(versionSpec, architecture) {
} }
const pipDir = utils_1.IS_WINDOWS ? 'Scripts' : 'bin'; const pipDir = utils_1.IS_WINDOWS ? 'Scripts' : 'bin';
const _binDir = path.join(installDir, pipDir); const _binDir = path.join(installDir, pipDir);
const binaryExtension = utils_1.IS_WINDOWS ? '.exe' : '';
const pythonPath = path.join(utils_1.IS_WINDOWS ? installDir : _binDir, `python${binaryExtension}`);
const pythonLocation = pypyInstall.getPyPyBinaryPath(installDir); const pythonLocation = pypyInstall.getPyPyBinaryPath(installDir);
core.exportVariable('pythonLocation', pythonLocation); core.exportVariable('pythonLocation', pythonLocation);
core.exportVariable('PKG_CONFIG_PATH', pythonLocation + '/lib/pkgconfig'); core.exportVariable('PKG_CONFIG_PATH', pythonLocation + '/lib/pkgconfig');
core.addPath(pythonLocation); core.addPath(pythonLocation);
core.addPath(_binDir); core.addPath(_binDir);
core.setOutput('python-version', 'pypy' + resolvedPyPyVersion.trim()); core.setOutput('python-version', 'pypy' + resolvedPyPyVersion.trim());
core.setOutput('python-path', pythonPath);
return { resolvedPyPyVersion, resolvedPythonVersion }; return { resolvedPyPyVersion, resolvedPythonVersion };
}); });
} }
@ -57027,8 +57030,11 @@ function useCpythonVersion(version, architecture) {
core.exportVariable('LD_LIBRARY_PATH', pyLibPath + libPath); core.exportVariable('LD_LIBRARY_PATH', pyLibPath + libPath);
} }
} }
const _binDir = binDir(installDir);
const binaryExtension = utils_1.IS_WINDOWS ? '.exe' : '';
const pythonPath = path.join(utils_1.IS_WINDOWS ? installDir : _binDir, `python${binaryExtension}`);
core.addPath(installDir); core.addPath(installDir);
core.addPath(binDir(installDir)); core.addPath(_binDir);
if (utils_1.IS_WINDOWS) { if (utils_1.IS_WINDOWS) {
// Add --user directory // Add --user directory
// `installDir` from tool cache should look like $RUNNER_TOOL_CACHE/Python/<semantic version>/x64/ // `installDir` from tool cache should look like $RUNNER_TOOL_CACHE/Python/<semantic version>/x64/
@ -57042,6 +57048,7 @@ function useCpythonVersion(version, architecture) {
// On Linux and macOS, pip will create the --user directory and add it to PATH as needed. // On Linux and macOS, pip will create the --user directory and add it to PATH as needed.
const installed = versionFromPath(installDir); const installed = versionFromPath(installDir);
core.setOutput('python-version', installed); core.setOutput('python-version', installed);
core.setOutput('python-path', pythonPath);
return { impl: 'CPython', version: installed }; return { impl: 'CPython', version: installed };
}); });
} }

View File

@ -48,12 +48,18 @@ export async function findPyPyVersion(
const pipDir = IS_WINDOWS ? 'Scripts' : 'bin'; const pipDir = IS_WINDOWS ? 'Scripts' : 'bin';
const _binDir = path.join(installDir, pipDir); const _binDir = path.join(installDir, pipDir);
const binaryExtension = IS_WINDOWS ? '.exe' : '';
const pythonPath = path.join(
IS_WINDOWS ? installDir : _binDir,
`python${binaryExtension}`
);
const pythonLocation = pypyInstall.getPyPyBinaryPath(installDir); const pythonLocation = pypyInstall.getPyPyBinaryPath(installDir);
core.exportVariable('pythonLocation', pythonLocation); core.exportVariable('pythonLocation', pythonLocation);
core.exportVariable('PKG_CONFIG_PATH', pythonLocation + '/lib/pkgconfig'); core.exportVariable('PKG_CONFIG_PATH', pythonLocation + '/lib/pkgconfig');
core.addPath(pythonLocation); core.addPath(pythonLocation);
core.addPath(_binDir); core.addPath(_binDir);
core.setOutput('python-version', 'pypy' + resolvedPyPyVersion.trim()); core.setOutput('python-version', 'pypy' + resolvedPyPyVersion.trim());
core.setOutput('python-path', pythonPath);
return {resolvedPyPyVersion, resolvedPythonVersion}; return {resolvedPyPyVersion, resolvedPythonVersion};
} }

View File

@ -83,8 +83,14 @@ export async function useCpythonVersion(
} }
} }
const _binDir = binDir(installDir);
const binaryExtension = IS_WINDOWS ? '.exe' : '';
const pythonPath = path.join(
IS_WINDOWS ? installDir : _binDir,
`python${binaryExtension}`
);
core.addPath(installDir); core.addPath(installDir);
core.addPath(binDir(installDir)); core.addPath(_binDir);
if (IS_WINDOWS) { if (IS_WINDOWS) {
// Add --user directory // Add --user directory
@ -106,6 +112,7 @@ export async function useCpythonVersion(
const installed = versionFromPath(installDir); const installed = versionFromPath(installDir);
core.setOutput('python-version', installed); core.setOutput('python-version', installed);
core.setOutput('python-path', pythonPath);
return {impl: 'CPython', version: installed}; return {impl: 'CPython', version: installed};
} }