2019-10-11 16:24:26 +05:30
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
2019-10-04 12:16:40 +05:30
import * as os from 'os' ;
import * as path from 'path' ;
import * as util from 'util' ;
import * as fs from 'fs' ;
2019-10-11 16:24:26 +05:30
import * as toolCache from '@actions/tool-cache' ;
import * as core from '@actions/core' ;
2019-10-04 12:16:40 +05:30
const helmToolName = 'helm' ;
const stableHelmVersion = 'v2.14.1' ;
const helmLatestReleaseUrl = 'https://api.github.com/repos/helm/helm/releases/latest' ;
function getExecutableExtension ( ) : string {
if ( os . type ( ) . match ( /^Win/ ) ) {
return '.exe' ;
}
return '' ;
}
function getHelmDownloadURL ( version : string ) : string {
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 ) ;
}
}
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 ) {
2019-10-15 13:33:32 +05:30
var files = fs . readdirSync ( dir ) ;
2019-10-04 12:16:40 +05:30
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 ;
} ;
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 ) ;
}
const helmpath = findHelm ( cachedToolpath ) ;
if ( ! helmpath ) {
2019-10-15 13:33:32 +05:30
throw new Error ( util . format ( "Helm executable not found in path " , cachedToolpath ) ) ;
2019-10-04 12:16:40 +05:30
}
fs . chmodSync ( helmpath , '777' ) ;
return helmpath ;
}
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 ] ;
}
}
async function run() {
let version = core . getInput ( 'version' , { 'required' : true } ) ;
if ( version . toLocaleLowerCase ( ) === 'latest' ) {
version = await getStableHelmVersion ( ) ;
}
let cachedPath = await downloadHelm ( version ) ;
2019-11-26 17:55:31 +05:30
try {
if ( ! process . env [ 'PATH' ] . startsWith ( path . dirname ( cachedPath ) ) ) {
core . addPath ( path . dirname ( cachedPath ) ) ;
}
}
catch {
//do nothing, set as output variable
}
2019-10-04 12:16:40 +05:30
console . log ( ` Helm tool version: ' ${ version } ' has been cached at ${ cachedPath } ` ) ;
core . setOutput ( 'helm-path' , cachedPath ) ;
}
run ( ) . catch ( core . setFailed ) ;