Skip to content

Commit fe93ed9

Browse files
committed
feedback updates
1 parent 3834416 commit fe93ed9

6 files changed

Lines changed: 84 additions & 54 deletions

File tree

.github/workflows/test-action.yml

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ on: [pull_request, workflow_dispatch]
33

44
jobs:
55
test-default:
6-
runs-on: ubuntu-latest
6+
strategy:
7+
matrix:
8+
os: [ubuntu-latest,windows-latest,macos-latest]
79
steps:
810
- uses: actions/checkout@v4
911
- name: Setup func CLI
@@ -37,18 +39,31 @@ jobs:
3739
name: 'my-func'
3840
- run: my-func version
3941

40-
test-windows:
41-
runs-on: windows-latest
42+
test-custom-destination:
43+
runs-on: ubuntu-latest
4244
steps:
4345
- uses: actions/checkout@v4
44-
- name: Setup func CLI
46+
- name: Setup func CLI with custom destination
4547
uses: ./
48+
with:
49+
destination: '/tmp/func-bin'
4650
- run: func version
51+
- name: Verify func using path
52+
run: /tmp/func-bin/func version
4753

48-
test-macos:
49-
runs-on: macos-latest
54+
test-invalid-version:
55+
runs-on: ubuntu-latest
5056
steps:
5157
- uses: actions/checkout@v4
52-
- name: Setup func CLI
58+
- name: Setup func CLI with non-existing version (should fail)
59+
id: invalid-version
5360
uses: ./
54-
- run: func version
61+
with:
62+
version: 'v99.99.99'
63+
continue-on-error: true
64+
- name: Verify action failed
65+
run: |
66+
if [ "${{ steps.invalid-version.outcome }}" != "failure" ]; then
67+
echo "Expected action to fail with invalid version"
68+
exit 1
69+
fi

action.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ inputs:
99
description: '(optional) Provide version to download. Any version in release pages works https://github.com/knative/func/tags'
1010
destination:
1111
description: '(optional) Provide a path where to move the desired downloaded binary, otherwise cwd is used'
12+
binarySource:
13+
description: '(optional) Base URL for downloading binaries. Defaults to GitHub releases. Pattern: <url-base>/<version>/<os-bin-name>'
1214
runs:
1315
using: 'node20'
1416
main: 'index.js'

index.js

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,29 @@ function getOsBinName() {
3030

3131
// Normalizes version to release tag format: knative-vX.Y.Z
3232
// Ex.: '1.16' or 'v1.16' will return 'knative-v1.16.0'
33-
function smartVersionUpdate(version){
33+
function smartVersionUpdate(version) {
3434
const versionRegex = /^(?<knprefix>knative-)?(?<prefix>v?)(?<major>\d+)\.(?<minor>\d+)(.(?<patch>\d+))?$/;
35-
let match = version.match(versionRegex);
36-
if (match) {
37-
const knprefix = 'knative-';
38-
const prefix = 'v';
39-
const patch = match.groups.patch ?? 0;
40-
return `${knprefix}${prefix}${match.groups.major}.${match.groups.minor}.${patch}`;
41-
}
42-
43-
core.setFailed(`Invalid version format (${version}). Expected format: "1.16[.X]" or "v1.16[.X]"`);
44-
return undefined;
35+
const match = version.match(versionRegex);
36+
if (!match) {
37+
throw new Error(`Invalid version format (${version}). Expected format: "1.16[.X]" or "v1.16[.X]"`);
38+
}
39+
const knprefix = 'knative-';
40+
const prefix = 'v';
41+
const patch = match.groups.patch ?? 0;
42+
return `${knprefix}${prefix}${match.groups.major}.${match.groups.minor}.${patch}`;
4543
}
4644

47-
// Downloads binary and makes it executable
48-
async function cmdConstructAndRun(url, binPath) {
49-
await exec.exec('curl', ['-L', '-o', binPath, url]);
50-
45+
const DEFAULT_BINARY_SOURCE = 'https://github.com/knative/func/releases/download';
46+
47+
// Downloads binary from release URL and makes it executable
48+
async function downloadFuncBinary(version, osBinName, binPath, binarySource) {
49+
const url = `${binarySource}/${version}/${osBinName}`;
50+
core.info(`Downloading from: ${url}`);
51+
52+
await exec.exec('curl', ['-L', '--fail', '-o', binPath, url]);
53+
5154
if (!fs.existsSync(binPath)) {
52-
core.setFailed("Download failed, couldn't find the binary on disk");
53-
return;
55+
throw new Error("Download failed, couldn't find the binary on disk");
5456
}
5557

5658
if (process.env.RUNNER_OS !== 'Windows') {
@@ -70,35 +72,43 @@ async function addBinToPath(binPath) {
7072
}
7173

7274
async function run() {
73-
try {
74-
const osBin = core.getInput('binary') || getOsBinName();
75-
if (osBin === "unknown") {
76-
core.setFailed("Invalid os binary determination, try setting it specifically using 'binary'");
77-
return;
78-
}
79-
80-
let version = core.getInput('version') || DEFAULT_FUNC_VERSION;
81-
const destination = core.getInput('destination') || process.cwd();
82-
let bin = core.getInput('name') || 'func';
83-
if (process.env.RUNNER_OS === 'Windows' && !bin.endsWith('.exe')) {
84-
bin += '.exe';
85-
}
75+
const osBinName = core.getInput('binary') || getOsBinName();
76+
if (osBinName === "unknown") {
77+
core.setFailed("Invalid os binary determination, try setting it specifically using 'binary'");
78+
return;
79+
}
8680

87-
version = smartVersionUpdate(version);
88-
if (!version) return;
81+
const versionInput = core.getInput('version') || DEFAULT_FUNC_VERSION;
82+
const destination = core.getInput('destination') || process.cwd();
83+
const binarySource = core.getInput('binarySource') || DEFAULT_BINARY_SOURCE;
84+
let bin = core.getInput('name') || 'func';
85+
if (process.env.RUNNER_OS === 'Windows' && !bin.endsWith('.exe')) {
86+
bin += '.exe';
87+
}
8988

90-
const url = `https://github.com/knative/func/releases/download/${version}/${osBin}`;
91-
core.info(`URL: ${url}`);
89+
let version;
90+
try {
91+
version = smartVersionUpdate(versionInput);
92+
} catch (error) {
93+
core.setFailed(error.message);
94+
return;
95+
}
9296

93-
const fullPathBin = path.resolve(destination, bin);
97+
if (!fs.existsSync(destination)) {
98+
fs.mkdirSync(destination, { recursive: true });
99+
}
94100

95-
await cmdConstructAndRun(url, fullPathBin);
96-
await addBinToPath(fullPathBin);
97-
await exec.exec(fullPathBin, ['version']);
101+
const fullPathBin = path.resolve(destination, bin);
98102

103+
try {
104+
await downloadFuncBinary(version, osBinName, fullPathBin, binarySource);
99105
} catch (error) {
100-
core.setFailed(error.message);
106+
core.setFailed(`Download failed: ${error.message}`);
107+
return;
101108
}
109+
110+
await addBinToPath(fullPathBin);
111+
await exec.exec(fullPathBin, ['version']);
102112
}
103113

104114
run();

node_modules/.package-lock.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.0.0",
44
"description": "GitHub Action to download and setup the func CLI",
55
"dependencies": {
6-
"@actions/core": "^1.11.1",
7-
"@actions/exec": "^1.1.1"
6+
"@actions/core": "~1.11.1",
7+
"@actions/exec": "~1.1.1"
88
}
99
}

0 commit comments

Comments
 (0)