mirror of
				https://kkgithub.com/actions/setup-python.git
				synced 2025-11-04 12:44:05 +08:00 
			
		
		
		
	Compare commits
	
		
			9 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 0c28554988 | |||
| 7010ec794f | |||
| 306c473438 | |||
| 7a69c2bc7d | |||
| 654aa00a6e | |||
| 6f45e887f6 | |||
| 2989dc4a1a | |||
| a0a76c4ddb | |||
| 2241d846ba | 
							
								
								
									
										31
									
								
								.github/workflows/test.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										31
									
								
								.github/workflows/test.yml
									
									
									
									
										vendored
									
									
								
							@ -2,7 +2,7 @@ name: Validate 'setup-python'
 | 
			
		||||
on: 
 | 
			
		||||
  push:
 | 
			
		||||
    branches:
 | 
			
		||||
      - master
 | 
			
		||||
      - main
 | 
			
		||||
    paths-ignore:
 | 
			
		||||
      - '**.md'
 | 
			
		||||
  pull_request:
 | 
			
		||||
@ -61,3 +61,32 @@ jobs:
 | 
			
		||||
 | 
			
		||||
    - name: Run simple code
 | 
			
		||||
      run: python -c 'import math; print(math.factorial(5))'
 | 
			
		||||
 | 
			
		||||
  setup-pre-release-version-from-manifest:
 | 
			
		||||
    name: Setup 3.9.0-beta.4 ${{ matrix.os }}
 | 
			
		||||
    runs-on: ${{ matrix.os }}
 | 
			
		||||
    strategy:
 | 
			
		||||
      fail-fast: false
 | 
			
		||||
      matrix:
 | 
			
		||||
        os: [macos-latest, windows-latest, ubuntu-16.04, ubuntu-18.04]
 | 
			
		||||
    steps:
 | 
			
		||||
    - name: Checkout
 | 
			
		||||
      uses: actions/checkout@v2
 | 
			
		||||
 | 
			
		||||
    - name: setup-python 3.9.0-beta.4
 | 
			
		||||
      uses: ./
 | 
			
		||||
      with:
 | 
			
		||||
        python-version: '3.9.0-beta.4'
 | 
			
		||||
 | 
			
		||||
    - name: Validate version
 | 
			
		||||
      run: |
 | 
			
		||||
        $pythonVersion = (python --version)
 | 
			
		||||
        if ("Python 3.9.0b4" -ne "$pythonVersion"){
 | 
			
		||||
          Write-Host "The current version is $pythonVersion; expected version is 3.9.0b4"
 | 
			
		||||
          exit 1
 | 
			
		||||
        }
 | 
			
		||||
        $pythonVersion
 | 
			
		||||
      shell: pwsh
 | 
			
		||||
 | 
			
		||||
    - name: Run simple code
 | 
			
		||||
      run: python -c 'import math; print(math.factorial(5))'
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										2
									
								
								.github/workflows/workflow.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.github/workflows/workflow.yml
									
									
									
									
										vendored
									
									
								
							@ -2,7 +2,7 @@ name: Main workflow
 | 
			
		||||
on:
 | 
			
		||||
  push:
 | 
			
		||||
    branches:
 | 
			
		||||
      - master
 | 
			
		||||
      - main
 | 
			
		||||
    paths-ignore:
 | 
			
		||||
      - '**.md'
 | 
			
		||||
  pull_request:
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										29
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								README.md
									
									
									
									
									
								
							@ -16,6 +16,7 @@ This action sets up a Python environment for use in actions by:
 | 
			
		||||
- Ability to download, install and set up Python packages from `actions/python-versions` that do not come preinstalled on runners
 | 
			
		||||
  - Allows for pinning to a specific patch version of Python without the worry of it ever being removed or changed
 | 
			
		||||
- Automatic setup and download of Python packages if using a self-hosted runner
 | 
			
		||||
- Support for pre-release versions of Python
 | 
			
		||||
 | 
			
		||||
