Skip to content

Commit

Permalink
Merge branch 'main' into v-dmshib/install-multiple-python-versions
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-shibanov committed Dec 4, 2022
2 parents fa589f7 + 1aafadc commit 69d9d7d
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 95 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release-new-action-version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Update the ${{ env.TAG_NAME }} tag
uses: actions/[email protected].0
uses: actions/[email protected].1
with:
source-tag: ${{ env.TAG_NAME }}
slack-webhook: ${{ secrets.SLACK_WEBHOOK }}
4 changes: 2 additions & 2 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ${{ matrix.operating-system }}
strategy:
matrix:
operating-system: [ubuntu-latest, windows-latest]
operating-system: [ubuntu-20.04, windows-latest]
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down Expand Up @@ -68,7 +68,7 @@ jobs:
python-version: 3.8
- name: Verify 3.8
run: python __tests__/verify-python.py 3.8

- name: Run with setup-python 3.7.5
uses: ./
with:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ steps:
python-version: 'pypy3.9'
- run: python my_script.py
```
The `python-version` input is optional. If not supplied, the action will try to resolve the version from the default `.python-version` file. If the `.python-version` file doesn't exist Python or PyPy version from the PATH will be used. The default version of Python or PyPy in PATH varies between runners and can be changed unexpectedly so we recommend always using `setup-python`.
The `python-version` input is optional. If not supplied, the action will try to resolve the version from the default `.python-version` file. If the `.python-version` file doesn't exist Python or PyPy version from the PATH will be used. The default version of Python or PyPy in PATH varies between runners and can be changed unexpectedly so we recommend always setting Python version explicitly using the `python-version` or `python-version-file` inputs.

The action will first check the local [tool cache](docs/advanced-usage.md#hosted-tool-cache) for a [semver](https://github.com/npm/node-semver#versions) match. If unable to find a specific version in the tool cache, the action will attempt to download a version of Python from [GitHub Releases](https://github.com/actions/python-versions/releases) and for PyPy from the official [PyPy's dist](https://downloads.python.org/pypy/).

Expand Down
117 changes: 80 additions & 37 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66511,27 +66511,45 @@ function installPyPy(pypyVersion, pythonVersion, architecture, releases) {
const { foundAsset, resolvedPythonVersion, resolvedPyPyVersion } = releaseData;
let downloadUrl = `${foundAsset.download_url}`;
core.info(`Downloading PyPy from "${downloadUrl}" ...`);
const pypyPath = yield tc.downloadTool(downloadUrl);
core.info('Extracting downloaded archive...');
if (utils_1.IS_WINDOWS) {
downloadDir = yield tc.extractZip(pypyPath);
try {
const pypyPath = yield tc.downloadTool(downloadUrl);
core.info('Extracting downloaded archive...');
if (utils_1.IS_WINDOWS) {
downloadDir = yield tc.extractZip(pypyPath);
}
else {
downloadDir = yield tc.extractTar(pypyPath, undefined, 'x');
}
// root folder in archive can have unpredictable name so just take the first folder
// downloadDir is unique folder under TEMP and can't contain any other folders
const archiveName = fs_1.default.readdirSync(downloadDir)[0];
const toolDir = path.join(downloadDir, archiveName);
let installDir = toolDir;
if (!utils_1.isNightlyKeyword(resolvedPyPyVersion)) {
installDir = yield tc.cacheDir(toolDir, 'PyPy', resolvedPythonVersion, architecture);
}
utils_1.writeExactPyPyVersionFile(installDir, resolvedPyPyVersion);
const binaryPath = getPyPyBinaryPath(installDir);
yield createPyPySymlink(binaryPath, resolvedPythonVersion);
yield installPip(binaryPath);
return { installDir, resolvedPythonVersion, resolvedPyPyVersion };
}
catch (err) {
if (err instanceof Error) {
// Rate limit?
if (err instanceof tc.HTTPError &&
(err.httpStatusCode === 403 || err.httpStatusCode === 429)) {
core.info(`Received HTTP status code ${err.httpStatusCode}. This usually indicates the rate limit has been exceeded`);
}
else {
core.info(err.message);
}
if (err.stack !== undefined) {
core.debug(err.stack);
}
}
throw err;
}
else {
downloadDir = yield tc.extractTar(pypyPath, undefined, 'x');
}
// root folder in archive can have unpredictable name so just take the first folder
// downloadDir is unique folder under TEMP and can't contain any other folders
const archiveName = fs_1.default.readdirSync(downloadDir)[0];
const toolDir = path.join(downloadDir, archiveName);
let installDir = toolDir;
if (!utils_1.isNightlyKeyword(resolvedPyPyVersion)) {
installDir = yield tc.cacheDir(toolDir, 'PyPy', resolvedPythonVersion, architecture);
}
utils_1.writeExactPyPyVersionFile(installDir, resolvedPyPyVersion);
const binaryPath = getPyPyBinaryPath(installDir);
yield createPyPySymlink(binaryPath, resolvedPythonVersion);
yield installPip(binaryPath);
return { installDir, resolvedPythonVersion, resolvedPyPyVersion };
});
}
exports.installPyPy = installPyPy;
Expand Down Expand Up @@ -66577,7 +66595,7 @@ function findRelease(releases, pythonVersion, pypyVersion, architecture) {
semver.satisfies(pypyVersionToSemantic(item.pypy_version), pypyVersion);
const isArchPresent = item.files &&
(utils_1.IS_WINDOWS
? isArchPresentForWindows(item)
? isArchPresentForWindows(item, architecture)
: isArchPresentForMacOrLinux(item, architecture, process.platform));
return isPythonVersionSatisfied && isPyPyVersionSatisfied && isArchPresent;
});
Expand All @@ -66590,7 +66608,7 @@ function findRelease(releases, pythonVersion, pypyVersion, architecture) {
});
const foundRelease = sortedReleases[0];
const foundAsset = utils_1.IS_WINDOWS
? findAssetForWindows(foundRelease)
? findAssetForWindows(foundRelease, architecture)
: findAssetForMacOrLinux(foundRelease, architecture, process.platform);
return {
foundAsset,
Expand All @@ -66613,24 +66631,31 @@ function pypyVersionToSemantic(versionSpec) {
return versionSpec.replace(prereleaseVersion, '$1-$2.$3');
}
exports.pypyVersionToSemantic = pypyVersionToSemantic;
function isArchPresentForWindows(item) {
return item.files.some((file) => utils_1.WINDOWS_ARCHS.includes(file.arch) &&
utils_1.WINDOWS_PLATFORMS.includes(file.platform));
function isArchPresentForWindows(item, architecture) {
architecture = replaceX32toX86(architecture);
return item.files.some((file) => utils_1.WINDOWS_PLATFORMS.includes(file.platform) && file.arch === architecture);
}
exports.isArchPresentForWindows = isArchPresentForWindows;
function isArchPresentForMacOrLinux(item, architecture, platform) {
return item.files.some((file) => file.arch === architecture && file.platform === platform);
}
exports.isArchPresentForMacOrLinux = isArchPresentForMacOrLinux;
function findAssetForWindows(releases) {
return releases.files.find((item) => utils_1.WINDOWS_ARCHS.includes(item.arch) &&
utils_1.WINDOWS_PLATFORMS.includes(item.platform));
function findAssetForWindows(releases, architecture) {
architecture = replaceX32toX86(architecture);
return releases.files.find((item) => utils_1.WINDOWS_PLATFORMS.includes(item.platform) && item.arch === architecture);
}
exports.findAssetForWindows = findAssetForWindows;
function findAssetForMacOrLinux(releases, architecture, platform) {
return releases.files.find((item) => item.arch === architecture && item.platform === platform);
}
exports.findAssetForMacOrLinux = findAssetForMacOrLinux;
function replaceX32toX86(architecture) {
// convert x32 to x86 because os.arch() returns x32 for 32-bit systems but PyPy releases json has x86 arch value.
if (architecture === 'x32') {
architecture = 'x86';
}
return architecture;
}


/***/ }),
Expand Down Expand Up @@ -66723,17 +66748,35 @@ function installCpythonFromRelease(release) {
return __awaiter(this, void 0, void 0, function* () {
const downloadUrl = release.files[0].download_url;
core.info(`Download from "${downloadUrl}"`);
const pythonPath = yield tc.downloadTool(downloadUrl, undefined, AUTH);
core.info('Extract downloaded archive');
let pythonExtractedFolder;
if (utils_1.IS_WINDOWS) {
pythonExtractedFolder = yield tc.extractZip(pythonPath);
let pythonPath = '';
try {
pythonPath = yield tc.downloadTool(downloadUrl, undefined, AUTH);
core.info('Extract downloaded archive');
let pythonExtractedFolder;
if (utils_1.IS_WINDOWS) {
pythonExtractedFolder = yield tc.extractZip(pythonPath);
}
else {
pythonExtractedFolder = yield tc.extractTar(pythonPath);
}
core.info('Execute installation script');
yield installPython(pythonExtractedFolder);
}
else {
pythonExtractedFolder = yield tc.extractTar(pythonPath);
catch (err) {
if (err instanceof tc.HTTPError) {
// Rate limit?
if (err.httpStatusCode === 403 || err.httpStatusCode === 429) {
core.info(`Received HTTP status code ${err.httpStatusCode}. This usually indicates the rate limit has been exceeded`);
}
else {
core.info(err.message);
}
if (err.stack) {
core.debug(err.stack);
}
}
throw err;
}
core.info('Execute installation script');
yield installPython(pythonExtractedFolder);
});
}
exports.installCpythonFromRelease = installCpythonFromRelease;
Expand Down
40 changes: 34 additions & 6 deletions docs/advanced-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,20 @@ steps:
- run: pip install -e . -r subdirectory/requirements-dev.txt
```

