2021-06-24 19:26:50 +05:30
|
|
|
// Copyright (c) Microsoft Corporation.
|
|
|
|
|
// Copyright (c) Microsoft Corporation.
|
|
|
|
|
// Licensed under the MIT license.
|
2019-10-11 16:24:26 +05:30
|
|
|
|
2022-06-27 15:27:44 -07:00
|
|
|
import * as os from 'os'
|
|
|
|
|
import * as path from 'path'
|
|
|
|
|
import * as util from 'util'
|
|
|
|
|
import * as fs from 'fs'
|
2021-06-24 19:26:50 +05:30
|
|
|
|
2022-06-27 15:27:44 -07:00
|
|
|
import * as toolCache from '@actions/tool-cache'
|
|
|
|
|
import * as core from '@actions/core'
|
2021-06-24 19:26:50 +05:30
|
|
|
|
2022-06-27 15:27:44 -07:00
|
|
|
const helmToolName = 'helm'
|
|
|
|
|
const stableHelmVersion = 'v3.8.0'
|
|
|
|
|
const helmAllReleasesUrl = 'https://api.github.com/repos/helm/helm/releases'
|
2021-06-24 19:26:50 +05:30
|
|
|
|
2022-01-26 15:27:11 -05:00
|
|
|
export async function run() {
|
2022-06-27 15:27:44 -07:00
|
|
|
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))
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
//do nothing, set as output variable
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log(
|
|
|
|
|
`Helm tool version: '${version}' has been cached at ${cachedPath}`
|
|
|
|
|
)
|
|
|
|
|
core.setOutput('helm-path', cachedPath)
|
2022-01-26 15:27:11 -05:00
|
|
|
}
|
|
|
|
|
|
2022-02-08 17:07:21 -05:00
|
|
|
//Returns version with proper v before it
|
|
|
|
|
export function getValidVersion(version: string): string {
|
2022-06-27 15:27:44 -07:00
|
|
|
return 'v' + version
|
|
|
|
|
}
|
2022-02-08 17:07:21 -05:00
|
|
|
|
2022-02-04 09:45:03 -05:00
|
|
|
// Downloads the helm releases JSON and parses all the recent versions of helm from it.
|
2022-01-26 15:27:11 -05:00
|
|
|
// Defaults to sending stable helm version if none are valid or if it fails
|
|
|
|
|
|
|
|
|
|
export async function getLatestHelmVersion(): Promise<string> {
|
2022-06-27 15:27:44 -07:00
|
|
|
const helmJSONPath: string = await 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
|
|
|
|
|
}
|
2022-02-04 09:45:03 -05:00
|
|
|
}
|
2022-06-27 15:27:44 -07:00
|
|
|
} catch (err) {
|
|
|
|
|
core.warning(
|
|
|
|
|
util.format(
|
|
|
|
|
'Error while fetching the latest Helm release. Error: %s. Using default Helm version %s',
|
|
|
|
|
err.toString(),
|
|
|
|
|
stableHelmVersion
|
|
|
|
|
)
|
2022-02-04 09:45:03 -05:00
|
|
|
)
|
2022-06-27 15:27:44 -07:00
|
|
|
return stableHelmVersion
|
|
|
|
|
}
|
2022-02-04 09:45:03 -05:00
|
|
|
|
2022-06-27 15:27:44 -07:00
|
|
|
return stableHelmVersion
|
2022-01-26 15:27:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// isValidVersion checks if verison is a stable release
|
|
|
|
|
function isValidVersion(version: string): boolean {
|
2022-06-27 15:27:44 -07:00
|
|
|
return version.indexOf('rc') == -1
|
2022-01-26 15:27:11 -05:00
|
|
|
}
|
|
|
|
|
|
2021-06-24 19:26:50 +05:30
|
|
|
export function getExecutableExtension(): string {
|
2022-06-27 15:27:44 -07:00
|
|
|
if (os.type().match(/^Win/)) {
|
|
|
|
|
return '.exe'
|
|
|
|
|
}
|
|
|
|
|
return ''
|
2021-06-24 19:26:50 +05:30
|
|
|
}
|
|
|
|
|
|
2022-06-27 15:27:44 -07:00
|
|
|
const LINUX = 'Linux'
|
|
|
|
|
const MAC_OS = 'Darwin'
|
|
|
|
|
const WINDOWS = 'Windows_NT'
|
|
|
|
|
const ARM64 = 'arm64'
|
2021-06-24 19:26:50 +05:30
|
|
|
export function getHelmDownloadURL(version: string): string {
|
2022-06-27 15:27:44 -07:00
|
|
|
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
|
|
|
|
|
)
|
|
|
|
|
}
|
2021-06-24 19:26:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function downloadHelm(version: string): Promise<string> {
|
2022-06-27 15:27:44 -07:00
|
|
|
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
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const helmpath = findHelm(cachedToolpath)
|
|
|
|
|
if (!helmpath) {
|
2022-02-04 09:45:03 -05:00
|
|
|
throw new Error(
|
2022-06-27 15:27:44 -07:00
|
|
|
util.format('Helm executable not found in path', cachedToolpath)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fs.chmodSync(helmpath, '777')
|
|
|
|
|
return helmpath
|
2021-06-24 19:26:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function findHelm(rootFolder: string): string {
|
2022-06-27 15:27:44 -07:00
|
|
|
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]
|
|
|
|
|
}
|
2021-06-24 19:26:50 +05:30
|
|
|
}
|
|
|
|
|
|
2022-01-26 15:27:11 -05:00
|
|
|
export var walkSync = function (dir, filelist, fileToFind) {
|
2022-06-27 15:27:44 -07:00
|
|
|
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))
|
|
|
|
|
}
|
2022-02-04 09:45:03 -05:00
|
|
|
}
|
2022-06-27 15:27:44 -07:00
|
|
|
})
|
|
|
|
|
return filelist
|
|
|
|
|
}
|
2021-06-24 19:26:50 +05:30
|
|
|
|
2022-06-27 15:27:44 -07:00
|
|
|
run().catch(core.setFailed)
|