Compare commits

..

30 Commits

Author SHA1 Message Date
beef3f6e66 Fix codeowners 2022-03-03 15:16:30 -05:00
fa870ea9a2 run to index (#67)
* run to index

* action.yaml route

* move to dev dep
2022-02-09 11:26:07 -05:00
e00756a00e Version fix (#66)
* Added version validation check

* Added check for latest

* Changed Helm 3.5.0 test to also test lack of v in version

* Pushing integration tests

* Removed push integration test

* Added more context to integration test

* Addressing comment
2022-02-08 17:07:21 -05:00
2998c83e16 Updated workflows, codeowner, .gitignore (#65) 2022-02-04 13:04:30 -05:00
5876560d6c Add arm64 support (#64)
* add arm64 support
2022-02-04 09:45:03 -05:00
7e6f48e5b4 master to main rename (#61) 2022-02-03 11:29:11 -05:00
da63a48ad7 Cleanup action for release (#56)
* Removed empty try catch

* Initial commit

* Compiled to js

* Escape slash for nodejs

* Updating getStableHelmVersion test

* Removing old versions of run

* Added comment on getLatestHelmVersion and variable for helm release url to command

* Pushing recent changes before computer reset

* Latest getLatestHelmVersion iteration

* Removed test

* Testing async test

* getLatestHelmVersion refactor

* fixed method to access helm releses JSON

* Edited comment on isValidVersion

* Removed old integration tests

* Small temp test

* Removed IntegrationTests path

* Updated package-lock.json and removed unused dep

* Integration testing

* Fixed if statement in run command

* Added exit

* Double brackets for new test

* Added version confirmation output and different check for version

* Helm 3.7.2 test

* added env to workflow paths

* adjusted env in workflow paths

* 3.5.0 tests

* Requested changes

* Fixed downloadHelm test

* Clean up for release

* Updated CODEOWNERS

* Updated CODEOWNERS
2022-01-26 16:35:07 -05:00
cc2c85bd7e support only v3 and switch away from github token 2022-01-26 15:27:11 -05:00
7e3c8fd604 Update README.md (#48) 2021-08-10 19:41:32 +05:30
ac8103f453 Create CODEOWNERS (#47) 2021-08-10 15:53:58 +05:30
feeafa6152 Update issue templates (#46) 2021-06-30 11:05:14 +05:30
b8c59ed1e4 Create defaultLabels.yml (#44) 2021-06-25 17:05:14 +05:30
14d85694fc Update run.ts 2021-06-24 19:26:50 +05:30
cfc72cf63e Update run.ts 2021-06-24 19:25:35 +05:30
470bf91618 Syncing package-lock.json in master with release (#42)
* Syncing package.json and package-lock.json in master with release

* reverting changes in package.json
2021-06-03 14:10:06 +05:30
0a4da9a67f Bump ws from 7.4.4 to 7.4.6 (#41)
Bumps [ws](https://github.com/websockets/ws) from 7.4.4 to 7.4.6.
- [Release notes](https://github.com/websockets/ws/releases)
- [Commits](https://github.com/websockets/ws/compare/7.4.4...7.4.6)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2021-06-01 16:01:11 +05:30
07143cd16a Bump browserslist from 4.16.3 to 4.16.6 (#40)
Bumps [browserslist](https://github.com/browserslist/browserslist) from 4.16.3 to 4.16.6.
- [Release notes](https://github.com/browserslist/browserslist/releases)
- [Changelog](https://github.com/browserslist/browserslist/blob/main/CHANGELOG.md)
- [Commits](https://github.com/browserslist/browserslist/compare/4.16.3...4.16.6)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2021-06-01 16:00:44 +05:30
dcb450d677 Bump hosted-git-info from 2.8.8 to 2.8.9 (#39)
Bumps [hosted-git-info](https://github.com/npm/hosted-git-info) from 2.8.8 to 2.8.9.
- [Release notes](https://github.com/npm/hosted-git-info/releases)
- [Changelog](https://github.com/npm/hosted-git-info/blob/v2.8.9/CHANGELOG.md)
- [Commits](https://github.com/npm/hosted-git-info/compare/v2.8.8...v2.8.9)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2021-05-26 13:05:55 +05:30
1571d72e78 skipLibCheck (#31)
* Update package.json

* Update tsconfig.json
2021-04-01 14:19:07 +05:30
62aeada586 Added trigger for L2 tests. (#26) 2021-03-31 17:23:12 +05:30
edc063ca5f adding graph ql for Helm versions - master (#22)
* Adding graphql to find out latest helm version of specified type

* Updating package-loc.json

* Updating js file

* Fixing PR comments

* Adding feature flag as environment variable

* Changing feature flag name

* Updating package-loc.json
2021-03-30 22:43:44 +05:30
e77e49c8f8 Added L0 tests and fixed some minor issues. (#20) 2021-03-26 13:08:55 +05:30
d315e938e1 Merge pull request #17 from Azure/dependabot/npm_and_yarn/actions/core-1.2.6
Bump @actions/core from 1.0.0 to 1.2.6
2020-10-05 12:26:52 +05:30
509333604f Bump @actions/core from 1.0.0 to 1.2.6
Bumps [@actions/core](https://github.com/actions/toolkit/tree/HEAD/packages/core) from 1.0.0 to 1.2.6.
- [Release notes](https://github.com/actions/toolkit/releases)
- [Changelog](https://github.com/actions/toolkit/blob/main/packages/core/RELEASES.md)
- [Commits](https://github.com/actions/toolkit/commits/HEAD/packages/core)

Signed-off-by: dependabot[bot] <support@github.com>
2020-10-01 17:24:52 +00:00
91b7d21636 Changed the documentation to specify v before 'version' and code changes to append 'v' if it not provided in version (#11) (#15)
* Chnaged the documentation to sepcify v before 'version'

* append v to version incase it does not already include

* review comments fix
2020-05-14 12:16:24 +05:30
b1c4524a5f Users/shigupt/fixing helm latest releases master (#14)
* Updating the logic for downloading latest Helm version

* Adding semver to package.json
2020-05-13 21:53:04 +05:30
f404b8932e Update README.md 2020-02-27 14:58:14 +05:30
1de6b70ec6 Use correct version string format in setup instructions #7
Use correct version string format in setup instructions
2020-02-04 18:12:33 +05:30
d0d63812ab Use correct version string format in setup instructions
If somebody uses `1.15.0` instead of `v1.15.0` the installation will fail, this adjusts instructions to be slightly clearer.
2019-12-17 16:21:45 -08:00
87a2cd59a0 Merge pull request #5 from Azure/users/ansheno/helminstallfix
Added helm binary to the Path environment variable
2019-11-26 18:05:34 +05:30
17 changed files with 12287 additions and 311 deletions

1
.github/CODEOWNERS vendored Normal file
View File

@ -0,0 +1 @@
* @Azure/aks-atlanta

View File

@ -0,0 +1,10 @@
---
name: 'Issue: Bug Report / Feature Request'
about: Create a report to help us improve
title: ''
labels: need-to-triage
assignees: ''
---

35
.github/workflows/defaultLabels.yml vendored Normal file
View File

@ -0,0 +1,35 @@
name: setting-default-labels
# Controls when the action will run.
on:
schedule:
- cron: "0 0/3 * * *"
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- uses: actions/stale@v3
name: Setting issue as idle
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
stale-issue-message: "This issue is idle because it has been open for 14 days with no activity."
stale-issue-label: "idle"
days-before-stale: 14
days-before-close: -1
operations-per-run: 100
exempt-issue-labels: "backlog"
- uses: actions/stale@v3
name: Setting PR as idle
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
stale-pr-message: "This PR is idle because it has been open for 14 days with no activity."
stale-pr-label: "idle"
days-before-stale: 14
days-before-close: -1
operations-per-run: 100

65
.github/workflows/integration-tests.yml vendored Normal file
View File

@ -0,0 +1,65 @@
name: "Trigger Integration tests"
on:
pull_request:
branches:
- main
- "releases/*"
jobs:
trigger-integration-tests:
name: Trigger Integration tests
runs-on: ubuntu-latest
env:
HELM_3_8_0: "v3.8.0"
HELM_3_7_2: "v3.7.2"
HELM_NO_V: "3.5.0"
PR_BASE_REF: ${{ github.event.pull_request.base.ref }}
steps:
- name: Check out repository
uses: actions/checkout@v2
- name: npm install and build
id: action-npm-build
run: |
echo $PR_BASE_REF
if [[ $PR_BASE_REF != releases/* ]]; then
npm install
npm run build
fi
- name: Setup helm
uses: ./
with:
version: ${{ env.HELM_3_8_0 }}
- name: Validate helm 3.8.0
run: |
if [[ $(helm version) != *$HELM_3_8_0* ]]; then
echo "HELM VERSION INCORRECT: HELM VERSION DOES NOT CONTAIN v3.8.0"
echo "HELM VERSION OUTPUT: $(helm version)"
exit 1
else
echo "HELM VERSION $HELM_3_8_0 INSTALLED SUCCESSFULLY"
fi
- name: Setup helm 3.7.2
uses: ./
with:
version: ${{ env.HELM_3_7_2 }}
- name: Validate 3.7.2
run: |
if [[ $(helm version) != *$HELM_3_7_2* ]]; then
echo "HELM VERSION INCORRECT: HELM VERSION DOES NOT CONTAIN v3.7.2"
echo "HELM VERSION OUTPUT: $(helm version)"
exit 1
else
echo "HELM VERSION $HELM_3_7_2 INSTALLED SUCCESSFULLY"
fi
- name: Setup helm 3.5.0 with no v in version
uses: ./
with:
version: ${{ env.HELM_NO_V }}
- name: Validate 3.5.0 without v in version
run: |
if [[ $(helm version) != *$HELM_NO_V* ]]; then
echo "HELM VERSION INCORRECT: HELM VERSION DOES NOT CONTAIN v3.5.0"
echo "HELM VERSION OUTPUT: $(helm version)"
exit 1
else
echo "HELM VERSION $HELM_3_5_0 INSTALLED SUCCESSFULLY"
fi

14
.github/workflows/release-pr.yml vendored Normal file
View File

@ -0,0 +1,14 @@
name: Create release PR
on:
workflow_dispatch:
inputs:
release:
description: "Define release version (ex: v1, v2, v3)"
required: true
jobs:
release-pr:
uses: OliverMKing/javascript-release-workflow/.github/workflows/release-pr.yml@main
with:
release: ${{ github.event.inputs.release }}

10
.github/workflows/tag-and-draft.yml vendored Normal file
View File

@ -0,0 +1,10 @@
name: Tag and create release draft
on:
push:
branches:
- releases/*
jobs:
tag-and-release:
uses: OliverMKing/javascript-release-workflow/.github/workflows/tag-and-release.yml@main

21
.github/workflows/unit-tests.yml vendored Normal file
View File

@ -0,0 +1,21 @@
name: "Run unit tests."
on: # rebuild any PRs and main branch changes
pull_request:
branches:
- main
- "releases/*"
push:
branches:
- main
- "releases/*"
jobs:
build: # make sure build/ci works properly
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Run L0 tests.
run: |
npm install
npm test

6
.gitignore vendored
View File

@ -59,3 +59,9 @@ typings/
# next.js build output
.next
node_modules
coverage
# Transpiled JS
lib/

View File

@ -1,7 +1,8 @@
# Setup Helm
#### Install a specific version of helm binary on the runner.
Install a specific version of helm binary on the runner.
## Example
Acceptable values are latest or any semantic version string like 1.15.0. Use this action in workflow to define which version of helm will be used.
Acceptable values are latest or any semantic version string like v2.16.7 Use this action in workflow to define which version of helm will be used.
```yaml
- uses: azure/setup-helm@v1

View File

@ -1,15 +1,15 @@
name: 'Helm tool installer'
description: 'Install a specific version of helm binary. Acceptable values are latest or any semantic version string like 1.15.0'
inputs:
name: "Helm tool installer"
description: "Install a specific version of helm binary. Acceptable values are latest or any semantic version string like 1.15.0"
inputs:
version:
description: 'Version of helm'
description: "Version of helm"
required: true
default: 'latest'
default: "latest"
outputs:
helm-path:
description: 'Path to the cached helm binary'
description: "Path to the cached helm binary"
branding:
color: 'blue'
color: "blue"
runs:
using: 'node12'
main: 'lib/run.js'
using: "node12"
main: "lib/index.js"

18
jest.config.js Normal file
View File

@ -0,0 +1,18 @@
module.exports = {
clearMocks: true,
moduleFileExtensions: ['js', 'ts'],
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
transform: {
'^.+\\.ts$': 'ts-jest'
},
verbose: true,
coverageThreshold: {
"global": {
"branches": 0,
"functions": 14,
"lines": 27,
"statements": 27
}
}
}

View File

@ -1,134 +0,0 @@
"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
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) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const os = __importStar(require("os"));
const path = __importStar(require("path"));
const util = __importStar(require("util"));
const fs = __importStar(require("fs"));
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';
function getExecutableExtension() {
if (os.type().match(/^Win/)) {
return '.exe';
}
return '';
}
function getHelmDownloadURL(version) {
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);
}
}
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;
});
});
}
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;
};
function downloadHelm(version) {
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;
});
}
function findHelm(rootFolder) {
fs.chmodSync(rootFolder, '777');
var filelist = [];
walkSync(rootFolder, filelist, helmToolName + getExecutableExtension());
if (!filelist) {
throw new Error(util.format("Helm executable not found in path ", rootFolder));
}
else {
return filelist[0];
}
}
function run() {
return __awaiter(this, void 0, void 0, function* () {
let version = core.getInput('version', { 'required': true });
if (version.toLocaleLowerCase() === 'latest') {
version = yield getStableHelmVersion();
}
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);
});
}
run().catch(core.setFailed);

11693
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -6,18 +6,25 @@
"author": "Anumita Shenoy",
"license": "MIT",
"dependencies": {
"@actions/tool-cache": "1.1.2",
"@actions/core": "^1.2.6",
"@actions/exec": "^1.0.0",
"@actions/io": "^1.0.0",
"@actions/core": "^1.0.0",
"@actions/exec": "^1.0.0"
"@actions/tool-cache": "1.1.2",
"@octokit/graphql": "^4.6.1",
"semver": "^6.1.0"
},
"main": "lib/run.js",
"main": "lib/index.js",
"scripts": {
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1"
"build": "ncc build src/run.ts -o lib",
"test": "jest",
"test-coverage": "jest --coverage"
},
"devDependencies": {
"@types/jest": "^25.2.2",
"@types/node": "^12.0.10",
"@vercel/ncc": "^0.33.1",
"jest": "^26.0.1",
"ts-jest": "^25.5.1",
"typescript": "^3.5.2"
}
}
}

268
src/run.test.ts Normal file
View File

@ -0,0 +1,268 @@
import * as run from "./run";
import * as os from "os";
import * as toolCache from "@actions/tool-cache";
import * as fs from "fs";
import * as path from "path";
import * as core from "@actions/core";
describe("run.ts", () => {
test("getExecutableExtension() - return .exe when os is Windows", () => {
jest.spyOn(os, "type").mockReturnValue("Windows_NT");
expect(run.getExecutableExtension()).toBe(".exe");
expect(os.type).toBeCalled();
});
test("getExecutableExtension() - return empty string for non-windows OS", () => {
jest.spyOn(os, "type").mockReturnValue("Darwin");
expect(run.getExecutableExtension()).toBe("");
expect(os.type).toBeCalled();
});
test("getHelmDownloadURL() - return the URL to download helm for Linux", () => {
jest.spyOn(os, "type").mockReturnValue("Linux");
jest.spyOn(os, "arch").mockReturnValueOnce("unknown");
const kubectlLinuxUrl = "https://get.helm.sh/helm-v3.8.0-linux-amd64.zip";
expect(run.getHelmDownloadURL("v3.8.0")).toBe(kubectlLinuxUrl);
expect(os.type).toBeCalled();
expect(os.arch).toBeCalled();
// arm64
jest.spyOn(os, "type").mockReturnValue("Linux");
jest.spyOn(os, "arch").mockReturnValueOnce("arm64");
const kubectlLinuxArm64Url =
"https://get.helm.sh/helm-v3.8.0-linux-arm64.zip";
expect(run.getHelmDownloadURL("v3.8.0")).toBe(kubectlLinuxArm64Url);
expect(os.type).toBeCalled();
expect(os.arch).toBeCalled();
});
test("getHelmDownloadURL() - return the URL to download helm for Darwin", () => {
jest.spyOn(os, "type").mockReturnValue("Darwin");
jest.spyOn(os, "arch").mockReturnValueOnce("unknown");
const kubectlDarwinUrl = "https://get.helm.sh/helm-v3.8.0-darwin-amd64.zip";
expect(run.getHelmDownloadURL("v3.8.0")).toBe(kubectlDarwinUrl);
expect(os.type).toBeCalled();
expect(os.arch).toBeCalled();
// arm64
jest.spyOn(os, "type").mockReturnValue("Darwin");
jest.spyOn(os, "arch").mockReturnValueOnce("arm64");
const kubectlDarwinArm64Url =
"https://get.helm.sh/helm-v3.8.0-darwin-arm64.zip";
expect(run.getHelmDownloadURL("v3.8.0")).toBe(kubectlDarwinArm64Url);
expect(os.type).toBeCalled();
expect(os.arch).toBeCalled();
});
test("getValidVersion() - return version with v prepended", () => {
expect(run.getValidVersion("3.8.0")).toBe("v3.8.0");
});
test("getHelmDownloadURL() - return the URL to download helm for Windows", () => {
jest.spyOn(os, "type").mockReturnValue("Windows_NT");
const kubectlWindowsUrl =
"https://get.helm.sh/helm-v3.8.0-windows-amd64.zip";
expect(run.getHelmDownloadURL("v3.8.0")).toBe(kubectlWindowsUrl);
expect(os.type).toBeCalled();
});
test("getLatestHelmVersion() - return the latest version of HELM", async () => {
try {
expect(await run.getLatestHelmVersion()).toBe("v3.8.0");
} catch (e) {
return e;
}
});
test("walkSync() - return path to the all files matching fileToFind in dir", () => {
jest.spyOn(fs, "readdirSync").mockImplementation((file, _) => {
if (file == "mainFolder")
return [
"file1" as unknown as fs.Dirent,
"file2" as unknown as fs.Dirent,
"folder1" as unknown as fs.Dirent,
"folder2" as unknown as fs.Dirent,
];
if (file == path.join("mainFolder", "folder1"))
return [
"file11" as unknown as fs.Dirent,
"file12" as unknown as fs.Dirent,
];
if (file == path.join("mainFolder", "folder2"))
return [
"file21" as unknown as fs.Dirent,
"file22" as unknown as fs.Dirent,
];
});
jest.spyOn(core, "debug").mockImplementation();
jest.spyOn(fs, "statSync").mockImplementation((file) => {
const isDirectory =
(file as string).toLowerCase().indexOf("file") == -1 ? true : false;
return { isDirectory: () => isDirectory } as fs.Stats;
});
expect(run.walkSync("mainFolder", null, "file21")).toEqual([
path.join("mainFolder", "folder2", "file21"),
]);
expect(fs.readdirSync).toBeCalledTimes(3);
expect(fs.statSync).toBeCalledTimes(8);
});
test("walkSync() - return empty array if no file with name fileToFind exists", () => {
jest.spyOn(fs, "readdirSync").mockImplementation((file, _) => {
if (file == "mainFolder")
return [
"file1" as unknown as fs.Dirent,
"file2" as unknown as fs.Dirent,
"folder1" as unknown as fs.Dirent,
"folder2" as unknown as fs.Dirent,
];
if (file == path.join("mainFolder", "folder1"))
return [
"file11" as unknown as fs.Dirent,
"file12" as unknown as fs.Dirent,
];
if (file == path.join("mainFolder", "folder2"))
return [
"file21" as unknown as fs.Dirent,
"file22" as unknown as fs.Dirent,
];
});
jest.spyOn(core, "debug").mockImplementation();
jest.spyOn(fs, "statSync").mockImplementation((file) => {
const isDirectory =
(file as string).toLowerCase().indexOf("file") == -1 ? true : false;
return { isDirectory: () => isDirectory } as fs.Stats;
});
expect(run.walkSync("mainFolder", null, "helm.exe")).toEqual([]);
expect(fs.readdirSync).toBeCalledTimes(3);
expect(fs.statSync).toBeCalledTimes(8);
});
test("findHelm() - change access permissions and find the helm in given directory", () => {
jest.spyOn(fs, "chmodSync").mockImplementation(() => {});
jest.spyOn(fs, "readdirSync").mockImplementation((file, _) => {
if (file == "mainFolder") return ["helm.exe" as unknown as fs.Dirent];
});
jest.spyOn(fs, "statSync").mockImplementation((file) => {
const isDirectory =
(file as string).indexOf("folder") == -1 ? false : true;
return { isDirectory: () => isDirectory } as fs.Stats;
});
jest.spyOn(os, "type").mockReturnValue("Windows_NT");
expect(run.findHelm("mainFolder")).toBe(
path.join("mainFolder", "helm.exe")
);
});
test("findHelm() - throw error if executable not found", () => {
jest.spyOn(fs, "chmodSync").mockImplementation(() => {});
jest.spyOn(fs, "readdirSync").mockImplementation((file, _) => {
if (file == "mainFolder") return [];
});
jest.spyOn(fs, "statSync").mockImplementation((file) => {
return { isDirectory: () => true } as fs.Stats;
});
jest.spyOn(os, "type").mockReturnValue("Windows_NT");
expect(() => run.findHelm("mainFolder")).toThrow(
"Helm executable not found in path mainFolder"
);
});
test("downloadHelm() - download helm and return path to it", async () => {
jest.spyOn(toolCache, "find").mockReturnValue("");
jest.spyOn(toolCache, "downloadTool").mockResolvedValue("pathToTool");
const response = JSON.stringify([{ tag_name: "v4.0.0" }]);
jest.spyOn(fs, "readFileSync").mockReturnValue(response);
jest.spyOn(os, "type").mockReturnValue("Windows_NT");
jest.spyOn(fs, "chmodSync").mockImplementation(() => {});
jest.spyOn(toolCache, "extractZip").mockResolvedValue("pathToUnzippedHelm");
jest.spyOn(toolCache, "cacheDir").mockResolvedValue("pathToCachedDir");
jest
.spyOn(fs, "readdirSync")
.mockImplementation((file, _) => ["helm.exe" as unknown as fs.Dirent]);
jest.spyOn(fs, "statSync").mockImplementation((file) => {
const isDirectory =
(file as string).indexOf("folder") == -1 ? false : true;
return { isDirectory: () => isDirectory } as fs.Stats;
});
expect(await run.downloadHelm("v4.0.0")).toBe(
path.join("pathToCachedDir", "helm.exe")
);
expect(toolCache.find).toBeCalledWith("helm", "v4.0.0");
expect(toolCache.downloadTool).toBeCalledWith(
"https://get.helm.sh/helm-v4.0.0-windows-amd64.zip"
);
expect(fs.chmodSync).toBeCalledWith("pathToTool", "777");
expect(toolCache.extractZip).toBeCalledWith("pathToTool");
expect(fs.chmodSync).toBeCalledWith(
path.join("pathToCachedDir", "helm.exe"),
"777"
);
});
test("downloadHelm() - throw error if unable to download", async () => {
jest.spyOn(toolCache, "find").mockReturnValue("");
jest.spyOn(toolCache, "downloadTool").mockImplementation(async () => {
throw "Unable to download";
});
jest.spyOn(os, "type").mockReturnValue("Windows_NT");
await expect(run.downloadHelm("v3.2.1")).rejects.toThrow(
"Failed to download Helm from location https://get.helm.sh/helm-v3.2.1-windows-amd64.zip"
);
expect(toolCache.find).toBeCalledWith("helm", "v3.2.1");
expect(toolCache.downloadTool).toBeCalledWith(
"https://get.helm.sh/helm-v3.2.1-windows-amd64.zip"
);
});
test("downloadHelm() - return path to helm tool with same version from toolCache", async () => {
jest.spyOn(toolCache, "find").mockReturnValue("pathToCachedDir");
jest.spyOn(fs, "chmodSync").mockImplementation(() => {});
expect(await run.downloadHelm("v3.2.1")).toBe(
path.join("pathToCachedDir", "helm.exe")
);
expect(toolCache.find).toBeCalledWith("helm", "v3.2.1");
expect(fs.chmodSync).toBeCalledWith(
path.join("pathToCachedDir", "helm.exe"),
"777"
);
});
test("downloadHelm() - throw error is helm is not found in path", async () => {
jest.spyOn(toolCache, "find").mockReturnValue("");
jest.spyOn(toolCache, "downloadTool").mockResolvedValue("pathToTool");
jest.spyOn(os, "type").mockReturnValue("Windows_NT");
jest.spyOn(fs, "chmodSync").mockImplementation();
jest.spyOn(toolCache, "extractZip").mockResolvedValue("pathToUnzippedHelm");
jest.spyOn(toolCache, "cacheDir").mockResolvedValue("pathToCachedDir");
jest.spyOn(fs, "readdirSync").mockImplementation((file, _) => []);
jest.spyOn(fs, "statSync").mockImplementation((file) => {
const isDirectory =
(file as string).indexOf("folder") == -1 ? false : true;
return { isDirectory: () => isDirectory } as fs.Stats;
});
await expect(run.downloadHelm("v3.2.1")).rejects.toThrow(
"Helm executable not found in path pathToCachedDir"
);
expect(toolCache.find).toBeCalledWith("helm", "v3.2.1");
expect(toolCache.downloadTool).toBeCalledWith(
"https://get.helm.sh/helm-v3.2.1-windows-amd64.zip"
);
expect(fs.chmodSync).toBeCalledWith("pathToTool", "777");
expect(toolCache.extractZip).toBeCalledWith("pathToTool");
});
});

View File

@ -1,132 +1,194 @@
// 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 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';
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 helmToolName = "helm";
const stableHelmVersion = "v3.8.0";
const helmAllReleasesUrl = "https://api.github.com/repos/helm/helm/releases";
function getExecutableExtension(): string {
if (os.type().match(/^Win/)) {
return '.exe';
export async function run() {
let version = core.getInput("version", { required: true });
if(version !== "latest" && version[0] !== "v"){
version = getValidVersion(version);
}
if (version.toLocaleLowerCase() === "latest") {
version = await getLatestHelmVersion();
}
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));
}
return '';
} catch {
//do nothing, set as output variable
}
console.log(
`Helm tool version: '${version}' has been cached at ${cachedPath}`
);
core.setOutput("helm-path", cachedPath);
}
function getHelmDownloadURL(version: string): string {
switch (os.type()) {
case 'Linux':
return util.format('https://get.helm.sh/helm-%s-linux-amd64.zip', version);
//Returns version with proper v before it
export function getValidVersion(version: string): string {
return "v" + version;
}
case 'Darwin':
return util.format('https://get.helm.sh/helm-%s-darwin-amd64.zip', version);
// Downloads the helm releases JSON and parses all the recent versions of helm from it.
// Defaults to sending stable helm version if none are valid or if it fails
case 'Windows_NT':
default:
return util.format('https://get.helm.sh/helm-%s-windows-amd64.zip', version);
export async function getLatestHelmVersion(): Promise<string> {
const helmJSONPath: string = await toolCache.downloadTool(helmAllReleasesUrl);
}
}
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;
});
}
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);
try {
const helmJSON = JSON.parse(fs.readFileSync(helmJSONPath, "utf-8"));
for (let i in helmJSON) {
if (isValidVersion(helmJSON[i].tag_name)) {
return helmJSON[i].tag_name;
}
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);
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);
}
} catch (err) {
core.warning(
util.format(
"Error while fetching the latest Helm release. Error: %s. Using default Helm version %s",
err.toString(),
stableHelmVersion
)
);
return stableHelmVersion;
}
const helmpath = findHelm(cachedToolpath);
if (!helmpath) {
throw new Error(util.format("Helm executable not found in path ", cachedToolpath));
}
fs.chmodSync(helmpath, '777');
return helmpath;
return stableHelmVersion;
}
function findHelm(rootFolder: string): string {
fs.chmodSync(rootFolder, '777');
var filelist: string[] = [];
walkSync(rootFolder, filelist, helmToolName + getExecutableExtension());
if (!filelist) {
throw new Error(util.format("Helm executable not found in path ", rootFolder));
}
else {
return filelist[0];
}
// isValidVersion checks if verison is a stable release
function isValidVersion(version: string): boolean {
return version.indexOf("rc") == -1;
}
async function run() {
let version = core.getInput('version', { 'required': true });
if (version.toLocaleLowerCase() === 'latest') {
version = await getStableHelmVersion();
}
export function getExecutableExtension(): string {
if (os.type().match(/^Win/)) {
return ".exe";
}
return "";
}
let cachedPath = await downloadHelm(version);
const LINUX = "Linux";
const MAC_OS = "Darwin";
const WINDOWS = "Windows_NT";
const ARM64 = "arm64";
export function getHelmDownloadURL(version: string): string {
const arch = os.arch();
const operatingSystem = os.type();
switch (true) {
case operatingSystem == LINUX && arch == ARM64:
return util.format(
"https://get.helm.sh/helm-%s-linux-arm64.zip",
version
);
case operatingSystem == LINUX:
return util.format(
"https://get.helm.sh/helm-%s-linux-amd64.zip",
version
);
case operatingSystem == MAC_OS && arch == ARM64:
return util.format(
"https://get.helm.sh/helm-%s-darwin-arm64.zip",
version
);
case operatingSystem == MAC_OS:
return util.format(
"https://get.helm.sh/helm-%s-darwin-amd64.zip",
version
);
case operatingSystem == WINDOWS:
default:
return util.format(
"https://get.helm.sh/helm-%s-windows-amd64.zip",
version
);
}
}
export async function downloadHelm(version: string): Promise<string> {
let cachedToolpath = toolCache.find(helmToolName, version);
if (!cachedToolpath) {
let helmDownloadPath;
try {
if (!process.env['PATH'].startsWith(path.dirname(cachedPath))) {
core.addPath(path.dirname(cachedPath));
}
}
catch {
//do nothing, set as output variable
helmDownloadPath = await toolCache.downloadTool(
getHelmDownloadURL(version)
);
} catch (exception) {
throw new Error(
util.format(
"Failed to download Helm from location",
getHelmDownloadURL(version)
)
);
}
console.log(`Helm tool version: '${version}' has been cached at ${cachedPath}`);
core.setOutput('helm-path', cachedPath);
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;
}
run().catch(core.setFailed);
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 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;
};
run().catch(core.setFailed);

View File

@ -45,7 +45,7 @@
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
@ -58,6 +58,7 @@
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
"skipLibCheck": true
},
"exclude": ["node_modules", "**/*.test.ts"]
}