From 39fc719fd98136d2324823444de62befdcebe681 Mon Sep 17 00:00:00 2001 From: Deepak Sattiraju Date: Tue, 10 Sep 2019 15:40:21 +0530 Subject: [PATCH] Port from k8s-actions --- action.yml | 15 +++++++++ lib/run.js | 81 +++++++++++++++++++++++++++++++++++++++++++++++ package-lock.json | 71 +++++++++++++++++++++++++++++++++++++++++ package.json | 24 ++++++++++++++ src/run.ts | 77 ++++++++++++++++++++++++++++++++++++++++++++ tsconfig.json | 9 ++++++ 6 files changed, 277 insertions(+) create mode 100644 action.yml create mode 100644 lib/run.js create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 src/run.ts create mode 100644 tsconfig.json diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..1d08c1e --- /dev/null +++ b/action.yml @@ -0,0 +1,15 @@ +name: 'Kubectl tool installer' +description: 'Install a specific version of kubectl binary. Acceptable values are latest or any semantic version string like 1.15.0' +inputs: + version: + description: 'Version of kubectl' + required: true + default: 'latest' +outputs: + kubectl-path: + description: 'Path to the cached kubectl binary' +branding: + color: 'blue' +runs: + using: 'node12' + main: 'lib/run.js' diff --git a/lib/run.js b/lib/run.js new file mode 100644 index 0000000..bb5bf12 --- /dev/null +++ b/lib/run.js @@ -0,0 +1,81 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const os = require("os"); +const path = require("path"); +const util = require("util"); +const fs = require("fs"); +const toolCache = require("@actions/tool-cache"); +const core = require("@actions/core"); +const kubectlToolName = 'kubectl'; +const stableKubectlVersion = 'v1.15.0'; +const stableVersionUrl = 'https://storage.googleapis.com/kubernetes-release/release/stable.txt'; +function getExecutableExtension() { + if (os.type().match(/^Win/)) { + return '.exe'; + } + return ''; +} +function getkubectlDownloadURL(version) { + switch (os.type()) { + case 'Linux': + return util.format('https://storage.googleapis.com/kubernetes-release/release/%s/bin/linux/amd64/kubectl', version); + case 'Darwin': + return util.format('https://storage.googleapis.com/kubernetes-release/release/%s/bin/darwin/amd64/kubectl', version); + case 'Windows_NT': + default: + return util.format('https://storage.googleapis.com/kubernetes-release/release/%s/bin/windows/amd64/kubectl.exe', version); + } +} +function getStableKubectlVersion() { + return __awaiter(this, void 0, void 0, function* () { + return toolCache.downloadTool(stableVersionUrl).then((downloadPath) => { + let version = fs.readFileSync(downloadPath, 'utf8').toString().trim(); + if (!version) { + version = stableKubectlVersion; + } + return version; + }, (error) => { + core.debug(error); + core.warning('GetStableVersionFailed'); + return stableKubectlVersion; + }); + }); +} +function downloadKubectl(version) { + return __awaiter(this, void 0, void 0, function* () { + let cachedToolpath = toolCache.find(kubectlToolName, version); + let kubectlDownloadPath = ''; + if (!cachedToolpath) { + try { + kubectlDownloadPath = yield toolCache.downloadTool(getkubectlDownloadURL(version)); + } + catch (exception) { + throw new Error('DownloadKubectlFailed'); + } + cachedToolpath = yield toolCache.cacheFile(kubectlDownloadPath, kubectlToolName + getExecutableExtension(), kubectlToolName, version); + } + const kubectlPath = path.join(cachedToolpath, kubectlToolName + getExecutableExtension()); + fs.chmodSync(kubectlPath, '777'); + return kubectlPath; + }); +} +function run() { + return __awaiter(this, void 0, void 0, function* () { + let version = core.getInput('version', { 'required': true }); + if (version.toLocaleLowerCase() === 'latest') { + version = yield getStableKubectlVersion(); + } + let cachedPath = yield downloadKubectl(version); + console.log(`Kubectl tool version: '${version}' has been cached at ${cachedPath}`); + core.setOutput('kubectl-path', cachedPath); + }); +} +run().catch(core.setFailed); diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..86ea577 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,71 @@ +{ + "name": "setup-kubectl-action", + "version": "0.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@actions/core": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@actions/core/-/core-1.1.0.tgz", + "integrity": "sha512-KKpo3xzo0Zsikni9tbOsEQkxZBGDsYSJZNkTvmo0gPSXrc98TBOcdTvKwwjitjkjHkreTggWdB1ACiAFVgsuzA==" + }, + "@actions/exec": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@actions/exec/-/exec-1.0.1.tgz", + "integrity": "sha512-nvFkxwiicvpzNiCBF4wFBDfnBvi7xp/as7LE1hBxBxKG2L29+gkIPBiLKMVORL+Hg3JNf07AKRfl0V5djoypjQ==" + }, + "@actions/io": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@actions/io/-/io-1.0.1.tgz", + "integrity": "sha512-rhq+tfZukbtaus7xyUtwKfuiCRXd1hWSfmJNEpFgBQJ4woqPEpsBw04awicjwz9tyG2/MVhAEMfVn664Cri5zA==" + }, + "@actions/tool-cache": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@actions/tool-cache/-/tool-cache-1.1.1.tgz", + "integrity": "sha512-AILekrrj/L4N/5z5TGtUKVie4nKjxDioCgOEymyYxzPhGfjIxfE71tN2VTTpiICEWJ883rPRj2+WinTr1b6yVA==", + "requires": { + "@actions/core": "^1.1.0", + "@actions/exec": "^1.0.1", + "@actions/io": "^1.0.1", + "semver": "^6.1.0", + "typed-rest-client": "^1.4.0", + "uuid": "^3.3.2" + } + }, + "@types/node": { + "version": "12.7.4", + "resolved": "https://registry.npmjs.org/@types/node/-/node-12.7.4.tgz", + "integrity": "sha512-W0+n1Y+gK/8G2P/piTkBBN38Qc5Q1ZSO6B5H3QmPCUewaiXOo2GCAWZ4ElZCcNhjJuBSUSLGFUJnmlCn5+nxOQ==", + "dev": true + }, + "semver": { + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", + "integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==" + }, + "tunnel": { + "version": "0.0.4", + "resolved": "https://registry.npmjs.org/tunnel/-/tunnel-0.0.4.tgz", + "integrity": "sha1-LTeFoVjBdMmhbcLARuxfxfF0IhM=" + }, + "typed-rest-client": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/typed-rest-client/-/typed-rest-client-1.5.0.tgz", + "integrity": "sha512-DVZRlmsfnTjp6ZJaatcdyvvwYwbWvR4YDNFDqb+qdTxpvaVP99YCpBkA8rxsLtAPjBVoDe4fNsnMIdZTiPuKWg==", + "requires": { + "tunnel": "0.0.4", + "underscore": "1.8.3" + } + }, + "underscore": { + "version": "1.8.3", + "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.8.3.tgz", + "integrity": "sha1-Tz+1OxBuYJf8+ctBCfKl6b36UCI=" + }, + "uuid": { + "version": "3.3.3", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.3.tgz", + "integrity": "sha512-pW0No1RGHgzlpHJO1nsVrHKpOEIxkGg1xB+v0ZmdNH5OAeAwzAVrCnI2/6Mtx+Uys6iaylxa+D3g4j63IKKjSQ==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..c71a6c4 --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "setup-kubectl-action", + "version": "0.0.0", + "private": true, + "main": "lib/run.js", + "scripts": { + "build": "tsc --outDir .\\lib\\ --rootDir .\\src\\" + }, + "keywords": [ + "actions", + "node", + "setup" + ], + "author": "GitHub", + "license": "MIT", + "dependencies": { + "@actions/core": "^1.0.0", + "@actions/exec": "^1.0.0", + "@actions/tool-cache": "^1.0.0" + }, + "devDependencies": { + "@types/node": "^12.0.4" + } +} diff --git a/src/run.ts b/src/run.ts new file mode 100644 index 0000000..4ba225d --- /dev/null +++ b/src/run.ts @@ -0,0 +1,77 @@ +import * as os from 'os'; +import * as path from 'path'; +import * as util from 'util'; +import * as fs from 'fs'; + +import * as toolCache from '@actions/tool-cache'; +import * as core from '@actions/core'; + +const kubectlToolName = 'kubectl'; +const stableKubectlVersion = 'v1.15.0'; +const stableVersionUrl = 'https://storage.googleapis.com/kubernetes-release/release/stable.txt'; + +function getExecutableExtension(): string { + if (os.type().match(/^Win/)) { + return '.exe'; + } + return ''; +} + +function getkubectlDownloadURL(version: string): string { + switch (os.type()) { + case 'Linux': + return util.format('https://storage.googleapis.com/kubernetes-release/release/%s/bin/linux/amd64/kubectl', version); + + case 'Darwin': + return util.format('https://storage.googleapis.com/kubernetes-release/release/%s/bin/darwin/amd64/kubectl', version); + + case 'Windows_NT': + default: + return util.format('https://storage.googleapis.com/kubernetes-release/release/%s/bin/windows/amd64/kubectl.exe', version); + + } +} + +async function getStableKubectlVersion(): Promise { + return toolCache.downloadTool(stableVersionUrl).then((downloadPath) => { + let version = fs.readFileSync(downloadPath, 'utf8').toString().trim(); + if (!version) { + version = stableKubectlVersion; + } + return version; + }, (error) => { + core.debug(error); + core.warning('GetStableVersionFailed'); + return stableKubectlVersion; + }); +} + +async function downloadKubectl(version: string): Promise { + let cachedToolpath = toolCache.find(kubectlToolName, version); + let kubectlDownloadPath = ''; + if (!cachedToolpath) { + try { + kubectlDownloadPath = await toolCache.downloadTool(getkubectlDownloadURL(version)); + } catch (exception) { + throw new Error('DownloadKubectlFailed'); + } + + cachedToolpath = await toolCache.cacheFile(kubectlDownloadPath, kubectlToolName + getExecutableExtension(), kubectlToolName, version); + } + + const kubectlPath = path.join(cachedToolpath, kubectlToolName + getExecutableExtension()); + fs.chmodSync(kubectlPath, '777'); + return kubectlPath; +} + +async function run() { + let version = core.getInput('version', { 'required': true }); + if (version.toLocaleLowerCase() === 'latest') { + version = await getStableKubectlVersion(); + } + let cachedPath = await downloadKubectl(version); + console.log(`Kubectl tool version: '${version}' has been cached at ${cachedPath}`); + core.setOutput('kubectl-path', cachedPath); +} + +run().catch(core.setFailed); \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..7a5d8be --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "target": "ES6", + "module": "commonjs" + }, + "exclude": [ + "node_modules" + ] +} \ No newline at end of file