Compare commits

...

8 Commits

Author SHA1 Message Date
7b194e8d64 Added push testing to repro issue faster 2022-04-12 16:43:23 -04:00
0a86c98d61 Fix codeowners (#71) 2022-04-11 11:05:10 -04:00
39f78708c2 add version support message (#69) 2022-04-11 11:05:03 -04: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
17 changed files with 614 additions and 634 deletions

2
.github/CODEOWNERS vendored
View File

@ -1 +1 @@
* @Azure/aksatlanta
* @Azure/aks-atlanta

View File

@ -13,24 +13,23 @@ jobs:
# 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'
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'
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'
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

View File

@ -0,0 +1,62 @@
name: "Trigger Integration tests"
on:
push:
jobs:
trigger-integration-tests:
name: Trigger Integration tests - macos-latest
runs-on: macos-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

View File

@ -2,8 +2,8 @@ name: "Trigger Integration tests"
on:
pull_request:
branches:
- master
- 'releases/*'
- main
- "releases/*"
jobs:
trigger-integration-tests:
name: Trigger Integration tests
@ -11,7 +11,7 @@ jobs:
env:
HELM_3_8_0: "v3.8.0"
HELM_3_7_2: "v3.7.2"
HELM_3_5_0: "v3.5.0"
HELM_NO_V: "3.5.0"
PR_BASE_REF: ${{ github.event.pull_request.base.ref }}
steps:
- name: Check out repository
@ -50,13 +50,13 @@ jobs:
else
echo "HELM VERSION $HELM_3_7_2 INSTALLED SUCCESSFULLY"
fi
- name: Setup helm 3.5.0
- name: Setup helm 3.5.0 with no v in version
uses: ./
with:
version: ${{ env.HELM_3_5_0 }}
- name: Validate 3.5.0
version: ${{ env.HELM_NO_V }}
- name: Validate 3.5.0 without v in version
run: |
if [[ $(helm version) != *$HELM_3_5_0* ]]; then
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

View File

@ -1,4 +1,4 @@
name: "Create release PR"
name: Create release PR
on:
workflow_dispatch:
@ -8,48 +8,7 @@ on:
required: true
jobs:
createPullRequest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
release-pr:
uses: OliverMKing/javascript-release-workflow/.github/workflows/release-pr.yml@main
with:
fetch-depth: 0
- name: Check if remote branch exists
env:
BRANCH: releases/${{ github.event.inputs.release }}
run: |
echo "##[set-output name=exists;]$(echo $(if [[ -z $(git ls-remote --heads origin ${BRANCH}) ]]; then echo false; else echo true; fi;))"
id: extract-branch-status
# these two only need to occur if the branch exists
- name: Checkout proper branch
if: ${{ steps.extract-branch-status.outputs.exists == 'true' }}
env:
BRANCH: releases/${{ github.event.inputs.release }}
run: git checkout ${BRANCH}
- name: Reset promotion branch
if: ${{ steps.extract-branch-status.outputs.exists == 'true' }}
run: |
git fetch origin master:master
git reset --hard master
- name: Install packages
run: |
rm -rf node_modules/
npm install --no-bin-links
npm run build
- name: Remove node_modules from gitignore
run: |
sed -i '/node_modules/d' ./.gitignore
- name: Create branch
uses: peterjgrainger/action-create-branch@v2.0.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
branch: releases/${{ github.event.inputs.release }}
- name: Create pull request
uses: peter-evans/create-pull-request@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
commit-message: Add node modules and new code for release
title: ${{ github.event.inputs.release }} new release
base: releases/${{ github.event.inputs.release }}
branch: create-release
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

View File

@ -1,77 +0,0 @@
name: "Tag and create release draft"
on:
push:
branches:
- releases/*
jobs:
gh_tagged_release:
runs-on: "ubuntu-latest"
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Test release
run: |
sudo npm install n
sudo n latest
npm test
- name: Get branch ending
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF##*/} | sed 's:.*/::')"
id: extract-branch
- name: Get tags
run: |
echo "##[set-output name=tags;]$(echo $(git tag))"
id: extract-tags
- name: Get latest tag
uses: actions/github-script@v5
env:
TAGS: ${{ steps.extract-tags.outputs.tags }}
BRANCH: ${{ steps.extract-branch.outputs.branch }}
with:
script: |
const tags = process.env["TAGS"]
.split(" ")
.map((x) => x.trim());
const branch = process.env["BRANCH"];
const splitTag = (x) =>
x
.substring(branch.length + 1)
.split(".")
.map((x) => Number(x));
function compareTags(nums1, nums2, position = 0) {
if (nums1.length < position && nums2.length < position) return nums2;
const num1 = splitTag(nums1)[position] || 0;
const num2 = splitTag(nums2)[position] || 0;
if (num1 === num2) return compareTags(nums1, nums2, position + 1);
else if (num1 > num2) return nums1;
else return nums2;
}
const branchTags = tags.filter((tag) => tag.startsWith(branch));
if (branchTags.length < 1) return branch + ".-1"
return branchTags.reduce((prev, curr) => compareTags(prev, curr));
result-encoding: string
id: get-latest-tag
- name: Get new tag
uses: actions/github-script@v5
env:
PREV: ${{ steps.get-latest-tag.outputs.result }}
with:
script: |
let version = process.env["PREV"]
if (!version.includes(".")) version += ".0"; // case of v1 or v2
const prefix = /^([a-zA-Z]+)/.exec(version)[0];
const numbers = version.substring(prefix.length);
let split = numbers.split(".");
split[split.length - 1] = parseInt(split[split.length - 1]) + 1;
return prefix + split.join(".");
result-encoding: string
id: get-new-tag
- uses: "marvinpinto/action-automatic-releases@v1.2.1"
with:
title: ${{ steps.get-new-tag.outputs.result }} release
automatic_release_tag: ${{ steps.get-new-tag.outputs.result }}
repo_token: "${{ secrets.GITHUB_TOKEN }}"
draft: true

