Skip to content

Commit 96c687a

Browse files
committed
cleanup & fixes for func action
1 parent e9f9397 commit 96c687a

305 files changed

Lines changed: 134 additions & 395374 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/test-action.yml

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,47 @@
11
name: test-action
2-
on: [pull_request,workflow_dispatch]
2+
on: [pull_request, workflow_dispatch]
33

44
jobs:
5-
func:
5+
test-default:
66
runs-on: ubuntu-latest
77
steps:
8-
- uses: functions-dev/action@main
8+
- uses: actions/checkout@v4
9+
- name: Setup func CLI
10+
uses: ./
11+
- run: func version
12+
13+
test-custom-version:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v4
17+
- name: Setup func CLI with custom version
18+
uses: ./
19+
with:
20+
version: 'v1.19.0'
21+
- run: func version
22+
23+
test-custom-name:
24+
runs-on: ubuntu-latest
25+
steps:
26+
- uses: actions/checkout@v4
27+
- name: Setup func CLI with custom name
28+
uses: ./
29+
with:
30+
name: 'my-func'
31+
- run: my-func version
32+
33+
test-windows:
34+
runs-on: windows-latest
35+
steps:
36+
- uses: actions/checkout@v4
37+
- name: Setup func CLI
38+
uses: ./
39+
- run: func version
40+
41+
test-macos:
42+
runs-on: macos-latest
43+
steps:
44+
- uses: actions/checkout@v4
45+
- name: Setup func CLI
46+
uses: ./
47+
- run: func version

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ By default, uses latest version, but you can specify which one you want using `v
77
`action.yml` -- action yaml with descriptions
88
```yaml
99
name: 'Func Action'
10-
description: 'This action does custom stuff for it is custom'
10+
description: 'Downloads and sets up the func CLI for Functions'
1111
inputs:
1212
name:
1313
description: '(optional) Name of the Function binary. It will be available in the runner under this name(defaults to "func")'

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: 'Func Action'
2-
description: 'This action does custom stuff for it is custom'
2+
description: 'Downloads and sets up the func CLI for Functions'
33
inputs:
44
name:
55
description: '(optional) Name of the Function binary. It will be available in the runner under this name(defaults to "func")'

index.js

Lines changed: 39 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
const core = require('@actions/core');
2-
const exec = require('@actions/exec')
3-
const path = require('path')
2+
const exec = require('@actions/exec');
3+
const path = require('path');
44
const fs = require('fs');
55

6+
// Static version - update manually when new func releases are available
7+
const DEFAULT_FUNC_VERSION = 'knative-v1.20.1';
68

7-
// change this accordingly
8-
const DEFAULT_FUNC_VERSION = 'knative-v1.16.1'
9-
10-
// detect os system in Github Actions and determine binary name
119
function getOsBinName() {
1210
const runnerOS = process.env.RUNNER_OS;
1311
const runnerArch = process.env.RUNNER_ARCH;
@@ -23,102 +21,77 @@ function getOsBinName() {
2321
} else if (runnerOS === 'macOS') {
2422
return runnerArch === 'X64' ? 'func_darwin_amd64' : 'func_darwin_arm64';
2523
} else if (runnerOS === 'Windows') {
26-
return 'func_windows_amd64';
24+
return 'func_windows_amd64.exe';
2725
} else {
2826
return 'unknown';
2927
}
3028
}
3129

