Skip to content

Commit 3834416

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

305 files changed

Lines changed: 165 additions & 395394 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: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,54 @@
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+
env:
16+
TEST_VERSION: '1.19.0'
17+
VERSION_OFFSET: 27 # internal version = minor + offset (v1.19 → v0.46)
18+
steps:
19+
- uses: actions/checkout@v4
20+
- name: Setup func CLI with custom version
21+
uses: ./
22+
with:
23+
version: 'v${{ env.TEST_VERSION }}'
24+
- name: Verify version
25+
run: |
26+
MINOR=$(echo "$TEST_VERSION" | cut -d. -f2)
27+
INTERNAL=$((MINOR + VERSION_OFFSET))
28+
func version | grep -q "v0.${INTERNAL}"
29+
30+
test-custom-name:
31+
runs-on: ubuntu-latest
32+
steps:
33+
- uses: actions/checkout@v4
34+
- name: Setup func CLI with custom name
35+
uses: ./
36+
with:
37+
name: 'my-func'
38+
- run: my-func version
39+
40+
test-windows:
41+
runs-on: windows-latest
42+
steps:
43+
- uses: actions/checkout@v4
44+
- name: Setup func CLI
45+
uses: ./
46+
- run: func version
47+
48+
test-macos:
49+
runs-on: macos-latest
50+
steps:
51+
- uses: actions/checkout@v4
52+
- name: Setup func CLI
53+
uses: ./
54+
- run: func version

README.md

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1-
# Functions as Github Action!
2-
Functions' Github Action, so you can get Functions in your workflows!
3-
Automatically determines what os the GH Runner has to get the right binary.
4-
You can change where you want the `func` to be downloaded with `destination` - regardless, it will be in `$PATH`!
5-
By default, uses latest version, but you can specify which one you want using `version`.
1+
# Func Action
2+
3+
GitHub Action to download and setup the func CLI. Automatically detects OS and architecture.
4+
5+
## Usage
66

7-
`action.yml` -- action yaml with descriptions
87
```yaml
9-
name: 'Func Action'
10-
description: 'This action does custom stuff for it is custom'
11-
inputs:
12-
name:
13-
description: '(optional) Name of the Function binary. It will be available in the runner under this name(defaults to "func")'
14-
binary:
15-
description: '(optional) Binary you want to download (exact string expected), otherwise will be determined via the OS of GH Runner'
16-
version:
17-
description: '(optional) Provide version to download. Any version in release pages works https://github.com/knative/func/tags'
18-
destination:
19-
description: '(optional) Provide a path where to move the desired downloaded binary, otherwise cwd is used'
20-
runs:
21-
using: 'node20'
22-
main: 'index.js'
8+
- uses: functions-dev/action@main
9+
with:
10+
version: 'v1.20.0' # optional
11+
name: 'func' # optional
2312
```
13+
14+
## Inputs
15+
16+
| Input | Description | Default |
17+
|-------|-------------|---------|
18+
| `version` | Version to download (e.g. `v1.20.0`) | recent stable |
19+
| `name` | Binary name | `func` |
20+
| `binary` | Specific binary to download | auto-detected |
21+
| `destination` | Download directory | cwd |

action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
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:
5-
description: '(optional) Name of the Function binary. It will be available in the runner under this name(defaults to "func")'
5+
description: '(optional) Name of the binary (defaults to "func")'
66
binary:
77
description: '(optional) Binary you want to download (exact string expected), otherwise will be determined via the OS of GH Runner'
88
version:

index.js

