21 Commits
Author SHA1 Message Date
slawek cbfadf3488 Fix: Enhance binary pattern matching with system and architecture replacements
Test Action / test (push) Successful in 14s
2026-04-06 23:33:46 +02:00
slawek 01d844f6d9 Feat: Integrate commander for improved CLI argument parsing 2026-04-06 23:21:03 +02:00
slawek e50ae03336 Fix: Add chmod command to make CLI and check-token scripts executable 2026-04-06 22:55:40 +02:00
slawek 1896949b27 Removed cli.js from the repository - it is not needed there. 2026-04-06 22:53:21 +02:00
slawek d9d7f67247 Refactored asset matching code. 2026-04-06 22:50:25 +02:00
slawek 0132ee6665 Refactor: Simplify getMatchingAsset function parameters in CLI and matcher modules 2026-04-06 18:46:31 +02:00
slawek 89ea0cecb0 Refactor: Consolidate asset matching logic into a single function 2026-04-06 12:30:28 +02:00
slawek 3a75adedc6 Refactor: Extract filename matching logic into a separate function 2026-04-06 12:18:19 +02:00
slawek dfa641afd4 Matched functionality of the CLI with the Bash predecessor. 2026-04-06 12:00:09 +02:00
slawek 483a1c5f13 Fix: README corrections. 2026-01-11 20:03:25 +01:00
slawek 90033e0b9e A dummy change to trigger the workflow. 2026-01-11 19:56:45 +01:00
slawek f8a559538e Add default token to check-token subaction 2026-01-11 19:54:25 +01:00
slawek 09f36edc01 Updated GitHub workflow with new Check Token action. 2026-01-11 19:46:28 +01:00
slawek 55d6019d0f Fix: call correct action.
Test Action / test (push) Successful in 4s
2026-01-11 19:31:53 +01:00
slawek 11a26bd176 Added check-token-action build and action.
Test Action / test (push) Failing after 4s
2026-01-11 19:25:14 +01:00
slawek 32a4011b54 Added Check Token subaction.
Test Action / test (push) Successful in 4s
2026-01-11 19:24:23 +01:00
slawek 2bb60fc0ed Added an utility that validates GitHub token.
Test Action / test (push) Successful in 3s
2026-01-11 14:27:32 +01:00
slawek fc727877e6 Fix: Minor formatting issue. 2026-01-11 14:27:08 +01:00
slawek 120b16b56e Update: Eliminated minor formatting issues.
Test Action / test (push) Successful in 3s
2026-01-11 14:03:36 +01:00
slawek 4ee62009bb Fix: Windows decompression and installation paths. 2026-01-11 14:03:10 +01:00
slawek b20a066030 Added test workflow for Github. 2026-01-11 11:38:24 +01:00
21 changed files with 42673 additions and 297 deletions
+5
View File
@@ -18,6 +18,11 @@ jobs:
- name: Checkout - name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Check Authentication
uses: ./check-token
with:
token: ${{ secrets.GH_TOKEN }}
- name: Go ACME Setup - name: Go ACME Setup
uses: ./ uses: ./
with: with:
+49
View File
@@ -0,0 +1,49 @@
name: Test Action
on:
push:
branches:
- main
paths:
- '.github/workflows/test.yml'
- 'action.yml'
- 'src/**'
- 'dist/**'
- 'package.json'
- 'package-lock.json'
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
# Let's test the token first.
- name: Check Authentication
uses: skoszewski/setup-github-release/check-token@v1
- name: Go ACME Setup
uses: skoszewski/setup-github-release@v1
with:
repository: 'go-acme/lego'
- name: Setup Hugo
uses: skoszewski/setup-github-release@v1
with:
repository: 'gohugoio/hugo'
file-name: '~hugo_extended_[^a-z]'
- name: Setup RClone
uses: skoszewski/setup-github-release@v1
with:
repository: 'rclone/rclone'
- name: Verify Installation
run: |
echo "Verifying installed tools..."
printf "\nGo ACME Lego:\n"
lego -v
printf "\nHugo:\n"
hugo version
printf "\nRClone:\n"
rclone version
+1
View File
@@ -4,3 +4,4 @@ lib/
*.map *.map
.DS_Store .DS_Store
.github/*.md .github/*.md
dist/cli.js
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2026 Slawomir Koszewski
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+72 -18
View File
@@ -10,7 +10,7 @@ Add the action to your workflow. Authenticate with `github.token` (default) or a
```yaml ```yaml
- name: Install Tool - name: Install Tool
uses: koszewscy/setup-github-release@v1 uses: skoszewski/setup-github-release@v1
with: with:
repository: 'owner/repo' repository: 'owner/repo'
``` ```
@@ -24,7 +24,7 @@ Install the CLI tool on any destination system with Node.js 24 or newer.
1. Clone the repository: 1. Clone the repository:
```bash ```bash
git clone https://github.com/koszewscy/setup-github-release git clone https://github.com/skoszewski/setup-github-release
cd setup-github-release cd setup-github-release
``` ```
@@ -41,7 +41,11 @@ npm run build
npm install -g . npm install -g .
``` ```
After installation, the tool will be available as `install-github-release`: After installation, the tool will be available as `install-github-release`. By default, it installs binaries to:
- Linux/macOS (root): `/usr/local/bin`
- Linux/macOS (user): `~/bin` (if exists) or `/usr/local/bin`
- Windows: `%LOCALAPPDATA%\bin`
```bash ```bash
install-github-release rclone/rclone install-github-release rclone/rclone
@@ -61,8 +65,8 @@ install-github-release rclone/rclone
The action will automatically detect your OS (Linux, Windows, macOS) and architecture (x64, ARM64) and look for a matching archive. It will search for a binary named after the repository. The action will automatically detect your OS (Linux, Windows, macOS) and architecture (x64, ARM64) and look for a matching archive. It will search for a binary named after the repository.
```yaml ```yaml
- name: Install Hugo - name: Install LEGO
uses: koszewscy/setup-github-release@v1 uses: skoszewski/setup-github-release@v1
with: with:
repository: 'go-acme/lego' repository: 'go-acme/lego'
``` ```
@@ -75,7 +79,7 @@ For projects with multiple binary versions, you can use a regex pattern (prefixe
```yaml ```yaml
- name: Install Extended Hugo - name: Install Extended Hugo
uses: koszewscy/setup-github-release@v1 uses: skoszewski/setup-github-release@v1
with: with:
repository: 'gohugoio/hugo' repository: 'gohugoio/hugo'
file-name: '~hugo_extended_[^a-z]' # Regex to match extended version file-name: '~hugo_extended_[^a-z]' # Regex to match extended version
@@ -87,7 +91,7 @@ If the binary name is different from the repository name, like in the example of
```yaml ```yaml
- name: Install GitHub CLI - name: Install GitHub CLI
uses: koszewscy/setup-github-release@v1 uses: skoszewski/setup-github-release@v1
with: with:
repository: 'cli/cli' repository: 'cli/cli'
binary-name: 'gh' # Searches for 'gh' (or 'gh.exe') inside the extracted release binary-name: 'gh' # Searches for 'gh' (or 'gh.exe') inside the extracted release
@@ -98,7 +102,7 @@ If the binary name is different from the repository name, like in the example of
If you are unsure how the binary is named, use the `debug` flag to list all files in the unpacked asset, or download the asset manually to inspect its structure. If you are unsure how the binary is named, use the `debug` flag to list all files in the unpacked asset, or download the asset manually to inspect its structure.
```yaml ```yaml
- uses: koszewscy/setup-github-release@v1 - uses: skoszewski/setup-github-release@v1
with: with:
repository: 'owner/repo' repository: 'owner/repo'
debug: true debug: true
@@ -111,10 +115,18 @@ The following inputs are available for the GitHub Action, and as options for the
- `repository` (required): The GitHub repository in the format `owner/repo` from which to download the release. - `repository` (required): The GitHub repository in the format `owner/repo` from which to download the release.
- `file-name` (optional): The name or the regex pattern (prefixed with `~`) of the asset file to download from the release. - `file-name` (optional): The name or the regex pattern (prefixed with `~`) of the asset file to download from the release.
- `binary-name` (optional): The name or regex pattern (prefixed with `~`) of the binary to search for within the downloaded asset. Defaults to the repository name. - `binary-name` (optional): The name or regex pattern (prefixed with `~`) of the binary to search for within the downloaded asset. Defaults to the repository name.
- `file-type` (optional, default: 'archive'): The regex pattern to identify the type of the file to be downloaded. There are two predefined keywords: - `file-type` (optional): Asset type selector.
- 'archive': matches common archive file extensions like .zip, .tar.gz, .tar, .tgz, .7z.
- 'package': matches common package file extensions like .deb, .rpm, .pkg. - `archive`: matches `.zip`, `.tar.gz`, `.tgz`.
- or a custom regex pattern can be provided to match specific file types. - `package`: matches `.deb`, `.pkg`, `.rpm`.
- short forms: `zip`, `gzip`, `gz`, `tar`, `tar.gz`, `tgz`, `deb`, `pkg`, `rpm`.
If not provided, selection defaults to OS-aware combined package/archive patterns:
- Linux: `.deb`, `.rpm`, `.zip`, `.tar.gz`, `.tgz`
- macOS: `.pkg`, `.zip`, `.tar.gz`, `.tgz`
- other: `.zip`, `.tar.gz`, `.tgz`
- `install-path` (optional, CLI only): Custom installation directory for the CLI tool. - `install-path` (optional, CLI only): Custom installation directory for the CLI tool.
- `update-cache` (optional, default: 'false', Action only): When set to 'false', the action will use the cached version of the tool if it is already available. If set to 'true', the action will check the latest release and update the cache if a newer version is found. If set to 'always', it will always download and install, updating the cache regardless. - `update-cache` (optional, default: 'false', Action only): When set to 'false', the action will use the cached version of the tool if it is already available. If set to 'true', the action will check the latest release and update the cache if a newer version is found. If set to 'always', it will always download and install, updating the cache regardless.
- `debug` (optional, default: 'false'): When set to `true`, the action will log the contents of the unpacked directory to the console. - `debug` (optional, default: 'false'): When set to `true`, the action will log the contents of the unpacked directory to the console.
@@ -133,32 +145,72 @@ Arguments:
repository The GitHub repository (owner/repo) repository The GitHub repository (owner/repo)
Options: Options:
--dry-run [level] Run in test mode (default level: 1)
Or set TEST_MODE environment variable to a value > 0
-l, --list [repository] List available assets from latest release and exit
-a, --app-name <name> Application name (optional, for output messages)
-f, --file-name <name> Asset file name or regex pattern (prefixed with ~) -f, --file-name <name> Asset file name or regex pattern (prefixed with ~)
-b, --binary-name <name> Binary to search for (prefixed with ~ for regex) -b, --binary-name <name> Binary name (supports source:destination form)
-t, --file-type <type> 'archive', 'package', or custom regex (default: archive) -t, --file-type <type> archive|package|zip|gzip|gz|tar|tar.gz|tgz|deb|pkg|rpm
-p, --install-path <path> Custom installation directory -p, --install-path <path> Custom installation directory
-o, --output-directory <path>
Only download selected asset to the specified directory
-j, --releases-json Download latest release JSON only
--system <name> Override detected system for asset matching
--arch <name> Override detected architecture for asset matching
-k, --token <token> GitHub token -k, --token <token> GitHub token
-d, --debug Enable debug logging -d, --debug Enable debug logging
-h, --help Show this help message -h, --help Show this help message
``` ```
## GitHub Token Verification
The project includes a utility to verify the validity of your GitHub token.
### CLI Utility
```bash
check-github-token <token>
```
If no token is provided as an argument, it will attempt to read from the `GITHUB_TOKEN` environment variable.
### GitHub Action
You can also use the `check-token` subaction in your workflows:
```yaml
- name: Verify Token
uses: skoszewski/setup-github-release/check-token@v1
with:
repository: 'actions/checkout' # Optional, defaults to actions/checkout
token: ${{ secrets.MY_TOKEN }}
```
If the `token` input is not provided, it will read from the `GITHUB_TOKEN` environment variable.
## Asset Selection Procedure ## Asset Selection Procedure
The list of assets from the latest release is filtered based on the following rules: The list of assets from the latest release is filtered based on the following rules:
1. If neither `file-name` nor `file-type` is provided, the tool defaults to selecting assets that match the following regular expression: `{{SYSTEM}}[_-]{{ARCH}}.*{{EXT_PATTERN}}$`, where: 1. If neither `file-name` nor `file-type` is provided, the tool defaults to selecting assets with an OS-aware extension pattern and this regular expression shape: `{{SYSTEM}}[_-]{{ARCH}}.*{{EXT_PATTERN}}`, where:
- `{{SYSTEM}}` is replaced with the detected operating system regex. - `{{SYSTEM}}` is replaced with the detected operating system regex.
- `{{ARCH}}` is replaced with the detected architecture regex. - `{{ARCH}}` is replaced with the detected architecture regex.
- `{{EXT_PATTERN}}` is a regex pattern defined by the `file-type` input (defaulting to 'archive' if not specified). - `{{EXT_PATTERN}}` is selected by OS:
Linux: `\.(deb|rpm|zip|tar\.gz|tgz)$`
macOS: `\.(pkg|zip|tar\.gz|tgz)$`
other: `\.(zip|tar\.gz|tgz)$`
2. If `file-name` is provided literally, the tool uses it directly to match the asset name by using exact string comparison. 2. If `file-name` is provided literally, the tool uses it directly to match the asset name by using exact string comparison.
3. If `file-name` is provided as a regex pattern (prefixed with `~`), then: 3. If `file-name` is provided as a regex pattern (prefixed with `~`), then:
- If the pattern does not end with `$` and does not include any placeholders, the tool appends `.*{{SYSTEM}}[_-]{{ARCH}}.*{{EXT_PATTERN}}$` to the provided pattern. - If the pattern does not end with `$` and does not include any placeholders, the tool appends `.*{{SYSTEM}}[_-]{{ARCH}}.*{{EXT_PATTERN}}$` to the provided pattern.
- If it already ends with `$` or includes all three placeholders, the tool uses it as-is to match the asset name using regex. - If it already ends with `$` or includes all three placeholders, the tool uses it as-is to match the asset name using regex.
- If only `{{SYSTEM}}` and `{{ARCH}}` placeholders are included, the tool appends `.*{{EXT_PATTERN}}$`. - If only `{{SYSTEM}}` and `{{ARCH}}` placeholders are included, the tool appends `.*{{EXT_PATTERN}}$`.
4. If `file-type` is not equal to 'archive' or 'package', it is treated as a custom regex pattern to match the file extension. 4. If `file-type` is provided, supported values are: `archive`, `package`, `zip`, `gzip`, `gz`, `tar`, `tar.gz`, `tgz`, `deb`, `pkg`, `rpm`.
5. The tool applies the constructed regex pattern to filter the assets from the latest release. 5. The tool applies the constructed regex pattern to filter the assets from the latest release.
@@ -167,11 +219,13 @@ The list of assets from the latest release is filtered based on the following ru
7. After download and extraction, the tool recursively searches for the binary specified by `binary-name` (or the repository name). If found, the directory containing the binary is used as the tool directory and added to the PATH (or used for installation). If the binary is not found, the tool fails. 7. After download and extraction, the tool recursively searches for the binary specified by `binary-name` (or the repository name). If found, the directory containing the binary is used as the tool directory and added to the PATH (or used for installation). If the binary is not found, the tool fails.
8. `{{SYSTEM}}` is replaced with the detected operating system regex: 8. `{{SYSTEM}}` is replaced with the detected operating system regex:
- For Linux: `linux`. - For Linux: `linux`.
- For MacOS: `(darwin|macos|mac|osx)`. - For MacOS: `(darwin|macos|mac)`.
- For Windows: `(windows|win)`. - For Windows: `(windows|win)`.
9. `{{ARCH}}` is replaced with the detected architecture regex: 9. `{{ARCH}}` is replaced with the detected architecture regex:
- For x64: `(x86_64|x64|amd64)`. - For x64: `(x86_64|x64|amd64)`.
- For arm64: `(aarch64|arm64)`. - For arm64: `(aarch64|arm64)`.
+1 -2
View File
@@ -12,9 +12,8 @@ inputs:
description: 'The name or regex pattern (prefixed with ~) of the binary to search for within the asset. Defaults to the repository name.' description: 'The name or regex pattern (prefixed with ~) of the binary to search for within the asset. Defaults to the repository name.'
required: false required: false
file-type: file-type:
description: 'The type of the file to be downloaded (archive, package, or custom regex).' description: 'Asset type selector: archive, package, zip, gzip, gz, tar, tar.gz, tgz, deb, pkg, rpm.'
required: false required: false
default: 'archive'
update-cache: update-cache:
description: 'How to handle the tool cache (false, true, or always). Defaults to false.' description: 'How to handle the tool cache (false, true, or always). Defaults to false.'
required: false required: false
+15
View File
@@ -0,0 +1,15 @@
name: 'check-token'
description: 'Verify the validity of a GitHub token'
author: 'Slawomir Koszewski with GitHub Copilot assistance'
inputs:
repository:
description: 'The GitHub repository to check (e.g., owner/repo)'
required: false
default: 'actions/checkout'
token:
description: 'The GitHub token to verify'
required: false
default: ${{ github.token }}
runs:
using: 'node24'
main: '../dist/check-token-action.js'
+19860
View File
File diff suppressed because one or more lines are too long
Vendored Executable
+49
View File
@@ -0,0 +1,49 @@
#!/usr/bin/env node
"use strict";
// src/check-token.ts
var import_util = require("util");
// src/core/downloader.ts
function getGithubApiHeaders(token) {
const headers = {
"Accept": "application/vnd.github.v3+json",
"User-Agent": "setup-github-release-action"
};
if (token) {
headers["Authorization"] = `token ${token}`;
}
return headers;
}
async function fetchLatestRelease(repository, token) {
const url = `https://api.github.com/repos/${repository}/releases/latest`;
const headers = getGithubApiHeaders(token);
const response = await fetch(url, { headers });
if (!response.ok) {
const errorBody = await response.text();
throw new Error(`Failed to fetch latest release for ${repository}: ${response.statusText}. ${errorBody}`);
}
return await response.json();
}
// src/check-token.ts
async function run() {
const { positionals } = (0, import_util.parseArgs)({
allowPositionals: true
});
const token = positionals[0] || process.env.GITHUB_TOKEN;
if (!token) {
console.error("Error: No GitHub token provided as an argument or found in GITHUB_TOKEN environment variable.");
process.exit(1);
}
try {
console.log("Verifying GitHub token...");
await fetchLatestRelease("actions/checkout", token);
console.log("\x1B[32mSuccess: The provided GitHub token is valid and has sufficient permissions to access public repositories.\x1B[0m");
} catch (error) {
console.error("\x1B[31mError: GitHub token verification failed.\x1B[0m");
console.error(`Reason: ${error.message}`);
process.exit(1);
}
}
run();
Vendored
-16
View File
@@ -1,16 +0,0 @@
#!/usr/bin/env node
"use strict";var F=Object.create;var N=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var O=Object.getOwnPropertyNames;var U=Object.getPrototypeOf,X=Object.prototype.hasOwnProperty;var G=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of O(e))!X.call(t,s)&&s!==n&&N(t,s,{get:()=>e[s],enumerable:!(r=H(e,s))||r.enumerable});return t};var f=(t,e,n)=>(n=t!=null?F(U(t)):{},G(e||!t||!t.__esModule?N(n,"default",{value:t,enumerable:!0}):n,t));var B=require("util"),p=f(require("path")),c=f(require("fs")),P=f(require("os"));var S=f(require("os")),Y={linux:"linux",darwin:"(darwin|macos|mac|osx)",win32:"(windows|win)"},q={x64:"(x86_64|x64|amd64)",arm64:"(aarch64|arm64)"};function _(){let t=S.platform(),e=S.arch();return{system:t,arch:e,systemPattern:Y[t]||t,archPattern:q[e]||e}}function C(t,e,n){let{fileName:r,fileType:s="archive"}=n,o;if(s==="archive"?o="\\.(zip|tar\\.gz|tar|tgz|7z)":s==="package"?o="\\.(deb|rpm|pkg)":o=s,r)if(r.startsWith("~")){let i=r.substring(1),l=i.includes("{{SYSTEM}}"),a=i.includes("{{ARCH}}"),h=i.includes("{{EXT_PATTERN}}"),$=i.endsWith("$");!l&&!a&&!h&&!$?i+=".*{{SYSTEM}}[_-]{{ARCH}}.*{{EXT_PATTERN}}$":l&&a&&!h&&!$&&(i+=".*{{EXT_PATTERN}}$");let m=i.replace(/{{SYSTEM}}/g,e.systemPattern).replace(/{{ARCH}}/g,e.archPattern).replace(/{{EXT_PATTERN}}/g,o),x=new RegExp(m,"i"),g=t.filter(w=>x.test(w.name));if(g.length===0)throw new Error(`No assets matched the regex: ${m}`);if(g.length>1)throw new Error(`Multiple assets matched the criteria: ${g.map(w=>w.name).join(", ")}`);return g[0]}else{let i=t.find(l=>l.name===r);if(!i)throw new Error(`No asset found matching the exact name: ${r}`);return i}else{let i=`${e.systemPattern}[_-]${e.archPattern}.*${o}$`,l=new RegExp(i,"i"),a=t.filter(h=>l.test(h.name));if(a.length===0)throw new Error(`No assets matched the default criteria: ${i}`);if(a.length>1)throw new Error(`Multiple assets matched the default criteria: ${a.map(h=>h.name).join(", ")}`);return a[0]}}var b=f(require("fs")),M=f(require("path"));function k(t,e,n,r){let s=b.readdirSync(t);n&&(r(`Searching for binary in ${t}...`),s.forEach(o=>r(` - ${o}`)));for(let o of s){let i=M.join(t,o);if(b.statSync(i).isDirectory()){let a=k(i,e,n,r);if(a)return a}else{let a=!1;if(e instanceof RegExp?a=e.test(o):(a=o===e,!a&&process.platform==="win32"&&!e.toLowerCase().endsWith(".exe")&&(a=o.toLowerCase()===`${e.toLowerCase()}.exe`)),a)return i}}}async function j(t,e){let n=`https://api.github.com/repos/${t}/releases/latest`,r={Accept:"application/vnd.github.v3+json","User-Agent":"setup-github-release-action"};e&&(r.Authorization=`token ${e}`);let s=await fetch(n,{headers:r});if(!s.ok){let o=await s.text();throw new Error(`Failed to fetch latest release for ${t}: ${s.statusText}. ${o}`)}return await s.json()}async function W(t,e,n){let r={"User-Agent":"setup-github-release-action"};n&&(r.Authorization=`token ${n}`);let s=await fetch(t,{headers:r});if(!s.ok)throw new Error(`Failed to download asset: ${s.statusText}`);let o=await import("fs"),{Readable:i}=await import("stream"),{finished:l}=await import("stream/promises"),a=o.createWriteStream(e);await l(i.fromWeb(s.body).pipe(a))}var E=require("child_process"),d=f(require("path")),y=f(require("fs"));async function L(t,e){let n=d.extname(t).toLowerCase(),r=d.basename(t).toLowerCase();if(y.existsSync(e)||y.mkdirSync(e,{recursive:!0}),r.endsWith(".tar.gz")||r.endsWith(".tgz")||r.endsWith(".tar")){let o=(0,E.spawnSync)("tar",["-xf",t,"-C",e]);if(o.status!==0)throw new Error(`tar failed with status ${o.status}: ${o.stderr.toString()}`)}else if(r.endsWith(".zip"))if(process.platform==="win32"){let s=`Expand-Archive -Path "${t}" -DestinationPath "${e}" -Force`,o=(0,E.spawnSync)("powershell",["-Command",s]);if(o.status!==0)throw new Error(`powershell Expand-Archive failed with status ${o.status}: ${o.stderr.toString()}`)}else{let s=(0,E.spawnSync)("unzip",["-q",t,"-d",e]);if(s.status!==0)throw new Error(`unzip failed with status ${s.status}: ${s.stderr.toString()}`)}else if(r.endsWith(".7z")){let s=(0,E.spawnSync)("7z",["x",t,`-o${e}`,"-y"]);if(s.status!==0)throw new Error(`7z failed with status ${s.status}. Make sure 7z is installed.`)}else{let s=d.join(e,d.basename(t));y.copyFileSync(t,s)}}async function K(){let{values:t,positionals:e}=(0,B.parseArgs)({options:{"file-name":{type:"string",short:"f"},"binary-name":{type:"string",short:"b"},"file-type":{type:"string",short:"t",default:"archive"},"install-path":{type:"string",short:"p"},token:{type:"string",short:"k"},debug:{type:"boolean",short:"d",default:!1},help:{type:"boolean",short:"h"}},allowPositionals:!0});(t.help||e.length===0)&&(console.log(`
Usage: install-github-release [options] <repository>
Arguments:
repository The GitHub repository (owner/repo)
Options:
-f, --file-name <name> Asset file name or regex pattern (prefixed with ~)
-b, --binary-name <name> Binary to search for (prefixed with ~ for regex)
-t, --file-type <type> 'archive', 'package', or custom regex (default: archive)
-p, --install-path <path> Custom installation directory
-k, --token <token> GitHub token
-d, --debug Enable debug logging
-h, --help Show this help message
`),process.exit(0));let n=e[0];n||(console.error("Error: Repository is required."),process.exit(1));let r=t["file-name"],s=t["binary-name"],o=t["file-type"],i=!!t.debug,l=t.token||process.env.GITHUB_TOKEN;try{let a=_(),h=n.split("/").pop()||n;console.log(`Fetching latest release for ${n}...`);let $=await j(n,l),m=C($.assets,a,{fileName:r,fileType:o});console.log(`Selected asset: ${m.name}`);let x=c.mkdtempSync(p.join(P.tmpdir(),"setup-gh-release-")),g=p.join(x,m.name);console.log(`Downloading ${m.name}...`),await W(m.browser_download_url,g,l);let w=p.join(x,"extract");console.log(`Extracting ${m.name}...`),await L(g,w);let A=s||h,R;A.startsWith("~")?R=new RegExp(A.substring(1),"i"):R=A;let T=k(w,R,i,console.log);if(!T)throw new Error(`Could not find binary "${A}" in the extracted asset.`);let u;if(t["install-path"])u=p.resolve(t["install-path"]);else if(process.getuid&&process.getuid()===0)u="/usr/local/bin";else{let z=p.join(P.homedir(),"bin");c.existsSync(z)?u=z:u="/usr/local/bin"}c.existsSync(u)||c.mkdirSync(u,{recursive:!0});let I=p.basename(T),v=p.join(u,I);console.log(`Installing ${I} to ${v}...`),c.copyFileSync(T,v),process.platform!=="win32"&&c.chmodSync(v,"755"),c.rmSync(x,{recursive:!0,force:!0}),console.log("Installation successful!")}catch(a){console.error(`Error: ${a.message}`),process.exit(1)}}K();
+22067 -59
View File
File diff suppressed because one or more lines are too long
+54 -3
View File
@@ -1,16 +1,22 @@
{ {
"name": "setup-github-release", "name": "install-github-release",
"version": "1.0.0", "version": "1.0.0",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "setup-github-release", "name": "install-github-release",
"version": "1.0.0", "version": "1.0.0",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@actions/core": "^1.11.0", "@actions/core": "^1.11.0",
"@actions/tool-cache": "^2.0.2" "@actions/tool-cache": "^2.0.2",
"commander": "^14.0.3",
"minimatch": "^10.2.5"
},
"bin": {
"check-github-token": "dist/check-token.js",
"install-github-release": "dist/cli.js"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^25.0.0", "@types/node": "^25.0.0",
@@ -530,6 +536,36 @@
"undici-types": "~7.16.0" "undici-types": "~7.16.0"
} }
}, },
"node_modules/balanced-match": {
"version": "4.0.4",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-4.0.4.tgz",
"integrity": "sha512-BLrgEcRTwX2o6gGxGOCNyMvGSp35YofuYzw9h1IMTRmKqttAZZVU67bdb9Pr2vUHA8+j3i2tJfjO6C6+4myGTA==",
"license": "MIT",
"engines": {
"node": "18 || 20 || >=22"
}
},
"node_modules/brace-expansion": {
"version": "5.0.5",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-5.0.5.tgz",
"integrity": "sha512-VZznLgtwhn+Mact9tfiwx64fA9erHH/MCXEUfB/0bX/6Fz6ny5EGTXYltMocqg4xFAQZtnO3DHWWXi8RiuN7cQ==",
"license": "MIT",
"dependencies": {
"balanced-match": "^4.0.2"
},
"engines": {
"node": "18 || 20 || >=22"
}
},
"node_modules/commander": {
"version": "14.0.3",
"resolved": "https://registry.npmjs.org/commander/-/commander-14.0.3.tgz",
"integrity": "sha512-H+y0Jo/T1RZ9qPP4Eh1pkcQcLRglraJaSLoyOtHxu6AapkjWVCy2Sit1QQ4x3Dng8qDlSsZEet7g5Pq06MvTgw==",
"license": "MIT",
"engines": {
"node": ">=20"
}
},
"node_modules/esbuild": { "node_modules/esbuild": {
"version": "0.27.2", "version": "0.27.2",
"resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.27.2.tgz", "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.27.2.tgz",
@@ -572,6 +608,21 @@
"@esbuild/win32-x64": "0.27.2" "@esbuild/win32-x64": "0.27.2"
} }
}, },
"node_modules/minimatch": {
"version": "10.2.5",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-10.2.5.tgz",
"integrity": "sha512-MULkVLfKGYDFYejP07QOurDLLQpcjk7Fw+7jXS2R2czRQzR56yHRveU5NDJEOviH+hETZKSkIk5c+T23GjFUMg==",
"license": "BlueOak-1.0.0",
"dependencies": {
"brace-expansion": "^5.0.5"
},
"engines": {
"node": "18 || 20 || >=22"
},
"funding": {
"url": "https://github.com/sponsors/isaacs"
}
},
"node_modules/semver": { "node_modules/semver": {
"version": "6.3.1", "version": "6.3.1",
"resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz", "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz",
+11 -12
View File
@@ -4,32 +4,31 @@
"description": "A GitHub Action and CLI tool to download and install binaries from GitHub releases", "description": "A GitHub Action and CLI tool to download and install binaries from GitHub releases",
"main": "dist/index.js", "main": "dist/index.js",
"bin": { "bin": {
"install-github-release": "dist/cli.js" "install-github-release": "dist/cli.js",
"check-github-token": "dist/check-token.js"
}, },
"scripts": { "scripts": {
"build:action": "esbuild src/index.ts --bundle --platform=node --target=node24 --outfile=dist/index.js --minify", "build:action": "esbuild src/index.ts --bundle --platform=node --target=node24 --outfile=dist/index.js",
"build:cli": "esbuild src/cli.ts --bundle --platform=node --target=node24 --outfile=dist/cli.js --minify --banner:js=\"#!/usr/bin/env node\"", "build:cli": "esbuild src/cli.ts --bundle --platform=node --target=node24 --outfile=dist/cli.js --banner:js=\"#!/usr/bin/env node\" && chmod +x dist/cli.js",
"build": "npm run build:action && npm run build:cli", "build:check-token": "esbuild src/check-token.ts --bundle --platform=node --target=node24 --outfile=dist/check-token.js --banner:js=\"#!/usr/bin/env node\" && chmod +x dist/check-token.js",
"format": "prettier --write '**/*.ts'", "build:check-token-action": "esbuild src/check-token-action.ts --bundle --platform=node --target=node24 --outfile=dist/check-token-action.js",
"format-check": "prettier --check '**/*.ts'", "build": "npm run build:action && npm run build:cli && npm run build:check-token && npm run build:check-token-action"
"lint": "eslint src/**/*.ts",
"package": "npm run build",
"test": "jest",
"all": "npm run format && npm run lint && npm run test && npm run package"
}, },
"keywords": [ "keywords": [
"actions", "actions",
"node", "node",
"setup" "setup"
], ],
"author": "", "author": "Sławomir Koszewski",
"license": "MIT", "license": "MIT",
"engines": { "engines": {
"node": ">=24" "node": ">=24"
}, },
"dependencies": { "dependencies": {
"@actions/core": "^1.11.0", "@actions/core": "^1.11.0",
"@actions/tool-cache": "^2.0.2" "@actions/tool-cache": "^2.0.2",
"commander": "^14.0.3",
"minimatch": "^10.2.5"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^25.0.0", "@types/node": "^25.0.0",
+24
View File
@@ -0,0 +1,24 @@
import * as core from '@actions/core';
import { fetchLatestRelease } from './core/downloader';
async function run() {
try {
const repository = core.getInput('repository') || 'actions/checkout';
const token = core.getInput('token') || process.env.GITHUB_TOKEN;
if (!token) {
core.setFailed('No GitHub token provided as an input or found in GITHUB_TOKEN environment variable.');
return;
}
core.info(`Verifying GitHub token using repository ${repository}...`);
// Attempt to list latest release of the specified repository as a test
await fetchLatestRelease(repository, token);
core.info('Success: The provided GitHub token is valid and has sufficient permissions to access the repository.');
} catch (error: any) {
core.setFailed(`GitHub token verification failed. Reason: ${error.message}`);
}
}
run();
+29
View File
@@ -0,0 +1,29 @@
import { parseArgs } from 'util';
import { fetchLatestRelease } from './core/downloader';
async function run() {
const { positionals } = parseArgs({
allowPositionals: true
});
const token = positionals[0] || process.env.GITHUB_TOKEN;
if (!token) {
console.error('Error: No GitHub token provided as an argument or found in GITHUB_TOKEN environment variable.');
process.exit(1);
}
try {
console.log('Verifying GitHub token...');
// Attempt to list latest release of actions/checkout as a test
await fetchLatestRelease('actions/checkout', token);
console.log('\x1b[32mSuccess: The provided GitHub token is valid and has sufficient permissions to access public repositories.\x1b[0m');
} catch (error: any) {
console.error('\x1b[31mError: GitHub token verification failed.\x1b[0m');
console.error(`Reason: ${error.message}`);
process.exit(1);
}
}
run();
+210 -86
View File
@@ -1,119 +1,243 @@
import { parseArgs } from 'util';
import * as path from 'path'; import * as path from 'path';
import * as fs from 'fs'; import * as fs from 'fs';
import * as os from 'os'; import * as os from 'os';
import { spawnSync } from 'child_process';
import { Command } from 'commander';
import { getPlatformInfo } from './core/platform'; import { getPlatformInfo } from './core/platform';
import { getMatchingAsset } from './core/matcher'; import { getMatchingAsset } from './core/matcher';
import { findBinary } from './core/finder'; import { findBinary } from './core/finder';
import { fetchLatestRelease, downloadAsset } from './core/downloader'; import { fetchLatestRelease, fetchLatestReleaseRaw, downloadAsset } from './core/downloader';
import { extractAsset } from './core/extractor'; import { extractAsset } from './core/extractor';
async function run() { interface CliOptions {
const { values, positionals } = parseArgs({ appName?: string;
options: { fileName?: string;
'file-name': { type: 'string', short: 'f' }, binaryName?: string;
'binary-name': { type: 'string', short: 'b' }, fileType?: string;
'file-type': { type: 'string', short: 't', default: 'archive' }, installPath?: string;
'install-path': { type: 'string', short: 'p' }, outputDirectory?: string;
'token': { type: 'string', short: 'k' }, releasesJsonOnly: boolean;
'debug': { type: 'boolean', short: 'd', default: false }, listOnly: boolean;
'help': { type: 'boolean', short: 'h' } token?: string;
}, debug: boolean;
allowPositionals: true dryRun: boolean;
}); systemOverride?: string;
archOverride?: string;
if (values.help || positionals.length === 0) { listRepo?: string;
console.log(` positionals: string[];
Usage: install-github-release [options] <repository>
Arguments:
repository The GitHub repository (owner/repo)
Options:
-f, --file-name <name> Asset file name or regex pattern (prefixed with ~)
-b, --binary-name <name> Binary to search for (prefixed with ~ for regex)
-t, --file-type <type> 'archive', 'package', or custom regex (default: archive)
-p, --install-path <path> Custom installation directory
-k, --token <token> GitHub token
-d, --debug Enable debug logging
-h, --help Show this help message
`);
process.exit(0);
} }
const repository = positionals[0]; function validateOutputDirectory(outputDirectory: string): string {
const resolvedPath = path.resolve(outputDirectory);
if (!fs.existsSync(resolvedPath) || !fs.statSync(resolvedPath).isDirectory()) {
throw new Error(`Output directory "${resolvedPath}" does not exist.`);
}
return resolvedPath;
}
function getInstallDir(installPath?: string): string {
if (installPath) {
return path.resolve(installPath);
}
if (process.platform === 'win32') {
const localAppData = process.env.LOCALAPPDATA || path.join(os.homedir(), 'AppData', 'Local');
return path.join(localAppData, 'bin');
}
const isRoot = process.getuid && process.getuid() === 0;
if (isRoot) {
return '/usr/local/bin';
}
const homeBin = path.join(os.homedir(), 'bin');
if (fs.existsSync(homeBin)) {
return homeBin;
}
return '/usr/local/bin';
}
function installSystemPackage(downloadPath: string): void {
const fileName = path.basename(downloadPath).toLowerCase();
const command: { binary: string; args: string[] } | undefined = fileName.endsWith('.deb')
? { binary: 'dpkg', args: ['-i', downloadPath] }
: fileName.endsWith('.pkg')
? { binary: 'installer', args: ['-pkg', downloadPath, '-target', '/'] }
: fileName.endsWith('.rpm')
? { binary: 'rpm', args: ['-i', downloadPath] }
: undefined;
if (!command) {
throw new Error(`Unsupported package type: ${fileName}`);
}
const isRoot = process.getuid && process.getuid() === 0;
const commandToRun = isRoot ? command.binary : 'sudo';
const argsToRun = isRoot ? command.args : [command.binary, ...command.args];
const result = spawnSync(commandToRun, argsToRun, { stdio: 'inherit' });
if (result.status !== 0) {
throw new Error(`Failed to install package using ${commandToRun} ${argsToRun.join(' ')}.`);
}
}
async function run() {
let tempDir: string | undefined;
const program = new Command();
program
.name('install-github-release')
.usage('[options] <repository>')
.argument('[repository]', 'The GitHub repository (owner/repo)')
.option('--dry-run', 'Run in test mode')
.option('-l, --list [repository]', 'List available assets from latest release and exit')
.option('-a, --app-name <name>', 'Application name (optional, for output messages)')
.option('-f, --file-name <name>', 'Asset file name or regex pattern (prefixed with ~)')
.option('-b, --binary-name <name>', 'Binary name (supports source:destination form)')
.option('-t, --file-type <type>', 'Known: archive|package|linux|macos|targz; custom: ~<regex> or extension')
.option('-p, --install-path <path>', 'Custom installation directory')
.option('-o, --output-directory <path>', 'Only download selected asset to the specified directory')
.option('-j, --releases-json', 'Download latest release JSON only')
.option('--system <name>', 'Override detected system for asset matching')
.option('--arch <name>', 'Override detected architecture for asset matching')
.option('-k, --token <token>', 'GitHub token')
.option('-d, --debug', 'Enable debug logging')
.allowUnknownOption(false);
const cleanupTempDir = () => {
if (tempDir && fs.existsSync(tempDir)) {
fs.rmSync(tempDir, { recursive: true, force: true });
}
};
program.parse(process.argv);
const parsedOptions = program.opts();
const rawFileType = parsedOptions.fileType;
const fileType = typeof rawFileType === 'string' ? rawFileType.trim() : undefined;
if (rawFileType !== undefined && !fileType) {
throw new Error(`Unknown asset type: ${rawFileType}`);
}
const listValue = parsedOptions.list as string | boolean | undefined;
const options: CliOptions = {
appName: parsedOptions.appName as string | undefined,
fileName: parsedOptions.fileName as string | undefined,
binaryName: parsedOptions.binaryName as string | undefined,
fileType,
installPath: parsedOptions.installPath as string | undefined,
outputDirectory: parsedOptions.outputDirectory as string | undefined,
releasesJsonOnly: Boolean(parsedOptions.releasesJson),
listOnly: listValue !== undefined,
token: parsedOptions.token as string | undefined,
debug: Boolean(parsedOptions.debug),
dryRun: Boolean(parsedOptions.dryRun),
systemOverride: parsedOptions.system as string | undefined,
archOverride: parsedOptions.arch as string | undefined,
listRepo: typeof listValue === 'string' ? listValue : undefined,
positionals: program.args
};
const repository = options.listRepo || options.positionals[0];
if (!repository) { if (!repository) {
console.error('Error: Repository is required.'); program.outputHelp();
process.exit(1); process.exit(1);
} }
const fileNameInput = values['file-name']; const token = options.token;
const binaryInput = values['binary-name'];
const fileType = values['file-type']; if (options.listOnly) {
const debug = !!values.debug; const release = await fetchLatestRelease(repository, token);
const token = values.token || process.env.GITHUB_TOKEN; release.assets.forEach((asset) => console.log(`- ${asset.browser_download_url}`));
process.exit(0);
}
try {
const platformInfo = getPlatformInfo();
const toolName = repository.split('/').pop() || repository; const toolName = repository.split('/').pop() || repository;
const appName = options.appName || (toolName.charAt(0).toUpperCase() + toolName.slice(1));
const binaryOption = options.binaryName || toolName;
const [binarySource, binaryDestination] = binaryOption.includes(':')
? [binaryOption.split(':')[0], binaryOption.split(':')[1]]
: [binaryOption, binaryOption];
if (options.releasesJsonOnly) {
const rawRelease = await fetchLatestReleaseRaw(repository, token);
const outputBase = binaryDestination || toolName;
const outputName = `${outputBase}.releases.json`;
const outputPath = options.outputDirectory
? path.join(validateOutputDirectory(options.outputDirectory), outputName)
: outputName;
fs.writeFileSync(outputPath, rawRelease, 'utf8');
console.log(`Downloaded GitHub releases to ${outputPath}.`);
process.exit(0);
}
const platformInfo = getPlatformInfo({
system: options.systemOverride,
arch: options.archOverride
});
console.log(`Fetching latest release for ${repository}...`); console.log(`Fetching latest release for ${repository}...`);
const release = await fetchLatestRelease(repository, token); const release = await fetchLatestRelease(repository, token);
const asset = getMatchingAsset(release.assets, platformInfo, { const asset = getMatchingAsset(release.assets, platformInfo, options.fileName, options.fileType);
fileName: fileNameInput,
fileType: fileType
});
console.log(`Selected asset: ${asset.name}`); const version = release.tag_name.replace(/^v/i, '');
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'setup-gh-release-')); const downloadUrl = asset.browser_download_url;
console.log(`Will download '${appName}' version: ${version}`);
console.log(`Download URL: "${downloadUrl}".`);
if (options.dryRun) {
process.exit(0);
}
if (options.outputDirectory) {
const outputDir = validateOutputDirectory(options.outputDirectory);
const outputPath = path.join(outputDir, path.basename(downloadUrl));
console.log(`Downloading '${appName}' version ${version} to '${outputPath}'...`);
await downloadAsset(downloadUrl, outputPath, token);
process.exit(0);
}
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'setup-gh-release-'));
process.once('exit', cleanupTempDir);
const downloadPath = path.join(tempDir, asset.name); const downloadPath = path.join(tempDir, asset.name);
await downloadAsset(downloadUrl, downloadPath, token);
console.log(`Downloading ${asset.name}...`); if (/\.(deb|pkg|rpm)$/i.test(asset.name)) {
await downloadAsset(asset.browser_download_url, downloadPath, token); installSystemPackage(downloadPath);
console.log('Installation successful!');
process.exit(0);
}
const extractDir = path.join(tempDir, 'extract'); const extractDir = path.join(tempDir, 'extract');
console.log(`Extracting ${asset.name}...`); console.log(`Extracting ${asset.name}...`);
await extractAsset(downloadPath, extractDir); await extractAsset(downloadPath, extractDir);
const binaryName = binaryInput || toolName;
let binaryPattern: string | RegExp; let binaryPattern: string | RegExp;
if (binaryName.startsWith('~')) { if (binarySource.startsWith('~')) {
binaryPattern = new RegExp(binaryName.substring(1), 'i'); const binaryRegex = binarySource
.substring(1)
.replace(/{{SYSTEM}}/g, platformInfo.systemPattern)
.replace(/{{ARCH}}/g, platformInfo.archPattern);
binaryPattern = new RegExp(binaryRegex, 'i');
} else { } else {
binaryPattern = binaryName; binaryPattern = binarySource
.replace(/{{SYSTEM}}/g, platformInfo.system)
.replace(/{{ARCH}}/g, platformInfo.arch);
} }
const binaryPath = findBinary(extractDir, binaryPattern, debug, console.log); const binaryPath = findBinary(extractDir, binaryPattern, options.debug, console.log);
if (!binaryPath) { if (!binaryPath) {
throw new Error(`Could not find binary "${binaryName}" in the extracted asset.`); throw new Error(`Could not find binary "${binarySource}" in the extracted asset.`);
}
// Determine install directory
let installDir: string;
if (values['install-path']) {
installDir = path.resolve(values['install-path']);
} else {
const isRoot = process.getuid && process.getuid() === 0;
if (isRoot) {
installDir = '/usr/local/bin';
} else {
const homeBin = path.join(os.homedir(), 'bin');
if (fs.existsSync(homeBin)) {
installDir = homeBin;
} else {
// Fallback or error? Let's use a local bin if possible or /usr/local/bin (might fail)
installDir = '/usr/local/bin';
}
}
} }
const installDir = getInstallDir(options.installPath);
if (!fs.existsSync(installDir)) { if (!fs.existsSync(installDir)) {
fs.mkdirSync(installDir, { recursive: true }); fs.mkdirSync(installDir, { recursive: true });
} }
const finalName = path.basename(binaryPath); const finalName = binaryDestination || path.basename(binaryPath);
const destPath = path.join(installDir, finalName); const destPath = path.join(installDir, finalName);
console.log(`Installing ${finalName} to ${destPath}...`); console.log(`Installing ${finalName} to ${destPath}...`);
@@ -123,15 +247,15 @@ Options:
fs.chmodSync(destPath, '755'); fs.chmodSync(destPath, '755');
} }
// Cleanup
fs.rmSync(tempDir, { recursive: true, force: true });
console.log('Installation successful!'); console.log('Installation successful!');
process.exit(0);
}
} catch (error: any) { void run().catch((error: unknown) => {
if (error instanceof Error && error.message) {
console.error(`Error: ${error.message}`); console.error(`Error: ${error.message}`);
} else {
console.error('Error: Unknown failure.');
}
process.exit(1); process.exit(1);
} });
}
run();
+19 -5
View File
@@ -1,6 +1,3 @@
import { getMatchingAsset } from './matcher';
import { PlatformInfo } from './platform';
export interface ReleaseAsset { export interface ReleaseAsset {
name: string; name: string;
browser_download_url: string; browser_download_url: string;
@@ -11,8 +8,7 @@ export interface ReleaseInfo {
assets: ReleaseAsset[]; assets: ReleaseAsset[];
} }
export async function fetchLatestRelease(repository: string, token?: string): Promise<ReleaseInfo> { function getGithubApiHeaders(token?: string): Record<string, string> {
const url = `https://api.github.com/repos/${repository}/releases/latest`;
const headers: Record<string, string> = { const headers: Record<string, string> = {
'Accept': 'application/vnd.github.v3+json', 'Accept': 'application/vnd.github.v3+json',
'User-Agent': 'setup-github-release-action' 'User-Agent': 'setup-github-release-action'
@@ -20,6 +16,12 @@ export async function fetchLatestRelease(repository: string, token?: string): Pr
if (token) { if (token) {
headers['Authorization'] = `token ${token}`; headers['Authorization'] = `token ${token}`;
} }
return headers;
}
export async function fetchLatestRelease(repository: string, token?: string): Promise<ReleaseInfo> {
const url = `https://api.github.com/repos/${repository}/releases/latest`;
const headers = getGithubApiHeaders(token);
const response = await fetch(url, { headers }); const response = await fetch(url, { headers });
if (!response.ok) { if (!response.ok) {
@@ -30,6 +32,18 @@ export async function fetchLatestRelease(repository: string, token?: string): Pr
return await response.json() as ReleaseInfo; return await response.json() as ReleaseInfo;
} }
export async function fetchLatestReleaseRaw(repository: string, token?: string): Promise<string> {
const url = `https://api.github.com/repos/${repository}/releases/latest`;
const headers = getGithubApiHeaders(token);
const response = await fetch(url, { headers });
const body = await response.text();
if (!response.ok) {
throw new Error(`Failed to fetch latest release for ${repository}: ${response.statusText}. ${body}`);
}
return body;
}
export async function downloadAsset(url: string, destPath: string, token?: string): Promise<void> { export async function downloadAsset(url: string, destPath: string, token?: string): Promise<void> {
const headers: Record<string, string> = { const headers: Record<string, string> = {
'User-Agent': 'setup-github-release-action' 'User-Agent': 'setup-github-release-action'
+16 -4
View File
@@ -18,11 +18,23 @@ export async function extractAsset(filePath: string, destDir: string): Promise<v
} }
} else if (name.endsWith('.zip')) { } else if (name.endsWith('.zip')) {
if (process.platform === 'win32') { if (process.platform === 'win32') {
const command = `Expand-Archive -Path "${filePath}" -DestinationPath "${destDir}" -Force`; // Modern Windows 10/11 has tar that handles zip
const result = spawnSync('powershell', ['-Command', command]); const tarResult = spawnSync('tar', ['-xf', filePath, '-C', destDir]);
if (result.status !== 0) { if (tarResult.status === 0) return;
throw new Error(`powershell Expand-Archive failed with status ${result.status}: ${result.stderr.toString()}`);
// Fallback: Use .NET ZipFile class to bypass PowerShell module trust issues (Microsoft.PowerShell.Archive)
// We escape single quotes for PowerShell.
const escapedFilePath = filePath.replace(/'/g, "''");
const escapedDestDir = destDir.replace(/'/g, "''");
const dotNetCommand = `Add-Type -AssemblyName System.IO.Compression.FileSystem; [System.IO.Compression.ZipFile]::ExtractToDirectory('${escapedFilePath}', '${escapedDestDir}')`;
// Try pwsh (PowerShell 7) then powershell (Windows PowerShell)
for (const shell of ['pwsh', 'powershell']) {
const result = spawnSync(shell, ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-Command', dotNetCommand]);
if (result.status === 0) return;
} }
throw new Error(`Extraction failed: Both tar and PowerShell fallback failed. Make sure your system can extract ZIP files.`);
} else { } else {
const result = spawnSync('unzip', ['-q', filePath, '-d', destDir]); const result = spawnSync('unzip', ['-q', filePath, '-d', destDir]);
if (result.status !== 0) { if (result.status !== 0) {
+62 -53
View File
@@ -1,67 +1,76 @@
import { PlatformInfo } from './platform'; import { PlatformInfo } from './platform';
import { minimatch } from 'minimatch';
export interface MatchOptions { type ReleaseAsset = { name: string; browser_download_url: string };
fileName?: string;
fileType?: string;
}
export function getMatchingAsset(assets: any[], platform: PlatformInfo, options: MatchOptions): any { const knownFileTypes: Record<string, string> = {
const { fileName, fileType = 'archive' } = options; archive: '*.{zip,tar.gz,tgz}',
let extPattern: string; package: '*.{deb,pkg,rpm}',
if (fileType === 'archive') { linux: '*.{deb,rpm}',
extPattern = '\\.(zip|tar\\.gz|tar|tgz|7z)'; macos: '*.pkg',
} else if (fileType === 'package') { targz: '*.{tgz,tar.gz}',
extPattern = '\\.(deb|rpm|pkg)'; };
} else {
extPattern = fileType;
}
if (!fileName) { function filterByRegex(assets: ReleaseAsset[], pattern: string): ReleaseAsset[] {
// Rule 1: Default matching rule
const pattern = `${platform.systemPattern}[_-]${platform.archPattern}.*${extPattern}$`;
const regex = new RegExp(pattern, 'i'); const regex = new RegExp(pattern, 'i');
const matchingAssets = assets.filter((a: any) => regex.test(a.name)); return assets.filter((asset) => regex.test(asset.name));
if (matchingAssets.length === 0) {
throw new Error(`No assets matched the default criteria: ${pattern}`);
}
if (matchingAssets.length > 1) {
throw new Error(`Multiple assets matched the default criteria: ${matchingAssets.map((a: any) => a.name).join(', ')}`);
}
return matchingAssets[0];
} else if (fileName.startsWith('~')) {
// Rule 3: Regex matching rule
let pattern = fileName.substring(1);
const hasSystem = pattern.includes('{{SYSTEM}}');
const hasArch = pattern.includes('{{ARCH}}');
const hasExt = pattern.includes('{{EXT_PATTERN}}');
const hasEnd = pattern.endsWith('$');
if (!hasSystem && !hasArch && !hasExt && !hasEnd) {
pattern += `.*{{SYSTEM}}[_-]{{ARCH}}.*{{EXT_PATTERN}}$`;
} else if (hasSystem && hasArch && !hasExt && !hasEnd) {
pattern += `.*{{EXT_PATTERN}}$`;
} }
const finalPattern = pattern function replacePlatformPlaceholders(pattern: string, platform: PlatformInfo): string {
return pattern
.replace(/{{SYSTEM}}/g, platform.systemPattern) .replace(/{{SYSTEM}}/g, platform.systemPattern)
.replace(/{{ARCH}}/g, platform.archPattern) .replace(/{{ARCH}}/g, platform.archPattern);
.replace(/{{EXT_PATTERN}}/g, extPattern); }
const regex = new RegExp(finalPattern, 'i'); export function getMatchingAsset(assets: ReleaseAsset[], platform: PlatformInfo, fileName?: string, fileType?: string): ReleaseAsset {
const matchingAssets = assets.filter((a: any) => regex.test(a.name)); // Filename provided as literal string (no ~): exact match.
if (matchingAssets.length === 0) { if (fileName && !fileName.startsWith('~')) {
throw new Error(`No assets matched the regex: ${finalPattern}`); const exactMatches = assets.filter((asset) => asset.name === fileName);
if (exactMatches.length !== 1) {
throw new Error(`Expected exactly one asset to match the provided filename, matched: ${exactMatches.length}`);
} }
if (matchingAssets.length > 1) { return exactMatches[0];
throw new Error(`Multiple assets matched the criteria: ${matchingAssets.map((a: any) => a.name).join(', ')}`);
} }
return matchingAssets[0];
// Filetype filtering stage (or passthrough when not provided).
let fileTypeFilteredAssets: ReleaseAsset[] = assets;
if (fileType) {
if (Object.hasOwn(knownFileTypes, fileType)) {
// 2. Known fileType key: use predefined glob.
const fileTypeGlob = knownFileTypes[fileType];
fileTypeFilteredAssets = assets.filter((asset) => minimatch(asset.name, fileTypeGlob, { nocase: true }));
} else if (fileType.startsWith('~')) {
// 3. Custom regex fileType: match regex at end of string.
const fileTypeRegex = `${fileType.substring(1)}$`;
fileTypeFilteredAssets = filterByRegex(assets, fileTypeRegex);
} else { } else {
// Rule 2: Literal matching rule // 4. Custom extension fileType: treat as plain extension glob.
const asset = assets.find((a: any) => a.name === fileName); const extension = fileType.replace(/^\./, '');
if (!asset) { const fileTypeGlob = `*.${extension}`;
throw new Error(`No asset found matching the exact name: ${fileName}`); fileTypeFilteredAssets = assets.filter((asset) => minimatch(asset.name, fileTypeGlob, { nocase: true }));
}
return asset;
} }
} }
// 4. Filename provided with ~: platform placeholder expansion and regex filtering.
if (fileName && fileName.startsWith('~')) {
const fileNamePattern = replacePlatformPlaceholders(fileName.substring(1), platform);
const fileNameFilteredAssets = filterByRegex(fileTypeFilteredAssets, fileNamePattern);
if (fileNameFilteredAssets.length !== 1) {
throw new Error(`Expected exactly one asset to match the filename regex, matched: ${fileNameFilteredAssets.length}`);
}
return fileNameFilteredAssets[0];
}
// 5. No filename: use default {{SYSTEM}}-{{ARCH}} regex.
const defaultPattern = replacePlatformPlaceholders('{{SYSTEM}}[_-]{{ARCH}}', platform);
const defaultFilteredAssets = filterByRegex(fileTypeFilteredAssets, defaultPattern);
// 6. Zero or multiple matches are errors.
if (defaultFilteredAssets.length !== 1) {
const errorMessage = defaultFilteredAssets.length === 0
? `No assets matched the default criteria: ${defaultPattern}`
: `Multiple assets matched the default criteria: ${defaultFilteredAssets.map((asset) => asset.name).join(', ')}`;
throw new Error(errorMessage);
}
return defaultFilteredAssets[0];
}
+8 -3
View File
@@ -7,6 +7,11 @@ export interface PlatformInfo {
archPattern: string; archPattern: string;
} }
export interface PlatformOverrides {
system?: string;
arch?: string;
}
export const systemPatterns: Record<string, string> = { export const systemPatterns: Record<string, string> = {
linux: 'linux', linux: 'linux',
darwin: '(darwin|macos|mac|osx)', darwin: '(darwin|macos|mac|osx)',
@@ -18,9 +23,9 @@ export const archPatterns: Record<string, string> = {
arm64: '(aarch64|arm64)' arm64: '(aarch64|arm64)'
}; };
export function getPlatformInfo(): PlatformInfo { export function getPlatformInfo(overrides?: PlatformOverrides): PlatformInfo {
const system = os.platform(); const system = (overrides?.system || os.platform()).toLowerCase();
const arch = os.arch(); const arch = (overrides?.arch || os.arch()).toLowerCase();
return { return {
system, system,
+70 -6
View File
@@ -1,19 +1,71 @@
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as tc from '@actions/tool-cache'; import * as tc from '@actions/tool-cache';
import * as path from 'path'; import * as path from 'path';
import * as os from 'os';
import * as fs from 'fs'; import * as fs from 'fs';
import { spawnSync } from 'child_process';
import { getPlatformInfo } from './core/platform'; import { getPlatformInfo } from './core/platform';
import { getMatchingAsset } from './core/matcher'; import { getMatchingAsset } from './core/matcher';
import { findBinary } from './core/finder'; import { findBinary } from './core/finder';
import { fetchLatestRelease } from './core/downloader'; import { fetchLatestRelease } from './core/downloader';
function installSystemPackage(downloadPath: string): void {
const fileName = path.basename(downloadPath).toLowerCase();
const command: { binary: string; args: string[] } | undefined = fileName.endsWith('.deb')
? { binary: 'dpkg', args: ['-i', downloadPath] }
: fileName.endsWith('.pkg')
? { binary: 'installer', args: ['-pkg', downloadPath, '-target', '/'] }
: fileName.endsWith('.rpm')
? { binary: 'rpm', args: ['-i', downloadPath] }
: undefined;
if (!command) {
throw new Error(`Unsupported package type: ${fileName}`);
}
const isRoot = process.getuid && process.getuid() === 0;
const commandToRun = isRoot ? command.binary : 'sudo';
const argsToRun = isRoot ? command.args : [command.binary, ...command.args];
const result = spawnSync(commandToRun, argsToRun, { stdio: 'inherit' });
if (result.status !== 0) {
throw new Error(`Failed to install package using ${commandToRun} ${argsToRun.join(' ')}.`);
}
}
function findInstalledBinary(binaryName: string): string | undefined {
const isRegex = binaryName.startsWith('~');
if (!isRegex) {
const whichResult = spawnSync('which', [binaryName], { encoding: 'utf8' });
if (whichResult.status === 0) {
const resolvedPath = (whichResult.stdout || '').trim();
if (resolvedPath) {
return resolvedPath;
}
}
}
const candidates = ['/usr/local/bin', '/usr/bin', '/opt/homebrew/bin', '/opt/local/bin'];
const pattern: string | RegExp = isRegex ? new RegExp(binaryName.substring(1), 'i') : binaryName;
for (const candidateDir of candidates) {
if (!fs.existsSync(candidateDir)) {
continue;
}
const candidatePath = findBinary(candidateDir, pattern, false, () => undefined);
if (candidatePath) {
return candidatePath;
}
}
return undefined;
}
async function run() { async function run() {
try { try {
const repository = core.getInput('repository', { required: true }); const repository = core.getInput('repository', { required: true });
const fileNameInput = core.getInput('file-name'); const fileNameInput = core.getInput('file-name');
const binaryInput = core.getInput('binary-name'); const binaryInput = core.getInput('binary-name');
const fileType = core.getInput('file-type') || 'archive'; const fileType = core.getInput('file-type');
const updateCache = core.getInput('update-cache') || 'false'; const updateCache = core.getInput('update-cache') || 'false';
const debug = core.getBooleanInput('debug'); const debug = core.getBooleanInput('debug');
const token = core.getInput('token') || process.env.GITHUB_TOKEN; const token = core.getInput('token') || process.env.GITHUB_TOKEN;
@@ -39,10 +91,7 @@ async function run() {
core.info(`Fetching latest release information for ${repository}...`); core.info(`Fetching latest release information for ${repository}...`);
const release = await fetchLatestRelease(repository, token); const release = await fetchLatestRelease(repository, token);
const asset = getMatchingAsset(release.assets, platformInfo, { const asset = getMatchingAsset(release.assets, platformInfo, fileNameInput, fileType);
fileName: fileNameInput,
fileType: fileType
});
core.info(`Selected asset: ${asset.name}`); core.info(`Selected asset: ${asset.name}`);
@@ -67,6 +116,21 @@ async function run() {
const nameLower = asset.name.toLowerCase(); const nameLower = asset.name.toLowerCase();
let toolDir: string; let toolDir: string;
if (/\.(deb|pkg|rpm)$/i.test(nameLower)) {
core.info(`Installing package asset ${asset.name}...`);
installSystemPackage(downloadPath);
const binaryPath = findInstalledBinary(binaryName);
if (!binaryPath) {
throw new Error(`Package installed, but binary "${binaryName}" could not be located in common executable paths.`);
}
const binaryDir = path.dirname(binaryPath);
core.addPath(binaryDir);
core.info(`Binary found at ${binaryPath}. Added ${binaryDir} to PATH.`);
return;
}
// Determine extraction method based on extension // Determine extraction method based on extension
if (/\.(tar\.gz|tar|tgz)$/i.test(nameLower)) { if (/\.(tar\.gz|tar|tgz)$/i.test(nameLower)) {
toolDir = await tc.extractTar(downloadPath); toolDir = await tc.extractTar(downloadPath);