diff --git a/.github/workflows/test-python.yml b/.github/workflows/test-python.yml index 00043f438..02835a964 100644 --- a/.github/workflows/test-python.yml +++ b/.github/workflows/test-python.yml @@ -10,35 +10,46 @@ on: - '**.md' schedule: - cron: 30 3 * * * + workflow_dispatch: jobs: - default-version: - name: Setup default version + setup-versions-from-manifest: + name: Setup ${{ matrix.python }} ${{ matrix.os }} runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: os: [macos-latest, windows-latest, ubuntu-18.04, ubuntu-20.04] + python: [3.5.4, 3.6.7, 3.7.5, 3.8.1] steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - - name: setup default python + - name: setup-python ${{ matrix.python }} id: setup-python uses: ./ + with: + 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 - run: python --version + run: | + $pythonVersion = (python --version) + if ("Python ${{ matrix.python }}" -ne "$pythonVersion"){ + Write-Host "The current version is $pythonVersion; expected version is ${{ matrix.python }}" + exit 1 + } + $pythonVersion + shell: pwsh - - name: Run simple python code + - name: Run simple code run: python -c 'import math; print(math.factorial(5))' - setup-versions-from-manifest: - name: Setup ${{ matrix.python }} ${{ matrix.os }} + setup-versions-from-file: + name: Setup ${{ matrix.python }} ${{ matrix.os }} version file runs-on: ${{ matrix.os }} strategy: fail-fast: false @@ -47,13 +58,16 @@ jobs: python: [3.5.4, 3.6.7, 3.7.5, 3.8.1] steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 + + - name: build-version-file ${{ matrix.python }} + run: echo ${{ matrix.python }} > .python-version - name: setup-python ${{ matrix.python }} id: setup-python uses: ./ with: - python-version: ${{ matrix.python }} + python-version-file: '.python-version' - name: Check python-path run: ./__tests__/check-python-path.sh '${{ steps.setup-python.outputs.python-path }}' @@ -81,7 +95,7 @@ jobs: os: [macos-latest, windows-latest, ubuntu-18.04, ubuntu-20.04] steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: setup-python 3.9.0-beta.4 id: setup-python diff --git a/action.yml b/action.yml index 00c1e5e49..27474e2aa 100644 --- a/action.yml +++ b/action.yml @@ -4,8 +4,9 @@ description: 'Set up a specific version of Python and add the command-line tools author: 'GitHub' inputs: python-version: - description: "Version range or exact version of a Python version to use, using SemVer's version range syntax." - default: '3.x' + description: "Version range or exact version of Python to use, using SemVer's version range syntax. Reads from .python-version if unset." + python-version-file: + description: "File containing the Python version to use. Example: .python-version" cache: description: 'Used to specify a package manager for caching in the default directory. Supported values: pip, pipenv, poetry.' required: false diff --git a/dist/setup/index.js b/dist/setup/index.js index 60ad85859..9ab89d404 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -64527,12 +64527,16 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; Object.defineProperty(exports, "__esModule", ({ value: true })); const core = __importStar(__nccwpck_require__(2186)); const finder = __importStar(__nccwpck_require__(9996)); const finderPyPy = __importStar(__nccwpck_require__(4003)); const path = __importStar(__nccwpck_require__(1017)); const os = __importStar(__nccwpck_require__(2037)); +const fs_1 = __importDefault(__nccwpck_require__(7147)); const cache_factory_1 = __nccwpck_require__(7549); const utils_1 = __nccwpck_require__(1314); function isPyPyVersion(versionSpec) { @@ -64545,6 +64549,23 @@ function cacheDependencies(cache, pythonVersion) { yield cacheDistributor.restoreCache(); }); } +function resolveVersionInput() { + let version = core.getInput('python-version'); + const versionFile = core.getInput('python-version-file'); + if (version && versionFile) { + core.warning('Both python-version and python-version-file inputs are specified, only python-version will be used'); + } + if (version) { + return version; + } + const versionFilePath = path.join(process.env.GITHUB_WORKSPACE, versionFile || '.python-version'); + if (!fs_1.default.existsSync(versionFilePath)) { + throw new Error(`The specified python version file at: ${versionFilePath} does not exist`); + } + version = fs_1.default.readFileSync(versionFilePath, 'utf8'); + core.info(`Resolved ${versionFile} as ${version}`); + return version; +} function run() { var _a; return __awaiter(this, void 0, void 0, function* () { @@ -64556,7 +64577,7 @@ function run() { core.debug(`Python is expected to be installed into RUNNER_TOOL_CACHE==${process.env['RUNNER_TOOL_CACHE']}`); } try { - const version = core.getInput('python-version'); + const version = resolveVersionInput(); if (version) { let pythonVersion; const arch = core.getInput('architecture') || os.arch(); diff --git a/package.json b/package.json index 4fafdb998..d7d88f24a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "setup-python", - "version": "3.1.1", + "version": "4.0.0", "private": true, "description": "Setup python action", "main": "dist/index.js", diff --git a/src/setup-python.ts b/src/setup-python.ts index e8789de64..f5f8c919d 100644 --- a/src/setup-python.ts +++ b/src/setup-python.ts @@ -3,6 +3,7 @@ import * as finder from './find-python'; import * as finderPyPy from './find-pypy'; import * as path from 'path'; import * as os from 'os'; +import fs from 'fs'; import {getCacheDistributor} from './cache-distributions/cache-factory'; import {isCacheFeatureAvailable} from './utils'; @@ -21,6 +22,35 @@ async function cacheDependencies(cache: string, pythonVersion: string) { await cacheDistributor.restoreCache(); } +function resolveVersionInput(): string { + let version = core.getInput('python-version'); + const versionFile = core.getInput('python-version-file'); + + if (version && versionFile) { + core.warning( + 'Both python-version and python-version-file inputs are specified, only python-version will be used' + ); + } + + if (version) { + return version; + } + + const versionFilePath = path.join( + process.env.GITHUB_WORKSPACE!, + versionFile || '.python-version' + ); + if (!fs.existsSync(versionFilePath)) { + throw new Error( + `The specified python version file at: ${versionFilePath} does not exist` + ); + } + version = fs.readFileSync(versionFilePath, 'utf8'); + core.info(`Resolved ${versionFile} as ${version}`); + + return version; +} + async function run() { if (process.env.AGENT_TOOLSDIRECTORY?.trim()) { core.debug( @@ -33,7 +63,7 @@ async function run() { ); } try { - const version = core.getInput('python-version'); + const version = resolveVersionInput(); if (version) { let pythonVersion: string; const arch: string = core.getInput('architecture') || os.arch();