diff --git a/actions/setup/README.md b/actions/setup/README.md index 6399a3d..2f67a5a 100644 --- a/actions/setup/README.md +++ b/actions/setup/README.md @@ -26,6 +26,25 @@ Pre-requisites: Example usage: +```yaml +on: + workflow_dispatch: {} +jobs: + demo-setup: + runs-on: ubuntu-latest + steps: + - name: Install Teleport + uses: teleport-actions/setup@v1 + with: + # specify version as "auto" and provide the address of your Teleport + # proxy using the "proxy" input. + version: auto + proxy: example.teleport.sh:443 + - run: tsh # tsh, tbot and tctl will now be available +``` + +You can also specify a particular version of the Teleport binaries to install: + ```yaml on: workflow_dispatch: {} diff --git a/actions/setup/action.yml b/actions/setup/action.yml index acc0230..8abc27d 100644 --- a/actions/setup/action.yml +++ b/actions/setup/action.yml @@ -3,7 +3,10 @@ description: 'Installs `teleport`, `tsh`, `tbot` and `tctl`.' inputs: version: required: true - description: 'Specify the Teleport version without the preceding "v"' + description: 'Specify the Teleport version without the preceding "v" or specify "auto" to use the version indicated by your Teleport cluster.' + proxy: + description: 'Specify the address of your Teleport Proxy, required if version is set to "auto".' + required: false enterprise: required: false default: 'false' diff --git a/actions/setup/package.json b/actions/setup/package.json index a547744..6e7c3b4 100644 --- a/actions/setup/package.json +++ b/actions/setup/package.json @@ -1,6 +1,6 @@ { "name": "setup", - "version": "1.0.6", + "version": "1.1.0", "license": "Apache-2.0", "repository": "https://github.com/teleport-actions/setup.git", "scripts": { diff --git a/actions/setup/src/index.ts b/actions/setup/src/index.ts index 0b295c1..cfeed68 100644 --- a/actions/setup/src/index.ts +++ b/actions/setup/src/index.ts @@ -45,6 +45,7 @@ function versionString( interface Inputs { version: string; enterprise: boolean; + proxyAddr: string; } function getInputs(): Inputs { @@ -52,28 +53,59 @@ function getInputs(): Inputs { if (version === '') { throw new Error("'version' input must be non-empty"); } - if (version.startsWith('v')) { - throw new Error("'version' input should not be prefixed with 'v'"); - } - const versionRegex = - /^([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$/i; - - if (!versionRegex.test(version)) { - throw new Error( - "incorrect 'version' specified, it should include all parts of the version e.g 11.0.1" - ); - } const enterprise = core.getBooleanInput('enterprise'); + const proxyAddr = core.getInput('proxy'); + + if (version !== 'auto') { + if (version.startsWith('v')) { + throw new Error("'version' input should not be prefixed with 'v'"); + } + const versionRegex = + /^([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$/i; + + if (!versionRegex.test(version)) { + throw new Error( + "incorrect 'version' specified, it should include all parts of the version e.g 11.0.1 or be set to 'auto'" + ); + } + } else { + if (proxyAddr === '') { + throw new Error( + "'proxy' input must be non-empty when 'version' is set to 'auto'" + ); + } + } return { version, enterprise, + proxyAddr, }; } +async function fetchVersionFromProxy(proxyAddr: string): Promise { + const resp = await fetch(`https://${proxyAddr}/webapi/find`); + const data = await resp.json(); + const version = data?.auto_update?.tools_version; + if (!version) { + throw new Error( + `malformed response from proxy missing version: ${JSON.stringify(data)}` + ); + } + return version; +} + async function run(): Promise { const inputs = getInputs(); + + if (inputs.version === 'auto') { + core.info(`Fetching version from proxy: ${inputs.proxyAddr}`); + const proxyVersion = await fetchVersionFromProxy(inputs.proxyAddr); + core.info(`Fetched version: ${proxyVersion}`); + inputs.version = proxyVersion; + } + const version = versionString(os.platform(), os.arch(), inputs.version); const toolName = inputs.enterprise ? 'teleport-ent' : 'teleport'; core.info(`Installing ${toolName} ${version}`);