Changing order

This commit is contained in:
Shivam Gupta
2021-03-17 12:19:23 +05:30
parent b32bc6e2d6
commit e7eef55398
2 changed files with 104 additions and 102 deletions

View File

@ -144,6 +144,7 @@ function getLatestHelmVersionFor(type) {
} }
`); `);
console.log(versions); console.log(versions);
return stableHelmVersion;
}); });
} }
function findHelm(rootFolder) { function findHelm(rootFolder) {
@ -163,15 +164,15 @@ function run() {
if (version.toLocaleLowerCase() === 'latest') { if (version.toLocaleLowerCase() === 'latest') {
version = yield getStableHelmVersion(); version = yield getStableHelmVersion();
} }
else if (!version.toLocaleLowerCase().startsWith('v')) {
version = 'v' + version;
}
else if (version === LATEST_HELM2_VERSION) { else if (version === LATEST_HELM2_VERSION) {
version = 'v' + getLatestHelmVersionFor(2); version = 'v' + getLatestHelmVersionFor(2);
} }
else if (version === LATEST_HELM3_VERSION) { else if (version === LATEST_HELM3_VERSION) {
version = 'v' + getLatestHelmVersionFor(3); version = 'v' + getLatestHelmVersionFor(3);
} }
else if (!version.toLocaleLowerCase().startsWith('v')) {
version = 'v' + version;
}
let cachedPath = yield downloadHelm(version); let cachedPath = yield downloadHelm(version);
try { try {
if (!process.env['PATH'].startsWith(path.dirname(cachedPath))) { if (!process.env['PATH'].startsWith(path.dirname(cachedPath))) {

View File

@ -18,92 +18,92 @@ const LATEST_HELM2_VERSION = '2.*';
const LATEST_HELM3_VERSION = '3.*'; const LATEST_HELM3_VERSION = '3.*';
function getExecutableExtension(): string { function getExecutableExtension(): string {
if (os.type().match(/^Win/)) { if (os.type().match(/^Win/)) {
return '.exe'; return '.exe';
} }
return ''; return '';
} }
function getHelmDownloadURL(version: string): string { function getHelmDownloadURL(version: string): string {
switch (os.type()) { switch (os.type()) {
case 'Linux': case 'Linux':
return util.format('https://get.helm.sh/helm-%s-linux-amd64.zip', version); return util.format('https://get.helm.sh/helm-%s-linux-amd64.zip', version);
case 'Darwin': case 'Darwin':
return util.format('https://get.helm.sh/helm-%s-darwin-amd64.zip', version); return util.format('https://get.helm.sh/helm-%s-darwin-amd64.zip', version);
case 'Windows_NT': case 'Windows_NT':
default: default:
return util.format('https://get.helm.sh/helm-%s-windows-amd64.zip', version); return util.format('https://get.helm.sh/helm-%s-windows-amd64.zip', version);
} }
} }
async function getStableHelmVersion(): Promise<string> { async function getStableHelmVersion(): Promise<string> {
try { try {
const downloadPath = await toolCache.downloadTool(helmAllReleasesUrl); const downloadPath = await toolCache.downloadTool(helmAllReleasesUrl);
const responseArray = JSON.parse(fs.readFileSync(downloadPath, 'utf8').toString().trim()); const responseArray = JSON.parse(fs.readFileSync(downloadPath, 'utf8').toString().trim());
let latestHelmVersion = semver.clean(stableHelmVersion); let latestHelmVersion = semver.clean(stableHelmVersion);
responseArray.forEach(response => { responseArray.forEach(response => {
if (response && response.tag_name) { if (response && response.tag_name) {
let currentHelmVerison = semver.clean(response.tag_name.toString()); let currentHelmVerison = semver.clean(response.tag_name.toString());
if (currentHelmVerison) { if (currentHelmVerison) {
if (currentHelmVerison.toString().indexOf('rc') == -1 && semver.gt(currentHelmVerison, latestHelmVersion)) { 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 //If current helm version is not a pre release and is greater than latest helm version
latestHelmVersion = currentHelmVerison; latestHelmVersion = currentHelmVerison;
} }
} }
} }
}); });
latestHelmVersion = "v" + latestHelmVersion; latestHelmVersion = "v" + latestHelmVersion;
return latestHelmVersion; return latestHelmVersion;
} catch (error) { } catch (error) {
core.warning(util.format("Cannot get the latest Helm info from %s. Error %s. Using default Helm version %s.", helmAllReleasesUrl, error, stableHelmVersion)); core.warning(util.format("Cannot get the latest Helm info from %s. Error %s. Using default Helm version %s.", helmAllReleasesUrl, error, stableHelmVersion));
} }
return stableHelmVersion; return stableHelmVersion;
} }
var walkSync = function (dir, filelist, fileToFind) { var walkSync = function (dir, filelist, fileToFind) {
var files = fs.readdirSync(dir); var files = fs.readdirSync(dir);
filelist = filelist || []; filelist = filelist || [];
files.forEach(function (file) { files.forEach(function (file) {
if (fs.statSync(path.join(dir, file)).isDirectory()) { if (fs.statSync(path.join(dir, file)).isDirectory()) {
filelist = walkSync(path.join(dir, file), filelist, fileToFind); filelist = walkSync(path.join(dir, file), filelist, fileToFind);
} }
else { else {
core.debug(file); core.debug(file);
if (file == fileToFind) { if (file == fileToFind) {
filelist.push(path.join(dir, file)); filelist.push(path.join(dir, file));
} }
} }
}); });
return filelist; return filelist;
}; };
async function downloadHelm(version: string): Promise<string> { async function downloadHelm(version: string): Promise<string> {
if (!version) { version = await getStableHelmVersion(); } if (!version) { version = await getStableHelmVersion(); }
let cachedToolpath = toolCache.find(helmToolName, version); let cachedToolpath = toolCache.find(helmToolName, version);
if (!cachedToolpath) { if (!cachedToolpath) {
let helmDownloadPath; let helmDownloadPath;
try { try {
helmDownloadPath = await toolCache.downloadTool(getHelmDownloadURL(version)); helmDownloadPath = await toolCache.downloadTool(getHelmDownloadURL(version));
} catch (exception) { } catch (exception) {
throw new Error(util.format("Failed to download Helm from location ", getHelmDownloadURL(version))); 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); fs.chmodSync(helmDownloadPath, '777');
if (!helmpath) { const unzipedHelmPath = await toolCache.extractZip(helmDownloadPath);
throw new Error(util.format("Helm executable not found in path ", cachedToolpath)); cachedToolpath = await toolCache.cacheDir(unzipedHelmPath, helmToolName, version);
} }
fs.chmodSync(helmpath, '777'); const helmpath = findHelm(cachedToolpath);
return helmpath; if (!helmpath) {
throw new Error(util.format("Helm executable not found in path ", cachedToolpath));
}
fs.chmodSync(helmpath, '777');
return helmpath;
} }
async function getLatestHelmVersionFor(type) { async function getLatestHelmVersionFor(type) {
@ -123,46 +123,47 @@ async function getLatestHelmVersionFor(type) {
` `
); );
console.log(versions); console.log(versions);
return stableHelmVersion;
} }
function findHelm(rootFolder: string): string { function findHelm(rootFolder: string): string {
fs.chmodSync(rootFolder, '777'); fs.chmodSync(rootFolder, '777');
var filelist: string[] = []; var filelist: string[] = [];
walkSync(rootFolder, filelist, helmToolName + getExecutableExtension()); walkSync(rootFolder, filelist, helmToolName + getExecutableExtension());
if (!filelist) { if (!filelist) {
throw new Error(util.format("Helm executable not found in path ", rootFolder)); throw new Error(util.format("Helm executable not found in path ", rootFolder));
} }
else { else {
return filelist[0]; return filelist[0];
} }
} }
async function run() { async function run() {
let version = core.getInput('version', { 'required': true }); let version = core.getInput('version', { 'required': true });
if (version.toLocaleLowerCase() === 'latest') { if (version.toLocaleLowerCase() === 'latest') {
version = await getStableHelmVersion(); version = await getStableHelmVersion();
} else if (!version.toLocaleLowerCase().startsWith('v')) { } else if (version === LATEST_HELM2_VERSION) {
version = 'v' + version; version = 'v' + getLatestHelmVersionFor(2);
} else if (version === LATEST_HELM2_VERSION) { } else if (version === LATEST_HELM3_VERSION) {
version = 'v' + getLatestHelmVersionFor(2); version = 'v' + getLatestHelmVersionFor(3);
} else if (version === LATEST_HELM3_VERSION) { } else if (!version.toLocaleLowerCase().startsWith('v')) {
version = 'v' + getLatestHelmVersionFor(3); version = 'v' + 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
}
let cachedPath = await downloadHelm(version); console.log(`Helm tool version: '${version}' has been cached at ${cachedPath}`);
core.setOutput('helm-path', cachedPath);
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); run().catch(core.setFailed);