So I have the case that I need to populate findfiles with files from more than one Dir
FILES = steps.findFiles(glob: "${FILE}/*.zip")
then I need to go to another folder and update it
FILES = steps.findFiles(glob: "${AnotherFilePath}/*.zip")
End goal is to iterate over the files and for each file do something.
e.g
for(file in FILES) {
I really want to get away from bash but is it possible to do that Jenkins Groovy way? Can u populate Files Variable?
you could use collectMany groovy method that executes closure for every item in initial list and joins result into one array
def FILES = [FILE, AnotherFilePath].collectMany{ steps.findFiles(glob: "${it}/*.zip") }
Related
A .csv file from another job is being copied into my job's workspace.
Since the .csv file name changes every day, how can I read it without specifying its name?
It's the only .csv file in the folder.
copyArtifacts(projectName: 'scanPinnedVersions')
def lastSuccessBuildNum = Jenkins.instance.getItem("scanPinnedVersions").lastSuccessfulBuild.displayName.replace("#","")
def da = readFile WORKSPACE + "/" + lastSuccessBuildNum + '/scanPinnedVersionReport.2021-12-05-080054.csv'
scanPinnedVersionReport.2021-12-05-080054.csv
Needs to be:
scanPinnedVersionReport*.csv
Thanks for helping. I would love to learn how to do this.
Using findFiles you should be able to accomplish this. Link to Jenkins doc.
It will return a list of files (in this case, sounds like only one) and you can then process it accordingly.
Im using Extended Choice Parameter in Jenkins.
I want to add a drop down parameter in the job and display all folder names within the given directory using a groovy script
how can i do that ?
You can use the following groovy script in the extended choice parameter to list all folders under a given folder (You will probably require an admin approval to run this script):
import groovy.io.FileType
def list = []
def dir = new File("/var/lib/jenkins/workspace")
dir.eachFileRecurse (FileType.DIRECTORIES) { file ->
list << file
}
return list
However, an easier option will be to use the Filesystem List Parameter Plugin.
The plugin lists file, directory or symlink names of a defined directory and exposes them as selection options for the parameter.
It also supports include/exclude patterns and execution on different nodes .
Here is my requirement.
I want to use the Jenkins for packaging multiple zip files.
We have an artifactory with repo A and repo B -- Each one of them have multiple zip files. I have the api's to list the files of a repo
In Jenkins, I want to create a parameterized job where 1st parameter should be able to populate list of zip files from Repo A and 2nd parameter should be able to populate list of zip files from Repo B + In 2nd parameter i should be able to select multiple zip files populated from Repo B
Can you please suggest a better way to do this.
Try something like it:
List<String> files = populate()
doSomething(files)
List<String> populate() {
List<String> files = ''
if (JOB_PARAMETER == 'repoA') {
files.add(yourApiCall())
} //similarly for another
return files
}
JOB_PARAMETER is a parameter in your Jenkins job
In the Job DSL, there is the method readFileFromWorkspace(), which makes it possible to read a files content from the workspace.
Now it would like to have something like readFilesFromDirectory() which gives me all files in some directory.
The goal is to make it possible to choose from different ansible playbooks:
choiceParam('PLAYBOOK_FILE', ['playbook1.yml', 'playbook2.yml'])
and to populate this list with existing files from a directory. Is something like this possible?
Well, shortly after asking this question, I found the solution.
So the Hudson API can be used:
hudson.FilePath workspace =
hudson.model.Executor.currentExecutor().getCurrentWorkspace()
def resultList = workspace.list().findAll { it.name ==~ /deploy.*\.yml/ }
I want to copy only specific files in a directory to remote server using Jenkins SCP Plugin.
I have folder structure /X/Y/...Under Y, I need only the files a b c among a b c d e f. Is this possible...?
Of course, to copy all files all you need is X/Y/**. But what about copying selectively.
I was reading somewhere that this is a kind of bug in the plugin.
I have string parameter, $FILES=x,y,z highlighted in "BUILD WITH PARAMETERS"
SCP Configuration:
Source: some/path/$FILES (relative to $WORKSPACE)
Destination: /var/lib/some/path
You should be able to say X/Y/a; X/Y/b; X/Y/c
Also remember that these files have to be under the job's ${WORKSPACE}
Alternatively, you can have another build step in-between that copies only the files that you want into a staging folder, and then supplying the staging folder to SCP plugin
Edit after OP clarification:
Your $FILES variable contains x,y,z When you supply this as Source to SCP plugin, it becomes:
some/path/x,y,z
Or if we break this one item per line:
some/path/x
y
z
The first item is valid, the next two are not complete paths, therefore are not found.
Several ways to fix it (chose either one):
Full path in parameter variable.
Under your FILES string parameter, list the full path, like:
some/path/x, some/path/y, some/path/z
Under SCP Source, use only $FILES
pros: quick and stable.
cons: looks ugly with long paths.
Wildcard path in parameter variables.
Under your FILES string parameter, list the global wildcard path (files will be found under any directory), like:
**/x, **/y, **/z
Under SCP Source, use only $FILES
pros: quick and looks better than long paths.
cons: only works if files x, y and z are unique in your whole workspace. If there is $WORKSPACE/x and $WORKSPACE/some/path/x, one will end up overwriting the other.
Prepare MYFILES variable and inject it.
You need an Execute Shell build step added. In there write the following:
mypath=some/path/
echo MYFILES=${mypath}${files//,/,$mypath} > myfiles.props
Then add Inject environment variables build step (get the plugin in the link). Under Properties File Path, specify myfiles.props.
Under SCP Source, use only $MYFILES (note you are reading modified and injected variable, not the original $FILES)
pros: looks good in UI, proper and further customizable.
cons: more build steps to maintain in configuration.
p.s.
In all these cases, a multi select Extended Choice Parameter will probably look better than a string parameter.