Issue
How to create artifactory directory with current date in jenkins. That is target path should have a directory with current date as the directory name.
rtUpload (
serverId: 'Artifactory-1',
spec: '''{
"files": [
{
"pattern": "bazinga/*froggy*.zip",
"target": "bazinga-repo/froggy-files/<CurrentDate>"
}
]
}''',
Everytime the pipeline is triggered, target path should have a directory name of that particular date. For Ex, if the pipeline runs on 2022-03-29 then:
"target": "bazinga-repo/froggy-files/220329/"
Solution
This is another option:
def current_date = new java.text.SimpleDateFormat('yyMMdd').format(new Date())
pipeline {
// do stuff, generate files...
rtUpload (
serverId: 'Artifactory-1',
spec: """{
"files": [
{
"pattern": "bazinga/*froggy*.zip",
"target": "bazinga-repo/froggy-files/${current_date}"
}
]
}"""
// more stuff
} // end pipeline
This makes use of the groovy string interpolation and a scripted pipeline variable outside the main pipeline
block.
When run the variable current_date
will be assigned and then when it gets to the rtUpload
call the spec
parameter will be evaluated and because it is using triple-double-quote
notation the ${current_date}
part will be replaced by the value of the groovy variable, before it is passed to the function.
This does not then rely on the rtUpload
function spawning a shell and evaluating the shell environment in order to provide the date value to the spec definition.
Groovy strings https://groovy-lang.org/syntax.html#all-strings
Answered By - edwardTew
Answer Checked By - Mary Flores (JavaFixing Volunteer)