Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating the logic for downloading latest Helm version #13

Merged
merged 2 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 25 additions & 13 deletions lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ 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 helmToolName = 'helm';
const stableHelmVersion = 'v2.14.1';
const helmLatestReleaseUrl = 'https://api.github.com/repos/helm/helm/releases/latest';
const stableHelmVersion = 'v3.2.1';
const helmAllReleasesUrl = 'https://api.github.com/repos/helm/helm/releases';
function getExecutableExtension() {
if (os.type().match(/^Win/)) {
return '.exe';
Expand All @@ -46,17 +47,28 @@ function getHelmDownloadURL(version) {
}
function getStableHelmVersion() {
return __awaiter(this, void 0, void 0, function* () {
return toolCache.downloadTool(helmLatestReleaseUrl).then((downloadPath) => {
const response = JSON.parse(fs.readFileSync(downloadPath, 'utf8').toString().trim());
if (!response.tag_name) {
return stableHelmVersion;
}
return response.tag_name;
}, (error) => {
core.debug(error);
core.warning(util.format("Failed to read latest kubectl version from stable.txt. From URL %s. Using default stable version %s", helmLatestReleaseUrl, stableHelmVersion));
return stableHelmVersion;
});
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) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"@actions/tool-cache": "1.1.2",
"@actions/io": "^1.0.0",
"@actions/core": "^1.0.0",
"@actions/exec": "^1.0.0"
"@actions/exec": "^1.0.0",
"semver": "^6.1.0"
},
"main": "lib/run.js",
"scripts": {
Expand Down
73 changes: 41 additions & 32 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ 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';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this need to be added to package.json?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was already there in node modules along with other packages and non of them are present in package.json. I'm not sure what's the criteria to add modules as dependencies in package.json

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I add it or leave it unchanged?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add it explicitly; we can't take dependencies on other packages to have semver all the time! :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant it's already present in node modules, but not as a part of another package. 😛


import * as toolCache from '@actions/tool-cache';
import * as core from '@actions/core';

const helmToolName = 'helm';
const stableHelmVersion = 'v2.14.1';
const helmLatestReleaseUrl = 'https://api.github.com/repos/helm/helm/releases/latest';
const stableHelmVersion = 'v3.2.1';
const helmAllReleasesUrl = 'https://api.github.com/repos/helm/helm/releases';

function getExecutableExtension(): string {
if (os.type().match(/^Win/)) {
Expand All @@ -35,40 +36,48 @@ function getHelmDownloadURL(version: string): string {
}

async function getStableHelmVersion(): Promise<string> {
return toolCache.downloadTool(helmLatestReleaseUrl).then((downloadPath) => {
const response = JSON.parse(fs.readFileSync(downloadPath, 'utf8').toString().trim());
if (!response.tag_name)
{
return stableHelmVersion;
}

return response.tag_name;
}, (error) => {
core.debug(error);
core.warning(util.format("Failed to read latest kubectl version from stable.txt. From URL %s. Using default stable version %s", helmLatestReleaseUrl, stableHelmVersion));
return stableHelmVersion;
});
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 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));
}
}
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;
};
};

async function downloadHelm(version: string): Promise<string> {
if (!version) { version = await getStableHelmVersion(); }
let cachedToolpath = toolCache.find(helmToolName, version);
Expand All @@ -89,7 +98,7 @@ async function downloadHelm(version: string): Promise<string> {
if (!helmpath) {
throw new Error(util.format("Helm executable not found in path ", cachedToolpath));
}

fs.chmodSync(helmpath, '777');
return helmpath;
}
Expand All @@ -113,9 +122,9 @@ async function run() {
}

let cachedPath = await downloadHelm(version);

try {

if (!process.env['PATH'].startsWith(path.dirname(cachedPath))) {
core.addPath(path.dirname(cachedPath));
}
Expand Down