Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/publish-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: "20"
node-version: "22.x"
cache: npm
- run: npm ci
- run: npm run build
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: "20"
node-version: "22.x"
cache: npm
- run: npm ci
- run: npm run build
Expand Down
8 changes: 8 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@actions/tool-cache": "^2.0.1",
"@octokit/auth-unauthenticated": "^5.0.1",
"@types/node": "^20.9.0",
"@types/semver": "^7.7.1",
"execa": "^8.0.1",
"semver": "^7.5.4",
"typescript": "^5.0.0",
Expand Down
91 changes: 64 additions & 27 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import * as core from "@actions/core";
import * as tc from "@actions/tool-cache";
import * as github from "@actions/github";
import { join, basename } from "node:path";
import { join } from "node:path";
import { existsSync } from "node:fs";
import semver from "semver";
import process from "node:process";
import { pipeline } from "node:stream/promises";
import { createWriteStream } from "node:fs";
import { $ } from "execa";
import { createUnauthenticatedAuth } from "@octokit/auth-unauthenticated";

Expand All @@ -28,40 +27,78 @@ if (version === "latest") {
repo: "cli",
});
const versions = releases.map((release) => release.tag_name.slice(1));
version = semver.maxSatisfying(versions, version);
version = semver.maxSatisfying(versions, version) ?? "2.28.0";
}
core.debug(`Resolved version: ${version}`);

const platformTypes: Partial<Record<NodeJS.Platform, string>> = {
linux: "linux",
darwin: "macOS",
win32: "windows",
};

const archTypes: Partial<Record<NodeJS.Architecture, string>> = {
x64: "amd64",
arm: "arm",
arm64: "arm64",
};

const extByPlatform: Partial<Record<NodeJS.Platform, string>> = {
linux: "tar.gz",
darwin: semver.lt(version, "2.28.0") ? "tar.gz" : "zip",
win32: "zip",
};

const platform = platformTypes[process.platform] ?? "linux";
const arch = archTypes[process.arch] ?? "amd64";
const ext = extByPlatform[process.platform] ?? "tar.gz";
const folderName = `gh_${version}_${platform}_${arch}`;
const zipFileName = `${folderName}.${ext}`;
const downloadUrl = `https://github.com/cli/cli/releases/download/v${version}/${zipFileName}`;
const binFileName = platform === 'windows' ? "gh.exe" : "gh";

let found = tc.find("gh", version);
core.setOutput("cache-hit", !!found);
if (!found) {
const platform = {
linux: "linux",
darwin: "macOS",
win32: "windows",
}[process.platform];
const arch = {
x64: "amd64",
arm: "arm",
arm64: "arm64",
}[process.arch];
const ext = {
linux: "tar.gz",
darwin: semver.lt(version, "2.28.0") ? "tar.gz" : "zip",
win32: "zip",
}[process.platform];
const file = `gh_${version}_${platform}_${arch}.${ext}`;
found = await tc.downloadTool(
`https://github.com/cli/cli/releases/download/v${version}/${file}`
);
if (file.endsWith(".zip")) {
found = await tc.extractZip(found);
core.debug(`Downloading GH CLI ${version} from ${downloadUrl} ...`);
const downloadedFile = await tc.downloadTool(downloadUrl);
core.debug(`Downloaded GH CLI ${version} to ${downloadedFile}`);
if (ext === "zip") {
found = await tc.extractZip(downloadedFile);
} else {
found = await tc.extractTar(found);
found = await tc.extractTar(downloadedFile);
}
found = await tc.cacheDir(found, "gh", version);
core.debug(`Cached GH CLI ${version} to ${found}`);
} else {
core.debug(`Using cached GH CLI ${version} from ${found}`);
}
core.addPath(found);
const bin0Dir = found;
const bin1Dir = join(found, "bin");
const bin2Dir = join(found, folderName, "bin");

const bin0Path = join(bin0Dir, binFileName);
const bin1Path = join(bin1Dir, binFileName);
const bin2Path = join(bin2Dir, binFileName);

let binDir: string;
if (existsSync(bin0Path)) {
core.debug(`Found GH CLI binary in ${bin0Dir}`);
binDir = bin0Dir;
} else if (existsSync(bin1Path)) {
core.debug(`Found GH CLI binary in ${bin1Dir}`);
binDir = bin1Dir;
} else if (existsSync(bin2Path)) {
core.debug(`Found GH CLI binary in ${bin2Dir}`);
binDir = bin2Dir;
} else {
core.error(`Could not find GH CLI binary in ${found}`);
core.setFailed(`Could not find GH CLI binary in ${found}`);
process.exit(1);
}

core.addPath(binDir);
core.debug(`Added ${binDir} to PATH`);
core.setOutput("gh-version", version);

const token = core.getInput("token");
Expand Down