Skip to content
Merged
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
88 changes: 81 additions & 7 deletions .github/workflows/test-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
os: [ubuntu-latest,windows-latest,macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v7
- name: Setup func CLI
uses: ./
- run: func version
Expand All @@ -19,7 +19,7 @@ jobs:
TEST_VERSION: '1.18.0'
VERSION_OFFSET: 27 # internal version = minor + offset (v1.19 → v0.46)
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v7
- name: Setup func CLI with custom version
uses: ./
with:
Expand All @@ -33,7 +33,7 @@ jobs:
test-nightly-version:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v7
- name: Setup func CLI with nightly version
uses: ./
with:
Expand All @@ -43,7 +43,7 @@ jobs:
test-custom-name:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v7
- name: Setup func CLI with custom name
uses: ./
with:
Expand All @@ -53,7 +53,7 @@ jobs:
test-custom-destination:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v7
- name: Setup func CLI with custom destination
uses: ./
with:
Expand All @@ -65,17 +65,91 @@ jobs:
test-custom-binary-source:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v7
- name: Setup func CLI with custom binarySource URL
uses: ./
with:
binarySource: 'https://github.com/knative/func/releases/download/knative-v1.19.0/func_linux_amd64'
- run: func version

test-binary-source-tar-gz:
strategy:
matrix:
include:
- { os: ubuntu-latest, bin: func_linux_amd64 }
- { os: macos-latest, bin: func_darwin_arm64 }
- { os: windows-latest, bin: func_windows_amd64.exe }
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v7
- name: Pack a func release binary into a tar.gz
id: pack
shell: bash
working-directory: ${{ runner.temp }}
run: |
mkdir bin
curl -L --fail -o bin/func "https://github.com/knative/func/releases/download/knative-v1.19.0/${{ matrix.bin }}"
tar -czf func.tar.gz bin/func
dir="$PWD"
if [ "$RUNNER_OS" = Windows ]; then dir="/$(cygpath -m "$PWD")"; fi
echo "url=file://$dir/func.tar.gz" >> "$GITHUB_OUTPUT"
- name: Setup func CLI from a tar.gz binarySource
uses: ./
with:
binarySource: ${{ steps.pack.outputs.url }}
binarySubpath: 'bin/func'
- run: func version

test-binary-source-tar-gz-errors:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- name: Pack a func release binary into a tar.gz
working-directory: ${{ runner.temp }}
run: |
mkdir bin
curl -L --fail -o bin/func "https://github.com/knative/func/releases/download/knative-v1.19.0/func_linux_amd64"
tar -czf func.tar.gz bin/func
- name: tar.gz without binarySubpath (should fail)
id: no-subpath
uses: ./
with:
binarySource: 'file://${{ runner.temp }}/func.tar.gz'
destination: '${{ runner.temp }}/out'
continue-on-error: true
- name: binarySubpath naming a directory (should fail)
id: dir-subpath
uses: ./
with:
binarySource: 'file://${{ runner.temp }}/func.tar.gz'
binarySubpath: 'bin'
destination: '${{ runner.temp }}/out'
continue-on-error: true
- name: binarySubpath with a plain binary (should fail)
id: plain-with-subpath
uses: ./
with:
binarySource: 'file://${{ runner.temp }}/bin/func'
binarySubpath: 'bin/func'
destination: '${{ runner.temp }}/out'
continue-on-error: true
- name: Verify every case failed and left no func behind
run: |
for outcome in "${{ steps.no-subpath.outcome }}" "${{ steps.dir-subpath.outcome }}" "${{ steps.plain-with-subpath.outcome }}"; do
if [ "$outcome" != "failure" ]; then
echo "Expected every case to fail, got: $outcome"
exit 1
fi
done
if [ -e "${{ runner.temp }}/out/func" ]; then
echo "Expected no func at the destination after failed runs"
exit 1
fi

test-invalid-version:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v7
- name: Setup func CLI with non-existing version (should fail)
id: invalid-version
uses: ./
Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,17 @@ GitHub Action to download and setup the func CLI. Automatically detects OS and a
| `name` | Filename for downloaded binary | `func` |
| `binary` | Specific binary to download from GitHub release | auto-detected |
| `destination` | Download directory | cwd |
| `binarySource` | Full URL for the func binary | empty (uses GitHub releases) |
| `binarySource` | Full URL for the func binary, or for a tar.gz holding it | empty (uses GitHub releases) |
| `binarySubpath` | Path of the binary inside a tar.gz `binarySource` | empty |

## Binary from a tar.gz

When the download is a tar.gz, the action unpacks it and takes the binary at
`binarySubpath`. It tells a tar.gz by its content, not by the URL.

```yaml
- uses: functions-dev/action@main
with:
binarySource: 'https://example.com/func-linux-amd64.tar.gz'
binarySubpath: 'bin/func'
```
4 changes: 3 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ inputs:
destination:
description: '(optional) Path where to move the desired downloaded binary, otherwise cwd is used'
binarySource:
description: '(optional) Full URL for downloading the binary. Overrides version if provided. Must be curl-able returning the func binary'
description: '(optional) Full URL for downloading the binary. Overrides version if provided. Must be curl-able returning the func binary, or a tar.gz holding it (then set binarySubpath)'
binarySubpath:
description: '(optional) Path of the binary inside a tar.gz binarySource, e.g. "bin/func". Required when the download is a tar.gz'
runs:
using: 'node20'
main: 'index.js'
57 changes: 53 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const core = require('@actions/core');
const exec = require('@actions/exec');
const path = require('path');
const fs = require('fs');
const os = require('os');

// Using latest as default
const DEFAULT_VERSION = 'latest';
Expand Down Expand Up @@ -82,16 +83,64 @@ function resolveDownloadUrl(version, binName) {

async function downloadFuncBinary(url, binPath) {
core.info(`Downloading from: ${url}`);
await exec.exec('curl', ['-L', '--fail', '-o', binPath, url]);

if (!fs.existsSync(binPath)) {
throw new Error("Download failed, couldn't find the binary on disk");
// Work in a temp folder, so that only the binary ever lands at binPath.
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'func-'));
try {
await exec.exec('curl', ['-L', '--fail', '-o', 'download', url], { cwd: dir });
if (!fs.existsSync(path.join(dir, 'download'))) {
throw new Error("Download failed, couldn't find the binary on disk");
}
fs.copyFileSync(await binaryInDownload(dir), binPath);
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
if (process.env.RUNNER_OS !== 'Windows') {
await exec.exec('chmod', ['+x', binPath]);
}
}

// A tar.gz starts with the gzip magic bytes; no func binary (ELF, Mach-O, PE) does.
function isGzip(file) {
const fd = fs.openSync(file, 'r');
try {
const magic = Buffer.alloc(2);
fs.readSync(fd, magic, 0, 2, 0);
return magic[0] === 0x1f && magic[1] === 0x8b;
} finally {
fs.closeSync(fd);
}
}

// Returns the path of the binary in dir/download: the download itself, or
// the file at binarySubpath when the download is a tar.gz.
async function binaryInDownload(dir) {
const download = path.join(dir, 'download');
const subpath = core.getInput('binarySubpath');
if (!isGzip(download)) {
if (subpath) {
throw new Error('binarySubpath is set, but the download is not a tar.gz archive');
}
return download;
}
if (!subpath) {
throw new Error('The download is a tar.gz archive. Set binarySubpath to the path of the binary inside it');
}

core.info(`Extracting ${subpath} from the tar.gz archive`);
const archive = path.join(dir, 'archive');
fs.mkdirSync(archive);
// Relative names only, so a GNU tar on Windows never reads the "C:"
// of a path as a remote host.
await exec.exec('tar', ['-xzf', 'download', '-C', 'archive'], { cwd: dir });
// path.join resolves "..", so a subpath that leaves the archive ends up
// outside this folder.
const bin = path.join(archive, subpath);
if (!bin.startsWith(archive + path.sep) || !fs.statSync(bin, { throwIfNoEntry: false })?.isFile()) {
throw new Error(`Couldn't find a file at ${subpath} in the tar.gz archive`);
}
return bin;
}

function addBinToPath(binPath) {
const dir = path.dirname(binPath);
fs.appendFileSync(process.env.GITHUB_PATH, `\n${dir}`);
Expand Down
Loading