# Usage
 | 
			
		||||
 | 
			
		||||
@ -87,11 +88,31 @@ jobs:
 | 
			
		||||
    - uses: actions/checkout@v2
 | 
			
		||||
    - uses: actions/setup-python@v2
 | 
			
		||||
      with:
 | 
			
		||||
        python-version: ${{ matrix.python }}
 | 
			
		||||
        python-version: ${{ matrix.python-version }}
 | 
			
		||||
    - run: python my_script.py
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
Download and set up a accurate pre-release version of Python:
 | 
			
		||||
```yaml
 | 
			
		||||
steps:
 | 
			
		||||
- uses: actions/checkout@v2
 | 
			
		||||
- uses: actions/setup-python@v2
 | 
			
		||||
  with:
 | 
			
		||||
    python-version: '3.9.0-beta.4'
 | 
			
		||||
- run: python my_script.py
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
Download and set up the latest available version of Python (includes both pre-release and stable versions):
 | 
			
		||||
```yaml
 | 
			
		||||
steps:
 | 
			
		||||
- uses: actions/checkout@v2
 | 
			
		||||
- uses: actions/setup-python@v2
 | 
			
		||||
  with:
 | 
			
		||||
    python-version: '3.9.0-alpha - 3.9.0' # SemVer's version range syntax
 | 
			
		||||
- run: python my_script.py
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
# 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).
 | 
			
		||||
