feat: add input for bun-version-file

This commit is contained in:
Ade Hery Shopyan 2024-04-04 17:19:34 +07:00 committed by GitHub
parent 8f24390df0
commit f83767c0cf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 116 additions and 16 deletions

View file

@ -85,6 +85,7 @@ jobs:
echo "Expected version to be 1.1.0, got ${{ steps.bun.outputs.version }}" echo "Expected version to be 1.1.0, got ${{ steps.bun.outputs.version }}"
exit 1 exit 1
fi fi
setup-bun-from-tool-versions: setup-bun-from-tool-versions:
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
strategy: strategy:
@ -99,10 +100,51 @@ jobs:
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Setup package.json
- name: Setup .tool-versions
shell: bash shell: bash
run: | run: |
echo "bun ${{ matrix.content }}" > .tool-versions echo "${{ matrix.content }}" > .tool-versions
- name: Setup Bun
uses: ./
- name: Run Bun
id: bun
shell: bash
run: |
bun --version
echo "version=$(bun --version)" >> $GITHUB_OUTPUT
- name: Check version
shell: bash
run: |
if [[ "${{ steps.bun.outputs.version }}" == "1.1.0" ]]; then
echo "Version is 1.1.0"
else
echo "Expected version to be 1.1.0, got ${{ steps.bun.outputs.version }}"
exit 1
fi
setup-bun-from-bumrc:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-latest
- macos-latest
- windows-latest
content:
- "1.1.0"
- "latest"
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup .bumrc
shell: bash
run: |
echo "${{ matrix.content }}" > .bumrc
- name: Setup Bun - name: Setup Bun
uses: ./ uses: ./

View file

@ -10,6 +10,14 @@ Download, install, and setup [Bun](https://bun.sh) in GitHub Actions.
bun-version: latest bun-version: latest
``` ```
## Using version file
```yaml
- uses: oven-sh/setup-bun@v1
with:
bun-version-file: '.bumrc'
```
### Using a custom NPM registry ### Using a custom NPM registry
```yaml ```yaml
@ -34,12 +42,13 @@ In most cases, you shouldn't need to use the [setup-node](https://github.com/act
## Inputs ## Inputs
| Name | Description | Default | Examples | | Name | Description | Default | Examples |
| -------------- | -------------------------------------------------- | ----------- | ------------------------------- | | ------------------- | ------------------------------------------------------ | ----------- | ------------------------------- |
| `bun-version` | The version of Bun to download and install. | `latest` | `canary`, `1.0.0`, `1.0.x` | | `bun-version` | The version of Bun to download and install. | `latest` | `canary`, `1.0.0`, `1.0.x` |
| `registry-url` | Registry URL where some private package is stored. | `undefined` | `"https://npm.pkg.github.com/"` | | `bun-version-file` | The version of Bun to download and install from file. | `undefined` | `canary`, `1.0.0`, `1.0.x` |
| `scope` | Scope for private packages. | `undefined` | `"@foo"`, `"@orgname"` | | `registry-url` | Registry URL where some private package is stored. | `undefined` | `"https://npm.pkg.github.com/"` |
| `no-cache` | Disable caching of the downloaded executable. | `false` | `true`, `false` | | `scope` | Scope for private packages. | `undefined` | `"@foo"`, `"@orgname"` |
| `no-cache` | Disable caching of the downloaded executable. | `false` | `true`, `false` |
## Outputs ## Outputs

View file

@ -8,6 +8,9 @@ inputs:
bun-version: bun-version:
description: 'The version of Bun to install. (e.g. "latest", "canary", "1.0.0", "1.0.x", <sha>)' description: 'The version of Bun to install. (e.g. "latest", "canary", "1.0.0", "1.0.x", <sha>)'
required: false required: false
bun-version-file:
description: 'The version of Bun to install from file. (e.g. ".bumrc")'
required: false
registry-url: registry-url:
required: false required: false
description: "The URL of the package registry to use for installing Bun. Set the $BUN_AUTH_TOKEN environment variable to authenticate with the registry." description: "The URL of the package registry to use for installing Bun. Set the $BUN_AUTH_TOKEN environment variable to authenticate with the registry."

View file

@ -14,12 +14,12 @@ if (!process.env.RUNNER_TEMP) {
process.env.RUNNER_TEMP = tmpdir(); process.env.RUNNER_TEMP = tmpdir();
} }
function readVersionFromPackageJson(): string | undefined { function readVersionFromPackageJson(file: string): string | undefined {
const cwd = process.env.GITHUB_WORKSPACE; const cwd = process.env.GITHUB_WORKSPACE;
if (!cwd) { if (!cwd) {
return; return;
} }
const path = join(cwd, "package.json"); const path = join(cwd, file);
try { try {
if (!existsSync(path)) { if (!existsSync(path)) {
return; return;
@ -32,16 +32,16 @@ function readVersionFromPackageJson(): string | undefined {
return version; return version;
} catch (error) { } catch (error) {
const { message } = error as Error; const { message } = error as Error;
warning(`Failed to read package.json: ${message}`); warning(`Failed to read ${file}: ${message}`);
} }
} }
function readVersionFromToolVersions(): string | undefined { function readVersionFromToolVersions(file: string): string | undefined {
const cwd = process.env.GITHUB_WORKSPACE; const cwd = process.env.GITHUB_WORKSPACE;
if (!cwd) { if (!cwd) {
return; return;
} }
const path = join(cwd, ".tool-versions"); const path = join(cwd, file);
try { try {
if (!existsSync(path)) { if (!existsSync(path)) {
return; return;
@ -52,15 +52,61 @@ function readVersionFromToolVersions(): string | undefined {
return match?.groups?.version; return match?.groups?.version;
} catch (error) { } catch (error) {
const { message } = error as Error; const { message } = error as Error;
warning(`Failed to read .tool-versions: ${message}`); warning(`Failed to read ${file}: ${message}`);
}
}
function readVersionFromBumrc(file: string): string | undefined {
const cwd = process.env.GITHUB_WORKSPACE;
if (!cwd) {
return;
}
const path = join(cwd, file);
try {
if (!existsSync(path)) {
return;
}
const match = readFileSync(path, "utf8");
return JSON.parse(match);
} catch (error) {
const { message } = error as Error;
warning(`Failed to read ${file}: ${message}`);
}
}
function readVersionFromFile(): string | undefined {
const cwd = process.env.GITHUB_WORKSPACE;
if (!cwd) {
return;
}
const file = getInput("bun-version-file");
const path = join(cwd, file);
try {
if (!existsSync(path)) {
return;
}
if (file === "package.json") {
return readVersionFromPackageJson(file)
} else if (file === ".tool-versions") {
return readVersionFromToolVersions(file)
} else if (file === ".bumrc") {
return readVersionFromBumrc(file)
} else {
warning(`Not allowed read version from ${file}`);
}
} catch (error) {
const { message } = error as Error;
warning(`Failed to read ${file}: ${message}`);
} }
} }
runAction({ runAction({
version: version:
getInput("bun-version") || getInput("bun-version") ||
readVersionFromPackageJson() || readVersionFromFile() ||
readVersionFromToolVersions() ||
undefined, undefined,
customUrl: getInput("bun-download-url") || undefined, customUrl: getInput("bun-download-url") || undefined,
registryUrl: getInput("registry-url") || undefined, registryUrl: getInput("registry-url") || undefined,