4
0
Fork 0
mirror of https://github.com/Azure/setup-helm.git synced 2025-11-07 13:06:56 +00:00

Removing old versions of run

This commit is contained in:
Asa Gayle 2022-01-14 08:49:29 -05:00
parent 7b95bcc15b
commit e468b13d6c
2 changed files with 92 additions and 373 deletions

View file

@ -11,46 +11,19 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
exports.__esModule = true;
Object.defineProperty(exports, "__esModule", { value: true });
exports.run = exports.findHelm = exports.downloadHelm = exports.walkSync = exports.getStableHelmVersion = exports.getHelmDownloadURL = exports.getExecutableExtension = void 0;
var os = require("os");
var path = require("path");
var util = require("util");
var fs = require("fs");
var semver = require("semver");
var exec = require("@actions/exec");
var toolCache = require("@actions/tool-cache");
var core = require("@actions/core");
var helmToolName = 'helm';
var stableHelmVersion = 'v3.7.2';
var helmAllReleasesUrl = 'https://api.github.com/repos/helm/helm/releases';
const os = require("os");
const path = require("path");
const util = require("util");
const fs = require("fs");
const semver = require("semver");
const exec = require("@actions/exec");
const toolCache = require("@actions/tool-cache");
const core = require("@actions/core");
const helmToolName = 'helm';
const stableHelmVersion = 'v3.7.2';
const helmAllReleasesUrl = 'https://api.github.com/repos/helm/helm/releases';
function getExecutableExtension() {
if (os.type().match(/^Win/)) {
return '.exe';
@ -71,46 +44,38 @@ function getHelmDownloadURL(version) {
}
exports.getHelmDownloadURL = getHelmDownloadURL;
function getStableHelmVersion() {
return __awaiter(this, void 0, void 0, function () {
var downloadPath, responseArray, latestHelmVersion_1, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, toolCache.downloadTool(helmAllReleasesUrl)];
case 1:
downloadPath = _a.sent();
responseArray = JSON.parse(fs.readFileSync(downloadPath, 'utf8').toString().trim());
latestHelmVersion_1 = semver.clean(stableHelmVersion);
responseArray.forEach(function (response) {
if (response && response.tag_name) {
var currentHelmVerison = semver.clean(response.tag_name.toString());
if (currentHelmVerison) {
if (currentHelmVerison.toString().indexOf('rc') == -1 && semver.gt(currentHelmVerison, latestHelmVersion_1)) {
//If current helm version is not a pre release and is greater than latest helm version
latestHelmVersion_1 = currentHelmVerison;
}
}
return __awaiter(this, void 0, void 0, function* () {
try {
const downloadPath = yield toolCache.downloadTool(helmAllReleasesUrl);
const responseArray = JSON.parse(fs.readFileSync(downloadPath, 'utf8').toString().trim());
let latestHelmVersion = semver.clean(stableHelmVersion);
responseArray.forEach(response => {
if (response && response.tag_name) {
let currentHelmVerison = semver.clean(response.tag_name.toString());
if (currentHelmVerison) {
if (currentHelmVerison.toString().indexOf('rc') == -1 && semver.gt(currentHelmVerison, latestHelmVersion)) {
//If current helm version is not a pre release and is greater than latest helm version
latestHelmVersion = currentHelmVerison;
}
});
latestHelmVersion_1 = "v" + latestHelmVersion_1;
return [2 /*return*/, latestHelmVersion_1];
case 2:
error_1 = _a.sent();
core.warning(util.format("Cannot get the latest Helm info from %s. Error %s. Using default Helm version %s.", helmAllReleasesUrl, error_1, stableHelmVersion));
return [3 /*break*/, 3];
case 3: return [2 /*return*/, stableHelmVersion];
}
});
}
}
});
latestHelmVersion = "v" + latestHelmVersion;
return latestHelmVersion;
}
catch (error) {
core.warning(util.format("Cannot get the latest Helm info from %s. Error %s. Using default Helm version %s.", helmAllReleasesUrl, error, stableHelmVersion));
}
return stableHelmVersion;
});
}
exports.getStableHelmVersion = getStableHelmVersion;
var walkSync = function (dir, filelist, fileToFind) {
exports.walkSync = function (dir, filelist, fileToFind) {
var files = fs.readdirSync(dir);
filelist = filelist || [];
files.forEach(function (file) {
if (fs.statSync(path.join(dir, file)).isDirectory()) {
filelist = (0, exports.walkSync)(path.join(dir, file), filelist, fileToFind);
filelist = exports.walkSync(path.join(dir, file), filelist, fileToFind);
}
else {
core.debug(file);
@ -121,79 +86,51 @@ var walkSync = function (dir, filelist, fileToFind) {
});
return filelist;
};
exports.walkSync = walkSync;
function downloadHelm(version) {
return __awaiter(this, void 0, void 0, function () {
var cachedToolpath, helmDownloadPath, exception_1, unzipedHelmPath, helmpath;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!!version) return [3 /*break*/, 2];
return [4 /*yield*/, getStableHelmVersion()];
case 1:
version = _a.sent();
_a.label = 2;
case 2:
cachedToolpath = toolCache.find(helmToolName, version);
if (!!cachedToolpath) return [3 /*break*/, 9];
helmDownloadPath = void 0;
_a.label = 3;
case 3:
_a.trys.push([3, 5, , 6]);
return [4 /*yield*/, toolCache.downloadTool(getHelmDownloadURL(version))];
case 4:
helmDownloadPath = _a.sent();
return [3 /*break*/, 6];
case 5:
exception_1 = _a.sent();
throw new Error(util.format("Failed to download Helm from location", getHelmDownloadURL(version)));
case 6:
fs.chmodSync(helmDownloadPath, '777');
return [4 /*yield*/, toolCache.extractZip(helmDownloadPath)];
case 7:
unzipedHelmPath = _a.sent();
return [4 /*yield*/, toolCache.cacheDir(unzipedHelmPath, helmToolName, version)];
case 8:
cachedToolpath = _a.sent();
_a.label = 9;
case 9:
helmpath = findHelm(cachedToolpath);
if (!helmpath) {
throw new Error(util.format("Helm executable not found in path", cachedToolpath));
}
fs.chmodSync(helmpath, '777');
return [2 /*return*/, helmpath];
return __awaiter(this, void 0, void 0, function* () {
if (!version) {
version = yield getStableHelmVersion();
}
let cachedToolpath = toolCache.find(helmToolName, version);
if (!cachedToolpath) {
let helmDownloadPath;
try {
helmDownloadPath = yield toolCache.downloadTool(getHelmDownloadURL(version));
}
});
catch (exception) {
throw new Error(util.format("Failed to download Helm from location", getHelmDownloadURL(version)));
}
fs.chmodSync(helmDownloadPath, '777');
const unzipedHelmPath = yield toolCache.extractZip(helmDownloadPath);
cachedToolpath = yield toolCache.cacheDir(unzipedHelmPath, helmToolName, version);
}
const helmpath = findHelm(cachedToolpath);
if (!helmpath) {
throw new Error(util.format("Helm executable not found in path", cachedToolpath));
}
fs.chmodSync(helmpath, '777');
return helmpath;
});
}
exports.downloadHelm = downloadHelm;
function getLatestHelmVersion() {
return __awaiter(this, void 0, void 0, function () {
var command, latestHelm, latestHelmErr, options;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
command = "curl -Ls https://api.github.com/repos/helm/helm/releases | grep 'v3.[0-9]*.[0-9]*' | sed -E 's/ .*/helm/helm/releases/tag/tag/(v[0-9.]+)\".*/\\1/g' | head -1 | sed -E 's/.*tag///' | sed -E 's/\".*//'";
latestHelm = "";
latestHelmErr = "";
options = {};
options.listeners = {
stdout: function (data) {
latestHelm += data.toString();
},
stderr: function (data) {
latestHelmErr += data.toString();
}
};
return [4 /*yield*/, exec.exec(command, [], options)];
case 1:
_a.sent();
if (latestHelmErr !== "")
return [2 /*return*/, getStableHelmVersion()];
return [2 /*return*/, latestHelm];
return __awaiter(this, void 0, void 0, function* () {
const command = `curl -Ls https://api.github.com/repos/helm/helm/releases | grep 'v3.[0-9]*.[0-9]*' | sed -E 's/ .*\/helm\/helm\/releases\/tag\/tag\/(v[0-9\.]+)".*/\\1/g' | head -1 | sed -E 's/.*tag\///' | sed -E 's/".*//'`;
let latestHelm = "";
let latestHelmErr = "";
const options = {};
options.listeners = {
stdout: (data) => {
latestHelm += data.toString();
},
stderr: (data) => {
latestHelmErr += data.toString();
}
});
};
yield exec.exec(command, [], options);
if (latestHelmErr !== "")
return getStableHelmVersion();
return latestHelm;
});
}
// isValidVersion checks if verison matches the specified type and is a stable release
@ -205,7 +142,7 @@ function isValidVersion(version, type) {
function findHelm(rootFolder) {
fs.chmodSync(rootFolder, '777');
var filelist = [];
(0, exports.walkSync)(rootFolder, filelist, helmToolName + getExecutableExtension());
exports.walkSync(rootFolder, filelist, helmToolName + getExecutableExtension());
if (!filelist || filelist.length == 0) {
throw new Error(util.format("Helm executable not found in path", rootFolder));
}
@ -215,36 +152,24 @@ function findHelm(rootFolder) {
}
exports.findHelm = findHelm;
function run() {
return __awaiter(this, void 0, void 0, function () {
var version, cachedPath;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
version = core.getInput('version', { 'required': true });
if (!(version.toLocaleLowerCase() === 'latest')) return [3 /*break*/, 2];
return [4 /*yield*/, getLatestHelmVersion()];
case 1:
version = _a.sent();
_a.label = 2;
case 2:
core.debug(util.format("Downloading %s", version));
return [4 /*yield*/, downloadHelm(version)];
case 3:
cachedPath = _a.sent();
try {
if (!process.env['PATH'].startsWith(path.dirname(cachedPath))) {
core.addPath(path.dirname(cachedPath));
}
}
catch (_b) {
//do nothing, set as output variable
}
console.log("Helm tool version: '".concat(version, "' has been cached at ").concat(cachedPath));
core.setOutput('helm-path', cachedPath);
return [2 /*return*/];
return __awaiter(this, void 0, void 0, function* () {
let version = core.getInput('version', { 'required': true });
if (version.toLocaleLowerCase() === 'latest') {
version = yield getLatestHelmVersion();
}
core.debug(util.format("Downloading %s", version));
let cachedPath = yield downloadHelm(version);
try {
if (!process.env['PATH'].startsWith(path.dirname(cachedPath))) {
core.addPath(path.dirname(cachedPath));
}
});
}
catch (_a) {
//do nothing, set as output variable
}
console.log(`Helm tool version: '${version}' has been cached at ${cachedPath}`);
core.setOutput('helm-path', cachedPath);
});
}
exports.run = run;
run()["catch"](core.setFailed);
run().catch(core.setFailed);

View file

@ -1,206 +0,0 @@
// Copyright (c) Microsoft Corporation.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import * as os from 'os';
import * as path from 'path';
import * as util from 'util';
import * as fs from 'fs';
import * as semver from 'semver';
import * as exec from '@actions/exec';
import * as toolCache from '@actions/tool-cache';
import * as core from '@actions/core';
import { graphql } from '@octokit/graphql';
const helmToolName = 'helm';
const stableHelmVersion = 'v3.2.1';
const stableHelm3Version = 'v3.5.3';
const stableHelm2Version = 'v2.17.0';
const LATEST_HELM2_VERSION = '2.*';
const LATEST_HELM3_VERSION = '3.*';
const helmAllReleasesUrl = 'https://api.github.com/repos/helm/helm/releases';
export function getExecutableExtension(): string {
if (os.type().match(/^Win/)) {
return '.exe';
}
return '';
}
export function getHelmDownloadURL(version: string): string {
switch (os.type()) {
case 'Linux':
return util.format('https://get.helm.sh/helm-%s-linux-amd64.zip', version);
case 'Darwin':
return util.format('https://get.helm.sh/helm-%s-darwin-amd64.zip', version);
case 'Windows_NT':
default:
return util.format('https://get.helm.sh/helm-%s-windows-amd64.zip', version);
}
}
export async function getStableHelmVersion(): Promise<string> {
try {
const downloadPath = await toolCache.downloadTool(helmAllReleasesUrl);
const responseArray = JSON.parse(fs.readFileSync(downloadPath, 'utf8').toString().trim());
let latestHelmVersion = semver.clean(stableHelmVersion);
responseArray.forEach(response => {
if (response && response.tag_name) {
let currentHelmVerison = semver.clean(response.tag_name.toString());
if (currentHelmVerison) {
if (currentHelmVerison.toString().indexOf('rc') == -1 && semver.gt(currentHelmVerison, latestHelmVersion)) {
//If current helm version is not a pre release and is greater than latest helm version
latestHelmVersion = currentHelmVerison;
}
}
}
});
latestHelmVersion = "v" + latestHelmVersion;
return latestHelmVersion;
} catch (error) {
core.warning(util.format("Cannot get the latest Helm info from %s. Error %s. Using default Helm version %s.", helmAllReleasesUrl, error, stableHelmVersion));
}
return stableHelmVersion;
}
export var walkSync = function (dir, filelist, fileToFind) {
var files = fs.readdirSync(dir);
filelist = filelist || [];
files.forEach(function (file) {
if (fs.statSync(path.join(dir, file)).isDirectory()) {
filelist = walkSync(path.join(dir, file), filelist, fileToFind);
}
else {
core.debug(file);
if (file == fileToFind) {
filelist.push(path.join(dir, file));
}
}
});
return filelist;
};
export async function downloadHelm(version: string): Promise<string> {
if (!version) { version = await getStableHelmVersion(); }
let cachedToolpath = toolCache.find(helmToolName, version);
if (!cachedToolpath) {
let helmDownloadPath;
try {
helmDownloadPath = await toolCache.downloadTool(getHelmDownloadURL(version));
} catch (exception) {
throw new Error(util.format("Failed to download Helm from location", getHelmDownloadURL(version)));
}
fs.chmodSync(helmDownloadPath, '777');
const unzipedHelmPath = await toolCache.extractZip(helmDownloadPath);
cachedToolpath = await toolCache.cacheDir(unzipedHelmPath, helmToolName, version);
}
const helmpath = findHelm(cachedToolpath);
if (!helmpath) {
throw new Error(util.format("Helm executable not found in path", cachedToolpath));
}
fs.chmodSync(helmpath, '777');
return helmpath;
}
async function getLatestHelmVersionForTest(type: string): Promise<string>{
exec.exec(`curl -Ls https://api.github.com/repos/helm/helm/releases | grep 'v3.[0-9]*.[0-9]*' | sed -E 's/ .*\/helm\/helm\/releases\/tag\/tag\/(v[0-9\.]+)".*/\1/g' | head -1 | sed -E 's/.*tag\///' | sed -E 's/".*//'`)
}
async function getLatestHelmVersionFor(type: string): Promise<string> {
const token = core.getInput('token', { 'required': true });
try {
const { repository } = await graphql(
`{
repository(name: "helm", owner: "helm") {
releases(last: 100) {
nodes {
tagName
}
}
}
}`,
{
headers: {
authorization: `token ${token}`
}
}
);
const releases = repository.releases.nodes.reverse();
let latestValidRelease = releases.find(release => isValidVersion(release.tagName, type));
if (latestValidRelease)
return latestValidRelease.tagName;
} catch (err) {
core.warning(util.format("Error while fetching the latest Helm %s release. Error: %s. Using default Helm version %s.", type, err.toString(), getStableHelmVersionFor(type)));
return getStableHelmVersionFor(type);
}
core.warning(util.format("Could not find stable release for Helm %s. Using default Helm version %s.", type, getStableHelmVersionFor(type)));
return getStableHelmVersionFor(type);
}
function getStableHelmVersionFor(type: string) {
return type === "v2" ? stableHelm2Version : stableHelm3Version;
}
// isValidVersion checks if verison matches the specified type and is a stable release
function isValidVersion(version: string, type: string): boolean {
if (!version.toLocaleLowerCase().startsWith(type))
return false;
return version.indexOf('rc') == -1;
}
export function findHelm(rootFolder: string): string {
fs.chmodSync(rootFolder, '777');
var filelist: string[] = [];
walkSync(rootFolder, filelist, helmToolName + getExecutableExtension());
if (!filelist || filelist.length == 0) {
throw new Error(util.format("Helm executable not found in path", rootFolder));
}
else {
return filelist[0];
}
}
export async function run() {
let version = core.getInput('version', { 'required': true });
if (process.env['HELM_INSTALLER_LEGACY_VERSIONING'] == 'true') {
if (version.toLocaleLowerCase() === 'latest') {
version = await getStableHelmVersion();
} else if (!version.toLocaleLowerCase().startsWith('v')) {
version = 'v' + version;
}
} else {
if (version.toLocaleLowerCase() === 'latest' || version === LATEST_HELM3_VERSION) {
version = await getLatestHelmVersionFor("v3");
} else if (version === LATEST_HELM2_VERSION) {
version = await getLatestHelmVersionFor("v2");
} else if (!version.toLocaleLowerCase().startsWith('v')) {
version = 'v' + version;
}
}
core.debug(util.format("Downloading %s", version));
let cachedPath = await downloadHelm(version);
try {
if (!process.env['PATH'].startsWith(path.dirname(cachedPath))) {
core.addPath(path.dirname(cachedPath));
}
}
catch {
//do nothing, set as output variable
}
console.log(`Helm tool version: '${version}' has been cached at ${cachedPath}`);
core.setOutput('helm-path', cachedPath);
}
run().catch(core.setFailed);