github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/src/com/sap/piper/PiperGoUtils.groovy (about) 1 package com.sap.piper 2 3 import hudson.AbortException 4 5 class PiperGoUtils implements Serializable { 6 7 private static piperExecutable = 'piper' 8 9 private static Script steps 10 private static Utils utils 11 12 PiperGoUtils(Script steps) { 13 this.steps = steps 14 this.utils = new Utils() 15 } 16 17 PiperGoUtils(Script steps, Utils utils) { 18 this.steps = steps 19 this.utils = utils 20 } 21 22 void unstashPiperBin() { 23 // Check if the piper binary is already present 24 if (steps.sh(script: "[ -x ./${piperExecutable} ]", returnStatus: true) == 0) { 25 steps.echo "Found ${piperExecutable} binary in the workspace - skipping unstash" 26 return 27 } 28 29 if (utils.unstash('piper-bin').size() > 0) return 30 31 if (steps.env.REPOSITORY_UNDER_TEST && steps.env.LIBRARY_VERSION_UNDER_TEST) { 32 steps.echo("Running in a consumer test, building unit-under-test binary for verification.") 33 steps.dockerExecute(script: steps, dockerImage: 'golang:1.19', dockerOptions: '-u 0', dockerEnvVars: [ 34 REPOSITORY_UNDER_TEST: steps.env.REPOSITORY_UNDER_TEST, 35 LIBRARY_VERSION_UNDER_TEST: steps.env.LIBRARY_VERSION_UNDER_TEST 36 ]) { 37 def piperTar = 'piper-go.tar.gz' 38 def piperTmp = 'piper-tmp' 39 steps.sh "wget --output-document ${piperTar} https://github.com/\${REPOSITORY_UNDER_TEST}/archive/\$LIBRARY_VERSION_UNDER_TEST.tar.gz" 40 steps.sh "PIPER_TMP=${piperTmp}; rm -rf \${PIPER_TMP} && mkdir -p \${PIPER_TMP} && tar --strip-components=1 -C \${PIPER_TMP} -xf ${piperTar}" 41 steps.dir(piperTmp) { 42 steps.sh "CGO_ENABLED=0 go build -tags release -ldflags \"-X github.com/ouraigua/jenkins-library/cmd.GitCommit=${steps.env.LIBRARY_VERSION_UNDER_TEST}\" -o ../piper . && chmod +x ../piper && chown 1000:999 ../piper" 43 } 44 steps.sh "rm -rf ${piperTar} ${piperTmp}" 45 } 46 } else { 47 def libraries = getLibrariesInfo() 48 String version 49 libraries.each {lib -> 50 if (lib.name == 'piper-lib-os') { 51 version = lib.version 52 } 53 } 54 55 // def fallbackUrl = 'https://github.com/SAP/jenkins-library/releases/latest/download/piper_master' 56 // def piperBinUrl = (version == 'master') ? fallbackUrl : "https://github.com/SAP/jenkins-library/releases/download/${version}/piper" 57 <<<<<<< HEAD 58 def fallbackUrl = 'https://github.com/SAP/jenkins-library/releases/download/v1.316.0/piper-darwin.arm64' 59 // def fallbackUrl = 'http://localhost/piper' 60 def piperBinUrl = fallbackUrl 61 ======= 62 // def fallbackUrl = 'https://github.com/SAP/jenkins-library/releases/download/v1.316.0/piper-darwin.arm64' 63 def fallbackUrl = 'http://localhost/piper' 64 def piperBinUrl = 'http://localhost/piper' 65 >>>>>>> 3226847 (Testing...) 66 67 boolean downloaded = downloadGoBinary(piperBinUrl) 68 if (!downloaded) { 69 //Inform that no Piper binary is available for used library branch 70 steps.echo ("Not able to download go binary of Piper for version ${version}") 71 //Fallback to master version & throw error in case this fails 72 steps.retry(12) { 73 if (!downloadGoBinary(fallbackUrl)) { 74 steps.sleep(10) 75 steps.error("Download of Piper go binary failed.") 76 } 77 } 78 } 79 } 80 try { 81 def piperVersion = steps.sh returnStdout: true, script: "./${piperExecutable} version" 82 steps.echo "Piper go binary version: ${piperVersion}" 83 } catch(AbortException ex) { 84 steps.error "Cannot get piper go binary version: ${ex}" 85 } 86 utils.stashWithMessage('piper-bin', 'failed to stash piper binary', piperExecutable) 87 } 88 89 List getLibrariesInfo() { 90 return new JenkinsUtils().getLibrariesInfo() 91 } 92 93 private boolean downloadGoBinary(url) { 94 95 try { 96 def httpStatus = steps.sh(returnStdout: true, script: "curl --insecure --silent --retry 5 --retry-max-time 240 --location --write-out '%{http_code}' --output ${piperExecutable} '${url}'") 97 98 if (httpStatus == '200') { 99 steps.sh(script: "chmod +x ${piperExecutable}") 100 return true 101 } 102 } catch(err) { 103 //nothing to do since error should just result in downloaded=false 104 steps.echo "Failed downloading Piper go binary with error '${err}'. " + 105 "If curl is missing, please ensure that curl is available in the Jenkins master and the agents. It is a prerequisite to run piper." 106 } 107 return false 108 } 109 }