Skip to content

Instantly share code, notes, and snippets.

@imoutsatsos
Created October 10, 2018 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imoutsatsos/9d5c4d4da65b572104ca195cdc000cda to your computer and use it in GitHub Desktop.
Save imoutsatsos/9d5c4d4da65b572104ca195cdc000cda to your computer and use it in GitHub Desktop.
Jenkins Scriptlet: Copies an artifact to a folder in the executing job's WORKSPACE
/*** BEGIN META {
"name" : "copyServerStaticArtifact",
"comment" : "Copies an artifact from a folder (typically in JENKINS_HOME) to a folder in the executing job's WORKSPACE",
"parameters" : [ 'artifactFilter','contentFolder','targetFolder'],
"core": "2.100",
"authors" : [
{ name : "Ioannis K. Moutsatsos" }
]
} END META**/
/* Required parameters:
artifactFilter, contentFolder
Copies static artifact(s) ( as defined by a filter)
to target folder in the executing job's Workspace
*/
import hudson.model.*
// get current thread / Executor
def thr = Thread.currentThread()
// get current build
def build = thr?.executable
def options =new HashMap()
// store build/environmental variables into options
def envVarsMap = build.parent.builds[0].properties.get("envVars")
options.putAll(envVarsMap)
println "JENKINS_HOME: ${options.JENKINS_HOME}"
def jHome=options.JENKINS_HOME
//check 2 alternative paths for contentFolder existence
testPath=new File("$jHome/$contentFolder/")
if (testPath.exists()){
jPath="$jHome/$contentFolder/"
println "Source Path: $jPath"
}else{
jPath="$contentFolder/"
println "Source Path: $jPath"
}
//selective copy using ant
def ant=new AntBuilder()
fileFilter=artifactFilter.split(',')
fileFilter.each{it->
filter=it.trim()
ant.copy(todir:"${options.WORKSPACE}/$targetFolder", overwrite:true){
fileset(dir:jPath){
include(name:"*$filter*")
}
}
}//end each
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment