Jenkins/vars/gomplate.groovy

47 lines
1.3 KiB
Groovy
Raw Normal View History

void call(String sourceTemplate, String destFile, Map env = [:], Map options = [:]) {
String gomplateBin = getOrInstallGomplate(options)
sh """
${exportEnvMap(env)}
${gomplateBin} -f '${sourceTemplate}' > '${destFile}'
"""
}
String exportEnvMap(Map env) {
String exports = ''
env.each { item ->
exports = """
${exports}
export ${item.key}="${item.value}"
"""
}
return exports
}
String getOrInstallGomplate(Map options = [:]) {
String installDir = options.get('installDir', '/usr/local/bin')
String version = options.get('version', '3.10.0')
Boolean forceDownload = options.get('forceDownload', false)
String downloadUrl = options.get('downloadUrl', "https://github.com/hairyhenderson/gomplate/releases/download/v${version}/gomplate_linux-amd64")
String gomplateBin = ''
lock("${env.NODE_NAME}:gomplate-install") {
gomplateBin = sh(returnStdout: true, script: 'which gomplate || exit 0').trim()
if (gomplateBin == '' || forceDownload) {
sh("""
mkdir -p '${installDir}'
curl -o '${installDir}/gomplate' -sSL '${downloadUrl}'
chmod +x '${installDir}/gomplate'
""")
gomplateBin = "${installDir}/gomplate"
}
}
return gomplateBin
}