Skip to content

Commit

Permalink
feat: support version-file input (#828)
Browse files Browse the repository at this point in the history
* feat: support version-file

* bump version

* support version in the tests

* fix

* report error

* fix lint

* fix lint

* fix

* make prepare

* update dist: npm ci and npm run prepare
  • Loading branch information
v1v authored Nov 25, 2024
1 parent 230b7ba commit be8e743
Show file tree
Hide file tree
Showing 7 changed files with 299 additions and 41 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,22 @@ Install Updatecli for GitHub Action

**Options**

`version`: Specify the Updatecli version to install. Accepted values are any valid releases such as v0.25.0
- `version`: Specify the Updatecli version to install. Accepted values are any valid releases such as `v0.86.1`.

- `version-file`: The path to a file containing updatecli version. Supported file types are `.updatecli-version` and `.tool-versions`. See more details in [about version-file](#Updatecli-version-file).

### Updatecli version file

If the `version-file` input is specified, the action will extract the version from the file and install it.

Supported files are `.updatecli-version` and `.tool-versions`.
In `.updatecli-version` file, only the version should be specified (e.g., `v0.86.1`).
In `.tool-versions` file, updatecli version should be preceded by the updatecli keyword (e.g., `updatecli v0.86.1`).
The `.tool-versions` file supports version specifications in accordance with Semantic Versioning ([semver](https://semver.org/)).

If both version and version-file inputs are provided, the `version` input will be used.

If the file contains multiple versions, only the first one will be recognized.

### Workflow

Expand Down
4 changes: 2 additions & 2 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ branding:
inputs:
version:
description: 'Specify Updatecli version to use. Accepted values are any Updatecli version'
required: false
default: v0.86.1
version-file:
description: 'The path to the `.updatecli-version` file. See examples of supported syntax in README file'
runs:
using: 'node20'
main: 'dist/index.js'
75 changes: 68 additions & 7 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

67 changes: 63 additions & 4 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,29 @@ import core from '@actions/core'
import tool from '@actions/tool-cache'
import exec from '@actions/exec'
import path from 'node:path'
import fs from 'node:fs'

const DEFAULT_VERSION = `v0.86.1`

// get the Updatecli version from the action inputs
export async function getUpdatecliVersion() {
const versionInput = core.getInput('version', {required: false})
const versionFile = core.getInput('version-file', {required: false})

let version = versionInput
if (!versionInput && !versionFile) {
core.info(`Set default value for version to ${DEFAULT_VERSION}`)
version = DEFAULT_VERSION
}

if (!version && versionFile) {
version = await getVersionFromFileContent(versionFile)
if (!version) {
throw new Error(`No supported version was found in file ${versionFile}`)
}
}
return version
}

export async function updatecliExtract(downloadPath, downloadUrl) {
if (downloadUrl.endsWith('.tar.gz')) {
Expand All @@ -14,9 +37,10 @@ export async function updatecliExtract(downloadPath, downloadUrl) {
}

// download Updatecli retrieve updatecli binary from Github Release
export async function updatecliDownload() {
const version = core.getInput('version')

export async function updatecliDownload(version) {
if (!version) {
throw new Error(`No supported version was found`)
}
const updatecliPackages = [
{
arch: 'x64',
Expand Down Expand Up @@ -93,10 +117,45 @@ export async function updatecliVersion() {

export async function run() {
try {
await updatecliDownload()
const version = await getUpdatecliVersion()
await updatecliDownload(version)
await updatecliVersion()
process.exitCode = core.ExitCode.Success
} catch (error) {
core.setFailed(error.message)
}
}

export async function getVersionFromFileContent(versionFile) {
if (!versionFile) {
return
}

let versionRegExp
const versionFileName = path.basename(versionFile)
if (versionFileName == '.tool-versions') {
versionRegExp = /^(updatecli\s+)(?:\S*-)?(?<version>v(\d+)(\.\d+)(\.\d+))$/m
} else if (versionFileName) {
versionRegExp = /(?<version>(v\d+\S*))(\s|$)/
} else {
return
}

try {
const content = fs.readFileSync(versionFile).toString().trim()
let fileContent = ''
if (content.match(versionRegExp)?.groups?.version) {
fileContent = content.match(versionRegExp)?.groups?.version
}
if (!fileContent) {
return
}
core.debug(`Version from file '${fileContent}'`)
return fileContent
} catch (error) {
if (error.code === 'ENOENT') {
return
}
throw error
}
}
Loading

0 comments on commit be8e743

Please sign in to comment.