@ -106,7 +127,7 @@ Check out our detailed guide on using [Python with GitHub Actions](https://help.
 | 
			
		||||
    - If `3.8.1` is installed for example, and `3.8.2` is released, expect `3.8.1` to be removed and replaced by `3.8.2` in the tools cache.
 | 
			
		||||
    - If the exact patch version doesn't matter to you, specifying just the major and minor version will get you the latest preinstalled patch version. In the previous example, the version spec `3.8` will use the `3.8.2` Python version found in the cache.
 | 
			
		||||
- Downloadable Python versions from GitHub Releases ([actions/python-versions](https://github.com/actions/python-versions/releases))
 | 
			
		||||
    - All available versions are listed in the [version-manifest.json](https://github.com/actions/python-versions/blob/master/versions-manifest.json) file.
 | 
			
		||||
    - All available versions are listed in the [version-manifest.json](https://github.com/actions/python-versions/blob/main/versions-manifest.json) file.
 | 
			
		||||
    - If there is a specific version of Python that is not available, you can open an issue here
 | 
			
		||||
 | 
			
		||||
# Hosted Tool Cache
 | 
			
		||||
@ -136,7 +157,9 @@ You should specify only a major and minor version if you are okay with the most
 | 
			
		||||
 | 
			
		||||
# Using `setup-python` with a self hosted runner
 | 
			
		||||
 | 
			
		||||
If you would like to use `setup-python` and a self-hosted runner, there are a few extra things you need to make sure are set up so that new versions of Python can be downloaded and configured on your runner.
 | 
			
		||||
Python distributions are only available for the same [environments](https://github.com/actions/virtual-environments#available-environments) that GitHub Actions hosted environments are available for. If you are using an unsupported version of Ubuntu such as `19.04` or another Linux distribution such as Fedora, `setup-python` will not work. If you have a supported self-hosted runner and you would like to use `setup-python`, there are a few extra things you need to make sure are set up so that new versions of Python can be downloaded and configured on your runner.
 | 
			
		||||
 | 
			
		||||
If you are experiencing problems while configuring Python on your self-hosted runner, turn on [step debugging](https://github.com/actions/toolkit/blob/main/docs/action-debugging.md#step-debug-logs) to see addition logs.
 | 
			
		||||
 | 
			
		||||
### Windows
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,13 +0,0 @@
 | 
			
		||||
{
 | 
			
		||||
    "version": "1.2.3",
 | 
			
		||||
    "stable": true,
 | 
			
		||||
    "release_url": "https://github.com/actions/sometool/releases/tag/1.2.3-20200402.6",
 | 
			
		||||
    "files": [
 | 
			
		||||
      {
 | 
			
		||||
        "filename": "sometool-1.2.3-linux-x64.tar.gz",
 | 
			
		||||
        "arch": "x64",
 | 
			
		||||
        "platform": "linux",
 | 
			
		||||
        "download_url": "https://github.com/actions/sometool/releases/tag/1.2.3-20200402.6/sometool-1.2.3-linux-x64.tar.gz"
 | 
			
		||||
      }      
 | 
			
		||||
    ]
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										52
									
								
								__tests__/data/versions-manifest.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								__tests__/data/versions-manifest.json
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,52 @@
 | 
			
		||||
[
 | 
			
		||||
    {
 | 
			
		||||
      "version": "1.2.3",
 | 
			
		||||
      "stable": true,
 | 
			
		||||
      "release_url": "https://github.com/actions/sometool/releases/tag/1.2.3-20200402.6",
 | 
			
		||||
      "files": [
 | 
			
		||||
        {
 | 
			
		||||
          "filename": "sometool-1.2.3-linux-x64.tar.gz",
 | 
			
		||||
          "arch": "x64",
 | 
			
		||||
          "platform": "linux",
 | 
			
		||||
          "download_url": "https://github.com/actions/sometool/releases/tag/1.2.3-20200402.6/sometool-1.2.3-linux-x64.tar.gz"
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
          "filename": "sometool-1.2.3-darwin-x64.tar.gz",
 | 
			
		||||
          "arch": "x64",
 | 
			
		||||
          "platform": "darwin",
 | 
			
		||||
          "download_url": "https://github.com/actions/sometool/releases/tag/1.2.3-20200402.6/sometool-1.2.3-darwin-x64.tar.gz"
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
          "filename": "sometool-1.2.3-win32-x64.tar.gz",
 | 
			
		||||
          "arch": "x64",
 | 
			
		||||
          "platform": "win32",
 | 
			
		||||
          "download_url": "https://github.com/actions/sometool/releases/tag/1.2.3-20200402.6/sometool-1.2.3-win32-x64.tar.gz"
 | 
			
		||||
        }
 | 
			
		||||
      ]
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "version": "1.2.3-beta.2",
 | 
			
		||||
      "stable": false,
 | 
			
		||||
      "release_url": "https://github.com/actions/sometool/releases/tag/1.2.3-beta.2-20200402.5",
 | 
			
		||||
      "files": [
 | 
			
		||||
        {
 | 
			
		||||
          "filename": "sometool-1.2.3-linux-x64.tar.gz",
 | 
			
		||||
          "arch": "x64",
 | 
			
		||||
          "platform": "linux",
 | 
			
		||||
          "download_url": "https://github.com/actions/sometool/releases/tag/1.2.3-beta.2-20200402.5/sometool-1.2.3-linux-x64.tar.gz"
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
          "filename": "sometool-1.2.3-darwin-x64.tar.gz",
 | 
			
		||||
          "arch": "x64",
 | 
			
		||||
          "platform": "darwin",
 | 
			
		||||
          "download_url": "https://github.com/actions/sometool/releases/tag/1.2.3-20200402.5/sometool-1.2.3-darwin-x64.tar.gz"
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
          "filename": "sometool-1.2.3-win32-x64.tar.gz",
 | 
			
		||||
          "arch": "x64",
 | 
			
		||||
          "platform": "win32",
 | 
			
		||||
          "download_url": "https://github.com/actions/sometool/releases/tag/1.2.3-20200402.5/sometool-1.2.3-win32-x64.tar.gz"
 | 
			
		||||
        }
 | 
			
		||||
      ]
 | 
			
		||||
    }
 | 
			
		||||
]
 | 
			
		||||
@ -22,7 +22,7 @@ import * as tc from '@actions/tool-cache';
 | 
			
		||||
import * as finder from '../src/find-python';
 | 
			
		||||
import * as installer from '../src/install-python';
 | 
			
		||||
 | 
			
		||||
const pythonRelease = require('./data/python-release.json');
 | 
			
		||||
const manifestData = require('./data/versions-manifest.json');
 | 
			
		||||
 | 
			
		||||
describe('Finder tests', () => {
 | 
			
		||||
  afterEach(() => {
 | 
			
		||||
@ -38,12 +38,9 @@ describe('Finder tests', () => {
 | 
			
		||||
    await finder.findPythonVersion('3.x', 'x64');
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  it('Finds Python if it is not installed, but exists in the manifest', async () => {
 | 
			
		||||
    const findSpy: jest.SpyInstance = jest.spyOn(
 | 
			
		||||
      installer,
 | 
			
		||||
      'findReleaseFromManifest'
 | 
			
		||||
    );
 | 
			
		||||
    findSpy.mockImplementation(() => <tc.IToolRelease>pythonRelease);
 | 
			
		||||
  it('Finds stable Python version if it is not installed, but exists in the manifest', async () => {
 | 
			
		||||
    const findSpy: jest.SpyInstance = jest.spyOn(tc, 'getManifestFromRepo');
 | 
			
		||||
    findSpy.mockImplementation(() => <tc.IToolRelease[]>manifestData);
 | 
			
		||||
 | 
			
		||||
    const installSpy: jest.SpyInstance = jest.spyOn(
 | 
			
		||||
      installer,
 | 
			
		||||
@ -58,6 +55,28 @@ describe('Finder tests', () => {
 | 
			
		||||
    await finder.findPythonVersion('1.2.3', 'x64');
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  it('Finds pre-release Python version in the manifest', async () => {
 | 
			
		||||
    const findSpy: jest.SpyInstance = jest.spyOn(tc, 'getManifestFromRepo');
 | 
			
		||||
    findSpy.mockImplementation(() => <tc.IToolRelease[]>manifestData);
 | 
			
		||||
 | 
			
		||||
    const installSpy: jest.SpyInstance = jest.spyOn(
 | 
			
		||||
      installer,
 | 
			
		||||
      'installCpythonFromRelease'
 | 
			
		||||
    );
 | 
			
		||||
    installSpy.mockImplementation(async () => {
 | 
			
		||||
      const pythonDir: string = path.join(
 | 
			
		||||
        toolDir,
 | 
			
		||||
        'Python',
 | 
			
		||||
        '1.2.3-beta.2',
 | 
			
		||||
        'x64'
 | 
			
		||||
      );
 | 
			
		||||
      await io.mkdirP(pythonDir);
 | 
			
		||||
      fs.writeFileSync(`${pythonDir}.complete`, 'hello');
 | 
			
		||||
    });
 | 
			
		||||
    // This will throw if it doesn't find it in the manifest (because no such version exists)
 | 
			
		||||
    await finder.findPythonVersion('1.2.3-beta.2', 'x64');
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  it('Errors if Python is not installed', async () => {
 | 
			
		||||
    // This will throw if it doesn't find it in the cache and in the manifest (because no such version exists)
 | 
			
		||||
    let thrown = false;
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										8738
									
								
								dist/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										8738
									
								
								dist/index.js
									
									
									
									
										vendored
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										6
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										6
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							@ -3030,9 +3030,9 @@
 | 
			
		||||
      }
 | 
			
		||||
    },
 | 
			
		||||
    "lodash": {
 | 
			
		||||
      "version": "4.17.15",
 | 
			
		||||
      "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz",
 | 
			
		||||
      "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==",
 | 
			
		||||
      "version": "4.17.19",
 | 
			
		||||
      "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.19.tgz",
 | 
			
		||||
      "integrity": "sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ==",
 | 
			
		||||
      "dev": true
 | 
			
		||||
    },
 | 
			
		||||
    "lodash.memoize": {
 | 
			
		||||
 | 
			
		||||
@ -3,12 +3,14 @@ import * as core from '@actions/core';
 | 
			
		||||
import * as tc from '@actions/tool-cache';
 | 
			
		||||
import * as exec from '@actions/exec';
 | 
			
		||||
import {ExecOptions} from '@actions/exec/lib/interfaces';
 | 
			
		||||
import {stderr} from 'process';
 | 
			
		||||
 | 
			
		||||
const TOKEN = core.getInput('token');
 | 
			
		||||
const AUTH = !TOKEN || isGhes() ? undefined : `token ${TOKEN}`;
 | 
			
		||||
const MANIFEST_REPO_OWNER = 'actions';
 | 
			
		||||
const MANIFEST_REPO_NAME = 'python-versions';
 | 
			
		||||
export const MANIFEST_URL = `https://raw.githubusercontent.com/${MANIFEST_REPO_OWNER}/${MANIFEST_REPO_NAME}/master/versions-manifest.json`;
 | 
			
		||||
const MANIFEST_REPO_BRANCH = 'main';
 | 
			
		||||
export const MANIFEST_URL = `https://raw.githubusercontent.com/${MANIFEST_REPO_OWNER}/${MANIFEST_REPO_NAME}/${MANIFEST_REPO_BRANCH}/versions-manifest.json`;
 | 
			
		||||
 | 
			
		||||
const IS_WINDOWS = process.platform === 'win32';
 | 
			
		||||
 | 
			
		||||
@ -19,11 +21,12 @@ export async function findReleaseFromManifest(
 | 
			
		||||
  const manifest: tc.IToolRelease[] = await tc.getManifestFromRepo(
 | 
			
		||||
    MANIFEST_REPO_OWNER,
 | 
			
		||||
    MANIFEST_REPO_NAME,
 | 
			
		||||
    AUTH
 | 
			
		||||
    AUTH,
 | 
			
		||||
    MANIFEST_REPO_BRANCH
 | 
			
		||||
  );
 | 
			
		||||
  return await tc.findFromManifest(
 | 
			
		||||
    semanticVersionSpec,
 | 
			
		||||
    true,
 | 
			
		||||
    false,
 | 
			
		||||
    manifest,
 | 
			
		||||
    architecture
 | 
			
		||||
  );
 | 
			
		||||
@ -35,7 +38,10 @@ async function installPython(workingDirectory: string) {
 | 
			
		||||
    silent: true,
 | 
			
		||||
    listeners: {
 | 
			
		||||
      stdout: (data: Buffer) => {
 | 
			
		||||
        core.debug(data.toString().trim());
 | 
			
		||||
        core.info(data.toString().trim());
 | 
			
		||||
      },
 | 
			
		||||
      stderr: (data: Buffer) => {
 | 
			
		||||
        core.error(data.toString().trim());
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  };
 | 
			
		||||
@ -52,13 +58,12 @@ export async function installCpythonFromRelease(release: tc.IToolRelease) {
 | 
			
		||||
 | 
			
		||||
  core.info(`Download from "${downloadUrl}"`);
 | 
			
		||||
  const pythonPath = await tc.downloadTool(downloadUrl, undefined, AUTH);
 | 
			
		||||
  const fileName = path.basename(pythonPath, '.zip');
 | 
			
		||||
  core.info('Extract downloaded archive');
 | 
			
		||||
  let pythonExtractedFolder;
 | 
			
		||||
  if (IS_WINDOWS) {
 | 
			
		||||
    pythonExtractedFolder = await tc.extractZip(pythonPath, `./${fileName}`);
 | 
			
		||||
    pythonExtractedFolder = await tc.extractZip(pythonPath);
 | 
			
		||||
  } else {
 | 
			
		||||
    pythonExtractedFolder = await tc.extractTar(pythonPath, `./${fileName}`);
 | 
			
		||||
    pythonExtractedFolder = await tc.extractTar(pythonPath);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  core.info('Execute installation script');
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user