2019-10-04 12:16:40 +05:30
"use strict" ;
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
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" ) ) ;
2020-05-13 21:53:04 +05:30
const semver = _ _importStar ( require ( "semver" ) ) ;
2019-11-26 17:55:31 +05:30
const toolCache = _ _importStar ( require ( "@actions/tool-cache" ) ) ;
const core = _ _importStar ( require ( "@actions/core" ) ) ;
2019-10-04 12:16:40 +05:30
const helmToolName = 'helm' ;
2020-05-13 21:53:04 +05:30
const stableHelmVersion = 'v3.2.1' ;
const helmAllReleasesUrl = 'https://api.github.com/repos/helm/helm/releases' ;
2019-10-04 12:16:40 +05:30
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 * ( ) {
2020-05-13 21:53:04 +05:30
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 ;
2019-10-04 12:16:40 +05:30
} ) ;
}
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 ;
} ;
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 ) {
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 ) {
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 ) ;
2019-11-26 17:55:31 +05:30
try {
if ( ! process . env [ 'PATH' ] . startsWith ( path . dirname ( cachedPath ) ) ) {
core . addPath ( path . dirname ( cachedPath ) ) ;
}
}
catch ( _a ) {
//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 ) ;