Lines changed: 42 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
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
9+
// Returns the binary name for the current OS/arch from GitHub releases
1110
function getOsBinName() {
1211
const runnerOS = process.env.RUNNER_OS;
1312
const runnerArch = process.env.RUNNER_ARCH;
@@ -23,102 +22,79 @@ function getOsBinName() {
2322
} else if (runnerOS === 'macOS') {
2423
return runnerArch === 'X64' ? 'func_darwin_amd64' : 'func_darwin_arm64';
2524
} else if (runnerOS === 'Windows') {
26-
return 'func_windows_amd64';
25+
return 'func_windows_amd64.exe';
2726
} else {
2827
return 'unknown';
2928
}
3029
}
3130

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'
31+
// Normalizes version to release tag format: knative-vX.Y.Z
32+
// Ex.: '1.16' or 'v1.16' will return 'knative-v1.16.0'
3533
function smartVersionUpdate(version){
36-
versionRegex = /^(?<knprefix>knative-)?(?<prefix>v?)(?<major>\d+)\.(?<minor>\d+)(.(?<patch>\d+))?$/;
34+
const versionRegex = /^(?<knprefix>knative-)?(?<prefix>v?)(?<major>\d+)\.(?<minor>\d+)(.(?<patch>\d+))?$/;
3735
let match = version.match(versionRegex);
38-
if (match){
39-
if (match.groups.knprefix == undefined){
40-
match.groups.knprefix = "";
41-
}
36+
if (match) {
37+
const knprefix = 'knative-';
4238
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}`;
39+
const patch = match.groups.patch ?? 0;
40+
return `${knprefix}${prefix}${match.groups.major}.${match.groups.minor}.${patch}`;
4741
}
4842

4943
core.setFailed(`Invalid version format (${version}). Expected format: "1.16[.X]" or "v1.16[.X]"`);
5044
return undefined;
5145
}
5246

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);
47+
// Downloads binary and makes it executable
48+
async function cmdConstructAndRun(url, binPath) {
49+
await exec.exec('curl', ['-L', '-o', binPath, url]);
6150

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

67-
await exec.exec(`chmod +x ${binPath}`);
56+
if (process.env.RUNNER_OS !== 'Windows') {
57+
await exec.exec('chmod', ['+x', binPath]);
58+
}
6859
}
6960

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
61+
// Adds binary directory to PATH for current and subsequent steps
62+
async function addBinToPath(binPath) {
63+
const dir = path.dirname(binPath);
7664
fs.appendFileSync(process.env.GITHUB_PATH, `\n${dir}`);
7765

78-
// add only if its not in PATH yet
79-
if (!process.env.PATH.includes(dir)){
66+
if (!process.env.PATH.includes(dir)) {
8067
process.env.PATH = process.env.PATH + path.delimiter + dir;
81-
core.info(`dir ${dir} added to $PATH`);
68+
core.info(`${dir} added to PATH`);
8269
}
83-
}
70+
}
8471

85-
// -------------------------------------------------------------------------- \\
86-
async function run(){
72+
async function run() {
8773
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
9374
const osBin = core.getInput('binary') || getOsBinName();
94-
if (osBin == "unknown"){
75+
if (osBin === "unknown") {
9576
core.setFailed("Invalid os binary determination, try setting it specifically using 'binary'");
77+
return;
9678
}
97-
// version to be downloaded
98-
let version = core.getInput('version') || DEFAULT_FUNC_VERSION
99-
// destination is a directory where to download the Func
79+
80+
let version = core.getInput('version') || DEFAULT_FUNC_VERSION;
10081
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';
82+
let bin = core.getInput('name') || 'func';
83+
if (process.env.RUNNER_OS === 'Windows' && !bin.endsWith('.exe')) {
84+
bin += '.exe';
85+
}
10486

10587
version = smartVersionUpdate(version);
88+
if (!version) return;
10689

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

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

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'
95+
await cmdConstructAndRun(url, fullPathBin);
11796
await addBinToPath(fullPathBin);
118-
119-
await exec.exec("ls -la")
120-
// run 'func version' as a test
121-
await exec.exec(`${bin} version`);
97+
await exec.exec(fullPathBin, ['version']);
12298

12399
} catch (error) {
124100
core.setFailed(error.message);

0 commit comments

Comments
 (0)