View File

@ -2,12 +2,12 @@ name: "Run unit tests."
on: # rebuild any PRs and main branch changes
pull_request:
branches:
- master
- 'releases/*'
- main
- "releases/*"
push:
branches:
- master
- 'releases/*'
- main
- "releases/*"
jobs:
build: # make sure build/ci works properly

3
.gitignore vendored
View File

@ -62,3 +62,6 @@ typings/
node_modules
coverage
# Transpiled JS
lib/

View File

@ -2,7 +2,7 @@
Install a specific version of helm binary on the runner.
## Example
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.
Acceptable values are latest or any semantic version string like v3.5.0 Use this action in workflow to define which version of helm will be used. v2 of this action only supports Helm3.
```yaml
- uses: azure/setup-helm@v1

View File

@ -1,174 +0,0 @@
import * as run from '../src/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');
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();
});
test('getHelmDownloadURL() - return the URL to download helm for Darwin', () => {
jest.spyOn(os, 'type').mockReturnValue('Darwin');
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();
});
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,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'
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"

View File

@ -1,142 +0,0 @@
"use strict";
// Copyright (c) Microsoft Corporation.
// 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.walkSync = exports.findHelm = exports.downloadHelm = exports.getHelmDownloadURL = exports.getExecutableExtension = exports.getLatestHelmVersion = exports.run = void 0;
const os = require("os");
const path = require("path");
const util = require("util");
const fs = require("fs");
const toolCache = require("@actions/tool-cache");
const core = require("@actions/core");
const helmToolName = 'helm';
const stableHelmVersion = 'v3.8.0';
const helmAllReleasesUrl = 'https://api.github.com/repos/helm/helm/releases';
function run() {
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;
// 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
function getLatestHelmVersion() {
return __awaiter(this, void 0, void 0, function* () {
const helmJSONPath = yield toolCache.downloadTool(helmAllReleasesUrl);
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;
}
}
}
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;
}
return stableHelmVersion;
});
}
exports.getLatestHelmVersion = getLatestHelmVersion;
// isValidVersion checks if verison is a stable release
function isValidVersion(version) {
return version.indexOf('rc') == -1;
}
function getExecutableExtension() {
if (os.type().match(/^Win/)) {
return '.exe';
}
return '';
}
exports.getExecutableExtension = getExecutableExtension;
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);
}
}
exports.getHelmDownloadURL = getHelmDownloadURL;
function downloadHelm(version) {
return __awaiter(this, void 0, void 0, function* () {
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 findHelm(rootFolder) {
fs.chmodSync(rootFolder, '777');
var filelist = [];
exports.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];
}
}
exports.findHelm = findHelm;
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 = exports.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);

22
package-lock.json generated
View File

@ -19,6 +19,7 @@
"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"
@ -1085,6 +1086,15 @@
"integrity": "sha512-7tFImggNeNBVMsn0vLrpn1H1uPrUBdnARPTpZoitY37ZrdJREzf7I16tMrlK3hen349gr1NYh8CmZQa7CTG6Aw==",
"dev": true
},
"node_modules/@vercel/ncc": {
"version": "0.33.1",
"resolved": "https://registry.npmjs.org/@vercel/ncc/-/ncc-0.33.1.tgz",
"integrity": "sha512-Mlsps/P0PLZwsCFtSol23FGqT3FhBGb4B1AuGQ52JTAtXhak+b0Fh/4T55r0/SVQPeRiX9pNItOEHwakGPmZYA==",
"dev": true,
"bin": {
"ncc": "dist/ncc/cli.js"
}
},
"node_modules/abab": {
"version": "2.0.5",
"resolved": "https://registry.npmjs.org/abab/-/abab-2.0.5.tgz",
@ -7402,6 +7412,12 @@
"integrity": "sha512-7tFImggNeNBVMsn0vLrpn1H1uPrUBdnARPTpZoitY37ZrdJREzf7I16tMrlK3hen349gr1NYh8CmZQa7CTG6Aw==",
"dev": true
},
"@vercel/ncc": {
"version": "0.33.1",
"resolved": "https://registry.npmjs.org/@vercel/ncc/-/ncc-0.33.1.tgz",
"integrity": "sha512-Mlsps/P0PLZwsCFtSol23FGqT3FhBGb4B1AuGQ52JTAtXhak+b0Fh/4T55r0/SVQPeRiX9pNItOEHwakGPmZYA==",
"dev": true
},
"abab": {
"version": "2.0.5",
"resolved": "https://registry.npmjs.org/abab/-/abab-2.0.5.tgz",
@ -9304,7 +9320,8 @@
"version": "1.2.2",
"resolved": "https://registry.npmjs.org/jest-pnp-resolver/-/jest-pnp-resolver-1.2.2.tgz",
"integrity": "sha512-olV41bKSMm8BdnuMsewT4jqlZ8+3TCARAXjZGT9jcoSnrfUnRCqnMoF9XEeoWjbzObpqF9dRhHQj0Xb9QdF6/w==",
"dev": true
"dev": true,
"requires": {}
},
"jest-regex-util": {
"version": "26.0.0",
@ -11608,7 +11625,8 @@
"version": "7.5.6",
"resolved": "https://registry.npmjs.org/ws/-/ws-7.5.6.tgz",
"integrity": "sha512-6GLgCqo2cy2A2rjCNFlxQS6ZljG/coZfZXclldI8FB/1G3CCI36Zd8xy2HrFVACi8tfk5XrgLQEk+P0Tnz9UcA==",
"dev": true
"dev": true,
"requires": {}
},
"xml-name-validator": {
"version": "3.0.0",

View File

@ -13,17 +13,18 @@
"@octokit/graphql": "^4.6.1",
"semver": "^6.1.0"
},
"main": "lib/run.js",
"main": "lib/index.js",
"scripts": {
"build": "tsc --outDir ./lib --rootDir ./src",
"build": "ncc build src/run.ts -o lib",
"test": "jest",
"test-coverage": "jest --coverage"
},
"devDependencies": {
"@types/node": "^12.0.10",
"typescript": "^3.5.2",
"jest": "^26.0.1",
"@types/jest": "^25.2.2",
"ts-jest": "^25.5.1"
"@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

@ -2,22 +2,25 @@
// 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 = 'v3.8.0';
const helmAllReleasesUrl = 'https://api.github.com/repos/helm/helm/releases';
const helmToolName = "helm";
const stableHelmVersion = "v3.8.0";
const helmAllReleasesUrl = "https://api.github.com/repos/helm/helm/releases";
export async function run() {
let version = core.getInput('version', { 'required': true });
let version = core.getInput("version", { required: true });
if (version.toLocaleLowerCase() === 'latest') {
if(version !== "latest" && version[0] !== "v"){
version = getValidVersion(version);
}
if (version.toLocaleLowerCase() === "latest") {
version = await getLatestHelmVersion();
}
@ -25,17 +28,22 @@ export async function run() {
let cachedPath = await downloadHelm(version);
try {
if (!process.env['PATH'].startsWith(path.dirname(cachedPath))) {
if (!process.env["PATH"].startsWith(path.dirname(cachedPath))) {
core.addPath(path.dirname(cachedPath));
}
}
catch {
} catch {
//do nothing, set as output variable
}
console.log(`Helm tool version: '${version}' has been cached at ${cachedPath}`);
core.setOutput('helm-path', cachedPath);
console.log(
`Helm tool version: '${version}' has been cached at ${cachedPath}`
);
core.setOutput("helm-path", cachedPath);
}
//Returns version with proper v before it
export function getValidVersion(version: string): string {
return "v" + version;
}
// Downloads the helm releases JSON and parses all the recent versions of helm from it.
@ -45,14 +53,20 @@ export async function getLatestHelmVersion(): Promise<string> {
const helmJSONPath: string = await toolCache.downloadTool(helmAllReleasesUrl);
try {
const helmJSON = JSON.parse(fs.readFileSync(helmJSONPath, 'utf-8'))
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;
}
}
} catch (err) {
core.warning(util.format("Error while fetching the latest Helm release. Error: %s. Using default Helm version %s", err.toString(), stableHelmVersion));
core.warning(
util.format(
"Error while fetching the latest Helm release. Error: %s. Using default Helm version %s",
err.toString(),
stableHelmVersion
)
);
return stableHelmVersion;
}
@ -61,27 +75,53 @@ export async function getLatestHelmVersion(): Promise<string> {
// isValidVersion checks if verison is a stable release
function isValidVersion(version: string): boolean {
return version.indexOf('rc') == -1;
return version.indexOf("rc") == -1;
}
export function getExecutableExtension(): string {
if (os.type().match(/^Win/)) {
return '.exe';
return ".exe";
}
return '';
return "";
}
const LINUX = "Linux";
const MAC_OS = "Darwin";
const WINDOWS = "Windows_NT";
const ARM64 = "arm64";
export function getHelmDownloadURL(version: string): string {
switch (os.type()) {
case 'Linux':
return util.format('https://get.helm.sh/helm-%s-linux-amd64.zip', version);
const arch = os.arch();
const operatingSystem = os.type();
case 'Darwin':
return util.format('https://get.helm.sh/helm-%s-darwin-amd64.zip', version);
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 'Windows_NT':
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);
return util.format(
"https://get.helm.sh/helm-%s-windows-amd64.zip",
version
);
}
}
@ -90,33 +130,47 @@ export async function downloadHelm(version: string): Promise<string> {
if (!cachedToolpath) {
let helmDownloadPath;
try {
helmDownloadPath = await toolCache.downloadTool(getHelmDownloadURL(version));
helmDownloadPath = await toolCache.downloadTool(
getHelmDownloadURL(version)
);
} 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');
fs.chmodSync(helmDownloadPath, "777");
const unzipedHelmPath = await toolCache.extractZip(helmDownloadPath);
cachedToolpath = await toolCache.cacheDir(unzipedHelmPath, helmToolName, version);
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));
throw new Error(
util.format("Helm executable not found in path", cachedToolpath)
);
}
fs.chmodSync(helmpath, '777');
fs.chmodSync(helmpath, "777");
return helmpath;
}
export function findHelm(rootFolder: string): string {
fs.chmodSync(rootFolder, '777');
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 {
throw new Error(
util.format("Helm executable not found in path", rootFolder)
);
} else {
return filelist[0];
}
}
@ -127,8 +181,7 @@ export var walkSync = function (dir, filelist, fileToFind) {
files.forEach(function (file) {
if (fs.statSync(path.join(dir, file)).isDirectory()) {
filelist = walkSync(path.join(dir, file), filelist, fileToFind);
}
else {
} else {
core.debug(file);
if (file == fileToFind) {
filelist.push(path.join(dir, file));