Skip to content

Commit fbfcfc9

Browse files
Add Token input for custom or unauthenticated API access
Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
1 parent 44bf307 commit fbfcfc9

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,36 @@ jobs:
4444
Prerelease: true
4545
```
4646
47+
### Using a custom token or anonymous API access
48+
49+
```yaml
50+
# Use a custom PAT to avoid rate limits
51+
- name: Install PowerShell (custom token)
52+
uses: PSModule/install-powershell@v1
53+
with:
54+
Token: ${{ secrets.MY_GITHUB_PAT }}
55+
56+
# Use anonymous (unauthenticated) API access
57+
- name: Install PowerShell (anonymous)
58+
uses: PSModule/install-powershell@v1
59+
with:
60+
Token: ''
61+
```
62+
4763
## Inputs
4864
4965
| Input | Required | Default | Description |
5066
| ----- | -------- | ------- | ----------- |
5167
| `Version` | `false` | `latest` | Desired PowerShell Core version (e.g. `7.4.1`, `7.6.0-preview.6`). Use `latest` to install the newest stable release (or newest prerelease when `Prerelease` is `true`). |
5268
| `Prerelease` | `false` | `false` | Install a prerelease version. When `true` and `Version` is `latest`, resolves to the latest prerelease. Similar to `-Prerelease` on `Install-PSResource`. |
69+
| `Token` | `false` | `${{ github.token }}` | GitHub token used for API calls when resolving the `latest` version. Pass a custom PAT to avoid rate limits, or an empty string (`''`) to make fully unauthenticated (anonymous) API calls. |
5370

5471
## Secrets
5572

5673
This action does **not** require any secrets.
74+
By default it authenticates to the GitHub API using the built-in `github.token`.
75+
You can override this by setting the `Token` input to a custom PAT, or to an empty
76+
string (`''`) for fully unauthenticated (anonymous) API access.
5777

5878
## Outputs
5979

action.yml

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ inputs:
2323
Similar to the `-Prerelease` switch on `Install-PSResource`.
2424
required: false
2525
default: 'false'
26+
Token:
27+
description: |
28+
GitHub token used for GitHub API calls (e.g. resolving `latest` version).
29+
Defaults to the built-in `github.token`. Pass a custom PAT to avoid rate
30+
limits, or an empty string (`''`) to make fully unauthenticated API calls.
31+
required: false
32+
default: ${{ github.token }}
2633

2734
runs:
2835
using: composite
@@ -34,22 +41,28 @@ runs:
3441
env:
3542
REQUESTED_VERSION: ${{ inputs.Version }}
3643
PRERELEASE: ${{ inputs.Prerelease }}
37-
GITHUB_TOKEN: ${{ github.token }}
44+
GITHUB_TOKEN: ${{ inputs.Token }}
3845
run:
3946
| # zizmor: ignore[github-env] GITHUB_PATH writes use hardcoded install dirs, not user input
4047
# Install-PowerShell
4148
set -e
4249
echo "Requested version: [$REQUESTED_VERSION]"
4350
echo "Prerelease: [$PRERELEASE]"
4451

52+
# Build auth header conditionally — omit when GITHUB_TOKEN is empty (anonymous access)
53+
AUTH_HEADER=()
54+
if [[ -n "$GITHUB_TOKEN" ]]; then
55+
AUTH_HEADER=(-H "Authorization: Bearer $GITHUB_TOKEN")
56+
fi
57+
4558
# Only resolve to latest version if explicitly set to 'latest' (case-insensitive)
4659
case "${REQUESTED_VERSION:-}" in
4760
[Ll][Aa][Tt][Ee][Ss][Tt])
4861
if [[ "$PRERELEASE" == "true" ]]; then
4962
REQUESTED_VERSION=$(
5063
curl -s -f \
5164
-H "Accept: application/vnd.github+json" \
52-
-H "Authorization: Bearer $GITHUB_TOKEN" \
65+
"${AUTH_HEADER[@]}" \
5366
-H "X-GitHub-Api-Version: 2022-11-28" \
5467
'https://api.github.com/repos/PowerShell/PowerShell/releases?per_page=100' |
5568
jq -r '[.[] | select(.prerelease == true)] | (.[0].tag_name // empty)' | sed 's/^v//'
@@ -63,7 +76,7 @@ runs:
6376
REQUESTED_VERSION=$(
6477
curl -s -f \
6578
-H "Accept: application/vnd.github+json" \
66-
-H "Authorization: Bearer $GITHUB_TOKEN" \
79+
"${AUTH_HEADER[@]}" \
6780
-H "X-GitHub-Api-Version: 2022-11-28" \
6881
https://api.github.com/repos/PowerShell/PowerShell/releases/latest |
6982
jq -r '.tag_name' | sed 's/^v//'
@@ -101,7 +114,7 @@ runs:
101114
RELEASE_JSON=$(
102115
curl -s -f \
103116
-H "Accept: application/vnd.github+json" \
104-
-H "Authorization: Bearer $GITHUB_TOKEN" \
117+
"${AUTH_HEADER[@]}" \
105118
-H "X-GitHub-Api-Version: 2022-11-28" \
106119
"https://api.github.com/repos/PowerShell/PowerShell/releases/tags/v${REQUESTED_VERSION}"
107120
)
@@ -222,22 +235,28 @@ runs:
222235
env:
223236
REQUESTED_VERSION: ${{ inputs.Version }}
224237
PRERELEASE: ${{ inputs.Prerelease }}
225-
GITHUB_TOKEN: ${{ github.token }}
238+
GITHUB_TOKEN: ${{ inputs.Token }}
226239
run:
227240
| # zizmor: ignore[github-env] GITHUB_PATH writes use hardcoded install dirs, not user input
228241
# Install-PowerShell
229242
set -e
230243
echo "Requested version: [$REQUESTED_VERSION]"
231244
echo "Prerelease: [$PRERELEASE]"
232245

246+
# Build auth header conditionally — omit when GITHUB_TOKEN is empty (anonymous access)
247+
AUTH_HEADER=()
248+
if [[ -n "$GITHUB_TOKEN" ]]; then
249+
AUTH_HEADER=(-H "Authorization: Bearer $GITHUB_TOKEN")
250+
fi
251+
233252
# Only resolve to latest version if explicitly set to 'latest' (case-insensitive)
234253
case "${REQUESTED_VERSION:-}" in
235254
[Ll][Aa][Tt][Ee][Ss][Tt])
236255
if [[ "$PRERELEASE" == "true" ]]; then
237256
REQUESTED_VERSION=$(
238257
curl -s -f \
239258
-H "Accept: application/vnd.github+json" \
240-
-H "Authorization: Bearer $GITHUB_TOKEN" \
259+
"${AUTH_HEADER[@]}" \
241260
-H "X-GitHub-Api-Version: 2022-11-28" \
242261
'https://api.github.com/repos/PowerShell/PowerShell/releases?per_page=100' |
243262
jq -r '[.[] | select(.prerelease == true)] | (.[0].tag_name // empty)' | sed 's/^v//'
@@ -251,7 +270,7 @@ runs:
251270
REQUESTED_VERSION=$(
252271
curl -s -f \
253272
-H "Accept: application/vnd.github+json" \
254-
-H "Authorization: Bearer $GITHUB_TOKEN" \
273+
"${AUTH_HEADER[@]}" \
255274
-H "X-GitHub-Api-Version: 2022-11-28" \
256275
https://api.github.com/repos/PowerShell/PowerShell/releases/latest |
257276
jq -r '.tag_name' | sed 's/^v//'
@@ -321,7 +340,7 @@ runs:
321340
env:
322341
REQUESTED_VERSION: ${{ inputs.Version }}
323342
PRERELEASE: ${{ inputs.Prerelease }}
324-
GITHUB_TOKEN: ${{ github.token }}
343+
GITHUB_TOKEN: ${{ inputs.Token }}
325344
run:
326345
| # zizmor: ignore[github-env] GITHUB_PATH writes use hardcoded install dirs, not user input
327346
# Install-PowerShell
@@ -333,9 +352,11 @@ runs:
333352
if ($req -and $req.Trim().ToLower() -eq 'latest') {
334353
$headers = @{
335354
'Accept' = 'application/vnd.github+json'
336-
'Authorization' = "Bearer $($env:GITHUB_TOKEN)"
337355
'X-GitHub-Api-Version' = '2022-11-28'
338356
}
357+
if ($env:GITHUB_TOKEN) {
358+
$headers['Authorization'] = "Bearer $($env:GITHUB_TOKEN)"
359+
}
339360
if ($env:PRERELEASE -eq 'true') {
340361
$releases = Invoke-RestMethod -Uri 'https://api.github.com/repos/PowerShell/PowerShell/releases?per_page=100' -Headers $headers
341362
$latestRelease = $releases | Where-Object { $_.prerelease -eq $true } | Select-Object -First 1

0 commit comments

Comments
 (0)