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

feat: set input for github token to avoid rate-limiting #149

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ This repo contains the github actions for installing gh cli in self hosted runne

To install the gh cli, use the actions as below:

```yaml
build:
runs-on: [self-hosted]
steps:
- uses: actions/checkout@v2
- name: Install the gh cli
uses: ksivamuthu/actions-setup-gh-cli@<VERSION>
with:
version: 2.24.3
- run: |
gh version
```
```yaml
build:
runs-on: [self-hosted]
steps:
- uses: actions/checkout@v2
- name: Install the gh cli
uses: ksivamuthu/actions-setup-gh-cli@<VERSION>
with:
token: ${{ secrets.TVSIT_AXON_COM_PUBLIC_PAT }}
version: 2.24.3
- run: |
gh version
```

`token`: The PAT token to authenticate github.io when installing the gh cli, avoiding the rate limit issues.
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ name: "Setup gh cli"
description: "Setup gh cli in self-hosted runners. This action is to setup gh cli in self-hosted runners"
author: "ksivamuthu"
inputs:
version:
token:
description: 'GitHub token to avoid rate-limiting'
required: true
version:
required: false
description: 'gh cli version to download'
archive_format:
Expand Down
44 changes: 41 additions & 3 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

34 changes: 22 additions & 12 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import * as core from '@actions/core'
import * as os from 'os'

import {
cacheFile,
downloadTool,
extractTar,
find,
extractZip
} from '@actions/tool-cache'
import {chmodSync} from 'fs'
import {cacheFile, extractTar, find, extractZip} from '@actions/tool-cache'
import {chmodSync, unlinkSync} from 'fs'
import {HttpClient} from '@actions/http-client'
import {pipeline} from 'node:stream'
import {promisify} from 'node:util'
import {createWriteStream} from 'node:fs'

const GH_CLI_TOOL_NAME = 'gh'
const TOKEN = core.getInput('token')
const AUTH_HTTP = new HttpClient('gh-release', [], {
headers: {
Authorization: `Bearer ${TOKEN}`
}
})

run()

Expand All @@ -30,7 +33,6 @@ async function install(): Promise<void> {
core.info('Installing gh cli in self hosted runner')

const version = core.getInput('version') || (await getLatestVersion())

const platform = core.getInput('platform') || os.platform()
const archive_format = core.getInput('archive_format') || 'tar.gz'
const packageUrl = `https://github.com/cli/cli/releases/download/v${version}/gh_${version}_${platform}_amd64.${archive_format}`
Expand All @@ -40,7 +42,15 @@ async function install(): Promise<void> {
let cliPath = find(GH_CLI_TOOL_NAME, version)

if (!cliPath) {
const downloadPath = await downloadTool(packageUrl, 'gh_tar')
const downloadPath = 'gh_tar' // Temporary file path
const response = await AUTH_HTTP.get(packageUrl)
if (response.message.statusCode !== 200) {
throw new Error(
`Unexpected HTTP response: ${response.message.statusCode}`
)
}
await promisify(pipeline)(response.message, createWriteStream(downloadPath))

chmodSync(downloadPath, '755')
cliPath =
archive_format === 'tar.gz'
Expand All @@ -52,15 +62,15 @@ async function install(): Promise<void> {
GH_CLI_TOOL_NAME,
version
)
unlinkSync(downloadPath)
}

core.addPath(cliPath)
core.info('gh cli installed successfully')
}

async function getLatestVersion(): Promise<string> {
const http = new HttpClient('gh-release')
const response = await http.getJson(
const response = await AUTH_HTTP.getJson(
'https://api.github.com/repos/cli/cli/releases/latest'
)
let latestVersion = (response.result as {tag_name: string}).tag_name
Expand Down
Loading