-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
128 lines (106 loc) · 4.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const core = require('@actions/core');
const exec = require('@actions/exec')
const path = require('path')
const fs = require('fs');
// change this accordingly
const DEFAULT_FUNC_VERSION = 'knative-v1.16.1'
// detect os system in Github Actions and determine binary name
function getOsBinName() {
const runnerOS = process.env.RUNNER_OS;
const runnerArch = process.env.RUNNER_ARCH;
if (runnerOS === 'Linux') {
switch (runnerArch) {
case 'X64': return 'func_linux_amd64';
case 'ARM64': return 'func_linux_arm64';
case 'PPC64LE': return 'func_linux_ppc64le';
case 'S390X': return 'func_linux_s390x';
default: return 'unknown';
}
} else if (runnerOS === 'macOS') {
return runnerArch === 'X64' ? 'func_darwin_amd64' : 'func_darwin_arm64';
} else if (runnerOS === 'Windows') {
return 'func_windows_amd64';
} else {
return 'unknown';
}
}
// smartVersionParse will check validity of given version and fill in the parts
// to make it correct if possible.
// Ex.: '1.16' or 'v1.16' will return 'v1.16.0'
function smartVersionUpdate(version){
versionRegex = /^(?<knprefix>knative-)?(?<prefix>v?)(?<major>\d+)\.(?<minor>\d+)(.(?<patch>\d+))?$/;
let match = version.match(versionRegex);
if (match){
if (match.groups.knprefix == undefined){
match.groups.knprefix = "";
}
const prefix = 'v';
if (match.groups.patch == undefined) {
match.groups.patch = 0;
}
return `${match.groups.knprefix}${prefix}${match.groups.major}.${match.groups.minor}.${match.groups.patch}`;
}
core.setFailed(`Invalid version format (${version}). Expected format: "1.16[.X]" or "v1.16[.X]"`);
return undefined;
}
/**
* @param {string} url - Full url to be curled
* @param {string} binPath - Full target path of the binary
*/
// download func, set as executable
async function cmdConstructAndRun(url,binPath){
const cmd = `curl -L -o "${binPath}" "${url}"`;
await exec.exec(cmd);
//check if downloaded successfully
if (!fs.existsSync(binPath)){
core.setFailed("Download failed, couldn't find the binary on disk");
}
await exec.exec(`chmod +x ${binPath}`);
}
/**
* @param {string} binPath - full path to Func binary
* */
async function addBinToPath(binPath){
dir = path.dirname(binPath)
// Write to $GITHUB_PATH, making it available for subsequent steps
fs.appendFileSync(process.env.GITHUB_PATH, `\n${dir}`);
// add only if its not in PATH yet
if (!process.env.PATH.includes(dir)){
process.env.PATH = process.env.PATH + path.delimiter + dir;
core.info(`dir ${dir} added to $PATH`);
}
}
// -------------------------------------------------------------------------- \\
async function run(){
try {
// Fetch value of inputs specified in action.yml or use defaults
// osBin refers to the exact name match of an existing binary available to
// download
const osBin = core.getInput('binary') || getOsBinName();
if (osBin == "unknown"){
core.setFailed("Invalid os binary determination, try setting it specifically using 'binary'");
}
// version to be downloaded
let version = core.getInput('version') || DEFAULT_FUNC_VERSION
// destination is a directory where to download the Func
const destination = core.getInput('destination') || process.cwd();
// bin refers to the name of the binary (Func) that will be downloaded (this
// is what it will be called)
const bin = core.getInput('name') || 'func';
version = smartVersionUpdate(version);
var url = `https://github.com/knative/func/releases/download/${version}/${osBin}`;
console.log(`URL: ${url}`);
fullPathBin = path.resolve(destination,bin);
// download Func
await cmdConstructAndRun(url,fullPathBin);
// add final binary to PATH (directory where the bin is ) and add it to
// GITHUB_PATH so it can be used bo subsequent 'runs'
await addBinToPath(fullPathBin);
await exec.exec("ls -la")
// run 'func version' as a test
await exec.exec(`${bin} version`);
} catch (error) {
core.setFailed(error.message);
}
}
run();