Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for locally hosted GHES instances to reduce rate limiting #720

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion action.yml
Expand Up @@ -15,9 +15,15 @@ inputs:
check-latest:
description: "Set this option if you want the action to check for the latest available version that satisfies the version spec."
default: false
github_api_url:
description: "The url you wish to gather Python distributions from. Useful when running on GHES when you have a local instance of actions/python-versions and actions/setup-python installed"
default: "https://api.github.com"
github_raw_url:
description: "The endpoint you wish to use as the raw url"
default: "https://raw.githubusercontent.com"
token:
description: "The token used to authenticate when fetching Python distributions from https://github.com/actions/python-versions. When running this action on github.com, the default value is sufficient. When running on GHES, you can pass a personal access token for github.com if you are experiencing rate limiting."
default: ${{ github.server_url == 'https://github.com' && github.token || '' }}
default: ${{ github.github_api_url == inputs.github_api_url && github.token || '' }}
cache-dependency-path:
description: "Used to specify the path to dependency files. Supports wildcards or a list of file names for caching multiple dependencies."
update-environment:
Expand Down
14 changes: 9 additions & 5 deletions dist/setup/index.js
Expand Up @@ -10957,10 +10957,10 @@ function findAllVersions(toolName, arch) {
return versions;
}
exports.findAllVersions = findAllVersions;
function getManifestFromRepo(owner, repo, auth, branch = 'master') {
function getManifestFromRepo(owner, repo, auth, branch = 'master', serverUrl = 'https://api.github.com') {
Copy link

@jhaxon jhaxon Oct 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getManifestFromRepo() is a function from a library imported at the top of src/install-python.ts. The code here in dist/* is generated from that library at compile time, so your changes will be overwritten.

For reference, here is the source of the library function. If you truly intend to reuse the library function you'll first need to merge a PR changing its signature here.

In order for this PR to work you need to limit your changes to the src/*.ts files.

return __awaiter(this, void 0, void 0, function* () {
let releases = [];
const treeUrl = `https://api.github.com/repos/${owner}/${repo}/git/trees/${branch}`;
const treeUrl = `${serverUrl}/repos/${owner}/${repo}/git/trees/${branch}`;
const http = new httpm.HttpClient('tool-cache');
const headers = {};
if (auth) {
Expand Down Expand Up @@ -69685,7 +69685,11 @@ const AUTH = !TOKEN ? undefined : `token ${TOKEN}`;
const MANIFEST_REPO_OWNER = 'actions';
const MANIFEST_REPO_NAME = 'python-versions';
const MANIFEST_REPO_BRANCH = 'main';
exports.MANIFEST_URL = `https://raw.githubusercontent.com/${MANIFEST_REPO_OWNER}/${MANIFEST_REPO_NAME}/${MANIFEST_REPO_BRANCH}/versions-manifest.json`;
const API_URL = core.getInput('github_api_url');
const GITHUB_API_URL = API_URL ? 'https://api.github.com' : API_URL;
const RAW_URL = core.getInput('github_raw_url');
const GITHUB_RAW_URL = RAW_URL ? 'https://raw.githubusercontent.com' : RAW_URL;
exports.MANIFEST_URL = `${GITHUB_RAW_URL}/${MANIFEST_REPO_OWNER}/${MANIFEST_REPO_NAME}/${MANIFEST_REPO_BRANCH}/versions-manifest.json`;
function findReleaseFromManifest(semanticVersionSpec, architecture, manifest) {
return __awaiter(this, void 0, void 0, function* () {
if (!manifest) {
Expand All @@ -69697,8 +69701,8 @@ function findReleaseFromManifest(semanticVersionSpec, architecture, manifest) {
}
exports.findReleaseFromManifest = findReleaseFromManifest;
function getManifest() {
core.debug(`Getting manifest from ${MANIFEST_REPO_OWNER}/${MANIFEST_REPO_NAME}@${MANIFEST_REPO_BRANCH}`);
return tc.getManifestFromRepo(MANIFEST_REPO_OWNER, MANIFEST_REPO_NAME, AUTH, MANIFEST_REPO_BRANCH);
core.debug(`Getting manifest from ${GITHUB_API_URL}/${MANIFEST_REPO_OWNER}/${MANIFEST_REPO_NAME}@${MANIFEST_REPO_BRANCH}`);
return tc.getManifestFromRepo(MANIFEST_REPO_OWNER, MANIFEST_REPO_NAME, AUTH, MANIFEST_REPO_BRANCH, GITHUB_API_URL);
}
exports.getManifest = getManifest;
function installPython(workingDirectory) {
Expand Down
10 changes: 10 additions & 0 deletions docs/advanced-usage.md
Expand Up @@ -591,6 +591,16 @@ Requests should now be authenticated. To verify that you are getting the higher
### 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/enterprise-server/admin/github-actions/managing-access-to-actions-from-githubcom/setting-up-the-tool-cache-on-self-hosted-runners-without-internet-access)" for more information.

### Other no access solutions
You can internally host a copy of [`actions/python-versions`](https://github.com/actions/python-versions) and manually point actions/setup-python to your internal endpoint with `github_api_url` and `github_raw_url`
```yml
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.8
github_api_url: api.github.YOUR_COMPANY.com
github_raw_url: raw.github.YOUR_COMPANY.com
```

## Allow pre-releases

Expand Down
11 changes: 8 additions & 3 deletions src/install-python.ts
Expand Up @@ -10,7 +10,11 @@ const AUTH = !TOKEN ? undefined : `token ${TOKEN}`;
const MANIFEST_REPO_OWNER = 'actions';
const MANIFEST_REPO_NAME = 'python-versions';
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 API_URL = core.getInput('github_api_url');
const GITHUB_API_URL = API_URL ? 'https://api.github.com' : API_URL;
Copy link

@jhaxon jhaxon Oct 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

truthy and falsey expressions are backwards here, should be API_URL ? API_URL : ...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unit tests caught this! ;)

const RAW_URL = core.getInput('github_raw_url');
const GITHUB_RAW_URL = RAW_URL ? 'https://raw.githubusercontent.com' : RAW_URL;
Copy link

@jhaxon jhaxon Oct 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

truthy and falsey expressions are backwards here, should be RAW_URL ? RAW_URL : ...

export const MANIFEST_URL = `${GITHUB_RAW_URL}/${MANIFEST_REPO_OWNER}/${MANIFEST_REPO_NAME}/${MANIFEST_REPO_BRANCH}/versions-manifest.json`;

export async function findReleaseFromManifest(
semanticVersionSpec: string,
Expand All @@ -33,13 +37,14 @@ export async function findReleaseFromManifest(

export function getManifest(): Promise<tc.IToolRelease[]> {
core.debug(
`Getting manifest from ${MANIFEST_REPO_OWNER}/${MANIFEST_REPO_NAME}@${MANIFEST_REPO_BRANCH}`
`Getting manifest from ${GITHUB_API_URL}/${MANIFEST_REPO_OWNER}/${MANIFEST_REPO_NAME}@${MANIFEST_REPO_BRANCH}`
);
return tc.getManifestFromRepo(
MANIFEST_REPO_OWNER,
MANIFEST_REPO_NAME,
AUTH,
MANIFEST_REPO_BRANCH
MANIFEST_REPO_BRANCH,
GITHUB_API_URL
);
}

Expand Down