32-
// smartVersionParse will check validity of given version and fill in the parts
33-
// to make it correct if possible.
34-
// Ex.: '1.16' or 'v1.16' will return 'v1.16.0'
30+
// Normalizes version to release tag format: knative-vX.Y.Z
31+
// Ex.: '1.16' or 'v1.16' will return 'knative-v1.16.0'
3532
function smartVersionUpdate(version){
36-
versionRegex = /^(?<knprefix>knative-)?(?<prefix>v?)(?<major>\d+)\.(?<minor>\d+)(.(?<patch>\d+))?$/;
33+
const versionRegex = /^(?<knprefix>knative-)?(?<prefix>v?)(?<major>\d+)\.(?<minor>\d+)(.(?<patch>\d+))?$/;
3734
let match = version.match(versionRegex);
38-
if (match){
39-
if (match.groups.knprefix == undefined){
40-
match.groups.knprefix = "";
41-
}
35+
if (match) {
36+
const knprefix = 'knative-';
4237
const prefix = 'v';
43-
if (match.groups.patch == undefined) {
44-
match.groups.patch = 0;
45-
}
46-
return `${match.groups.knprefix}${prefix}${match.groups.major}.${match.groups.minor}.${match.groups.patch}`;
38+
const patch = match.groups.patch ?? 0;
39+
return `${knprefix}${prefix}${match.groups.major}.${match.groups.minor}.${patch}`;
4740
}
4841

4942
core.setFailed(`Invalid version format (${version}). Expected format: "1.16[.X]" or "v1.16[.X]"`);
5043
return undefined;
5144
}
5245

53-
/**
54-
* @param {string} url - Full url to be curled
55-
* @param {string} binPath - Full target path of the binary
56-
*/
57-
// download func, set as executable
58-
async function cmdConstructAndRun(url,binPath){
59-
const cmd = `curl -L -o "${binPath}" "${url}"`;
60-
await exec.exec(cmd);
46+
async function cmdConstructAndRun(url, binPath) {
47+
await exec.exec('curl', ['-L', '-o', binPath, url]);
6148

62-
//check if downloaded successfully
63-
if (!fs.existsSync(binPath)){
49+
if (!fs.existsSync(binPath)) {
6450
core.setFailed("Download failed, couldn't find the binary on disk");
51+
return;
6552
}
6653

67-
await exec.exec(`chmod +x ${binPath}`);
54+
if (process.env.RUNNER_OS !== 'Windows') {
55+
await exec.exec('chmod', ['+x', binPath]);
56+
}
6857
}
6958

70-
/**
71-
* @param {string} binPath - full path to Func binary
72-
* */
73-
async function addBinToPath(binPath){
74-
dir = path.dirname(binPath)
75-
// Write to $GITHUB_PATH, making it available for subsequent steps
59+
async function addBinToPath(binPath) {
60+
const dir = path.dirname(binPath);
7661
fs.appendFileSync(process.env.GITHUB_PATH, `\n${dir}`);
7762

78-
// add only if its not in PATH yet
79-
if (!process.env.PATH.includes(dir)){
63+
if (!process.env.PATH.includes(dir)) {
8064
process.env.PATH = process.env.PATH + path.delimiter + dir;
81-
core.info(`dir ${dir} added to $PATH`);
65+
core.info(`${dir} added to PATH`);
8266
}
83-
}
67+
}
8468

85-
// -------------------------------------------------------------------------- \\
86-
async function run(){
69+
async function run() {
8770
try {
88-
89-
// Fetch value of inputs specified in action.yml or use defaults
90-
91-
// osBin refers to the exact name match of an existing binary available to
92-
// download
9371
const osBin = core.getInput('binary') || getOsBinName();
94-
if (osBin == "unknown"){
72+
if (osBin === "unknown") {
9573
core.setFailed("Invalid os binary determination, try setting it specifically using 'binary'");
74+
return;
9675
}
97-
// version to be downloaded
98-
let version = core.getInput('version') || DEFAULT_FUNC_VERSION
99-
// destination is a directory where to download the Func
76+
77+
let version = core.getInput('version') || DEFAULT_FUNC_VERSION;
10078
const destination = core.getInput('destination') || process.cwd();
101-
// bin refers to the name of the binary (Func) that will be downloaded (this
102-
// is what it will be called)
103-
const bin = core.getInput('name') || 'func';
79+
let bin = core.getInput('name') || 'func';
80+
if (process.env.RUNNER_OS === 'Windows' && !bin.endsWith('.exe')) {
81+
bin += '.exe';
82+
}
10483

10584
version = smartVersionUpdate(version);
85+
if (!version) return;
10686

107-
var url = `https://github.com/knative/func/releases/download/${version}/${osBin}`;
108-
console.log(`URL: ${url}`);
109-
110-
fullPathBin = path.resolve(destination,bin);
87+
const url = `https://github.com/knative/func/releases/download/${version}/${osBin}`;
88+
core.info(`URL: ${url}`);
11189

112-
// download Func
113-
await cmdConstructAndRun(url,fullPathBin);
90+
const fullPathBin = path.resolve(destination, bin);
11491

115-
// add final binary to PATH (directory where the bin is ) and add it to
116-
// GITHUB_PATH so it can be used bo subsequent 'runs'
92+
await cmdConstructAndRun(url, fullPathBin);
11793
await addBinToPath(fullPathBin);
118-
119-
await exec.exec("ls -la")
120-
// run 'func version' as a test
121-
await exec.exec(`${bin} version`);
94+
await exec.exec(fullPathBin, ['version']);
12295

12396
} catch (error) {
12497
core.setFailed(error.message);

0 commit comments

Comments
 (0)