4
0
Fork 0
mirror of https://github.com/Azure/setup-helm.git synced 2025-11-07 04:56:56 +00:00
This commit is contained in:
Shivam Gupta 2021-03-17 13:39:16 +05:30
parent 93b6cbba4e
commit 41b50dea4b
2 changed files with 53 additions and 92 deletions

View file

@ -34,13 +34,11 @@ const os = __importStar(require("os"));
const path = __importStar(require("path"));
const util = __importStar(require("util"));
const fs = __importStar(require("fs"));
const semver = __importStar(require("semver"));
const toolCache = __importStar(require("@actions/tool-cache"));
const core = __importStar(require("@actions/core"));
const graphql_1 = require("@octokit/graphql");
const helmToolName = 'helm';
const stableHelmVersion = 'v3.2.1';
const helmAllReleasesUrl = 'https://api.github.com/repos/helm/helm/releases';
const LATEST_HELM2_VERSION = '2.*';
const LATEST_HELM3_VERSION = '3.*';
function getExecutableExtension() {
@ -60,32 +58,6 @@ function getHelmDownloadURL(version) {
return util.format('https://get.helm.sh/helm-%s-windows-amd64.zip', version);
}
}
function getStableHelmVersion() {
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 = "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;
});
}
var walkSync = function (dir, filelist, fileToFind) {
var files = fs.readdirSync(dir);
filelist = filelist || [];
@ -105,7 +77,7 @@ var walkSync = function (dir, filelist, fileToFind) {
function downloadHelm(version) {
return __awaiter(this, void 0, void 0, function* () {
if (!version) {
version = yield getStableHelmVersion();
version = yield getLatestHelmVersionFor("v3");
}
let cachedToolpath = toolCache.find(helmToolName, version);
if (!cachedToolpath) {
@ -130,9 +102,8 @@ function downloadHelm(version) {
}
function getLatestHelmVersionFor(type) {
return __awaiter(this, void 0, void 0, function* () {
console.log("Running graphql");
console.log(type);
const token = core.getInput('token', { 'required': true });
try {
const { repository } = yield graphql_1.graphql(`
{
repository(name:"helm", owner:"helm") {
@ -148,10 +119,24 @@ function getLatestHelmVersionFor(type) {
authorization: `token ${token}`
}
});
console.log(repository.releases.nodes);
const releases = repository.releases.nodes.reverse();
releases.forEach(release => {
if (isValidVersion(release.tagName, type))
return release.tagName;
});
}
catch (err) {
core.warning(util.format("Error while fetching the latest Helm %s release. Error: %s. Using defaul Helm version %s.", type, err.toString(), stableHelmVersion));
}
return stableHelmVersion;
});
}
// isValidVersion checks if verison matches the specified type and is a stable release
function isValidVersion(version, type) {
if (!version.startsWith(type))
return false;
return version.indexOf('rc') == -1;
}
function findHelm(rootFolder) {
fs.chmodSync(rootFolder, '777');
var filelist = [];
@ -166,16 +151,11 @@ function findHelm(rootFolder) {
function run() {
return __awaiter(this, void 0, void 0, function* () {
let version = core.getInput('version', { 'required': true });
if (version.toLocaleLowerCase() === 'latest') {
version = yield getStableHelmVersion();
if (version.toLocaleLowerCase() === 'latest' || version === LATEST_HELM3_VERSION) {
version = yield getLatestHelmVersionFor("v3");
}
else if (version === LATEST_HELM2_VERSION) {
const v = yield getLatestHelmVersionFor(2);
version = 'v' + v;
}
else if (version === LATEST_HELM3_VERSION) {
const v = yield getLatestHelmVersionFor(3);
version = v;
version = yield getLatestHelmVersionFor("v2");
}
else if (!version.toLocaleLowerCase().startsWith('v')) {
version = 'v' + version;

View file

@ -5,7 +5,6 @@ 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 toolCache from '@actions/tool-cache';
import * as core from '@actions/core';
@ -13,7 +12,6 @@ import { graphql } from '@octokit/graphql';
const helmToolName = 'helm';
const stableHelmVersion = 'v3.2.1';
const helmAllReleasesUrl = 'https://api.github.com/repos/helm/helm/releases';
const LATEST_HELM2_VERSION = '2.*';
const LATEST_HELM3_VERSION = '3.*';
@ -38,32 +36,6 @@ function getHelmDownloadURL(version: string): string {
}
}
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;
}
var walkSync = function (dir, filelist, fileToFind) {
var files = fs.readdirSync(dir);
filelist = filelist || [];
@ -82,7 +54,7 @@ var walkSync = function (dir, filelist, fileToFind) {
};
async function downloadHelm(version: string): Promise<string> {
if (!version) { version = await getStableHelmVersion(); }
if (!version) { version = await getLatestHelmVersionFor("v3"); }
let cachedToolpath = toolCache.find(helmToolName, version);
if (!cachedToolpath) {
let helmDownloadPath;
@ -107,9 +79,8 @@ async function downloadHelm(version: string): Promise<string> {
}
async function getLatestHelmVersionFor(type) {
console.log("Running graphql")
console.log(type)
const token = core.getInput('token', { 'required': true });
try {
const { repository } = await graphql(
`
{
@ -128,10 +99,24 @@ async function getLatestHelmVersionFor(type) {
}
}
);
console.log(repository.releases.nodes);
const releases = repository.releases.nodes.reverse();
releases.forEach(release => {
if (isValidVersion(release.tagName, type))
return release.tagName;
});
} catch (err) {
core.warning(util.format("Error while fetching the latest Helm %s release. Error: %s. Using defaul Helm version %s.", type, err.toString(), stableHelmVersion));
}
return stableHelmVersion;
}
// isValidVersion checks if verison matches the specified type and is a stable release
function isValidVersion(version, type) {
if (!version.startsWith(type))
return false;
return version.indexOf('rc') == -1
}
function findHelm(rootFolder: string): string {
fs.chmodSync(rootFolder, '777');
var filelist: string[] = [];
@ -146,14 +131,10 @@ function findHelm(rootFolder: string): string {
async function run() {
let version = core.getInput('version', { 'required': true });
if (version.toLocaleLowerCase() === 'latest') {
version = await getStableHelmVersion();
if (version.toLocaleLowerCase() === 'latest' || version === LATEST_HELM3_VERSION) {
version = await getLatestHelmVersionFor("v3");
} else if (version === LATEST_HELM2_VERSION) {
const v = await getLatestHelmVersionFor(2);
version = 'v' + v;
} else if (version === LATEST_HELM3_VERSION) {
const v = await getLatestHelmVersionFor(3);
version = v;
version = await getLatestHelmVersionFor("v2");
} else if (!version.toLocaleLowerCase().startsWith('v')) {
version = 'v' + version;
}