**Caching projects that use setup.py:**

```yaml
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.11'
cache: 'pip'
cache-dependency-path: setup.py
- run: pip install -e .
# Or pip install -e '.[test]' to install test dependencies
```

# Outputs and environment variables

## Outputs
Expand Down Expand Up @@ -471,15 +485,29 @@ One quick way to grant access is to change the user and group of `/Users/runner/

## Using `setup-python` on GHES

`setup-python` comes pre-installed on the appliance with GHES if Actions is enabled. When dynamically downloading Python distributions, `setup-python` downloads distributions from [`actions/python-versions`](https://github.com/actions/python-versions) on github.com (outside of the appliance). These calls to `actions/python-versions` are made via unauthenticated requests, which are limited to [60 requests per hour per IP](https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting). If more requests are made within the time frame, then you will start to see rate-limit errors during downloading that looks like: `##[error]API rate limit exceeded for...`.
### Avoiding rate limit issues

`setup-python` comes pre-installed on the appliance with GHES if Actions is enabled. When dynamically downloading Python distributions, `setup-python` downloads distributions from [`actions/python-versions`](https://github.com/actions/python-versions) on github.com (outside of the appliance). These calls to `actions/python-versions` are by default made via unauthenticated requests, which are limited to [60 requests per hour per IP](https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting). If more requests are made within the time frame, then you will start to see rate-limit errors during downloading that look like this:

##[error]API rate limit exceeded for YOUR_IP. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)

To get a higher rate limit, you can [generate a personal access token (PAT) on github.com](https://github.com/settings/tokens/new) and pass it as the `token` input for the action. It is important to understand that this needs to be a token from github.com and _not_ from your GHES instance. If you or your colleagues do not yet have a github.com account, you might need to create one.

To get a higher rate limit, you can [generate a personal access token on github.com](https://github.com/settings/tokens/new) and pass it as the `token` input for the action:
Here are the steps you need to follow to avoid the rate limit:

1. Create a PAT on any github.com account by using [this link](https://github.com/settings/tokens/new) after logging into github.com (not your Enterprise instance). This PAT does _not_ need any rights, so make sure all the boxes are unchecked.
2. Store this PAT in the repository / organization where you run your workflow, e.g. as `GH_GITHUB_COM_TOKEN`. You can do this by navigating to your repository -> **Settings** -> **Secrets** -> **Actions** -> **New repository secret**.
3. To use this functionality, you need to use any version newer than `v4.3`. Also, change _python-version_ as needed.

```yml
uses: actions/setup-python@v4
with:
token: ${{ secrets.GH_DOTCOM_TOKEN }}
python-version: 3.11
- name: Set up Python
uses: actions/setup-python@4
with:
python-version: 3.8
token: ${{ secrets.GH_GITHUB_COM_TOKEN }}
```

Requests should now be authenticated. To verify that you are getting the higher rate limit, you can call GitHub's [rate limit API](https://docs.github.com/en/rest/rate-limit) from within your workflow ([example](https://github.com/actions/setup-python/pull/443#issuecomment-1206776401)).

### No access to github.com
If the runner is not able to access github.com, any Python versions requested during a workflow run must come from the runner's tool cache. See "[Setting up the tool cache on self-hosted runners without internet access](https://docs.github.com/en/[email protected]/admin/github-actions/managing-access-to-actions-from-githubcom/setting-up-the-tool-cache-on-self-hosted-runners-without-internet-access)" for more information.
Loading

0 comments on commit 69d9d7d

Please sign in to comment.