Find parent directory name of a file - Jenkins pipeline - jenkins

I have some groovy code (Jenkins Pipeline) to loop over a coverage directory and find any files matching a certain pattern. For any files found, I want to determine what their parent directory is named as. These files are currently in sub dirs under a coverage directory.
def coverageReportFiles = findFiles(glob: 'coverage/**/coverage-summary.json')
for(file in coverageReportFiles ){
echo "${file.directory}" // currently prints out 'false'
}
Are there any utilities with Jenkins Pipeline steps or anything native in groovy I can use to determine this?

It should be possible with file.parentFile.name

Related

How can I explore Jenkins workspace?

I want to scan file stored in my Jenkins project workspace.
for example, when I make a project named my_project, I will get report pattern from Jenkins like **/report.xml. and to find report.xml file, I will explore in my own plugin.
since when I print pwd on jenkins console, it prints where my plugin written, not jenkins workspace. so I need to know how to explore jenkins workspace in my plugin.
thanks!
If you're asking where your jenkins workspace is, it is in the workspace folder where you can find all your projects. It should be relative to your plugin folder: ../workspace. You can try to change directory using the relative path. Hope this helps.

jenkins parameter From Properties file

I have 3 Jenkins jobs to be run in serial.
Run a Ant File
Run another ANT File
Run a command line
All the above jobs use a file path which is set in a properties file.
Ex Job 1 , Executes ANT file placed in file path location
Job 2 , Executes another file placed in same file path location
Job 3 , Executes command line to do SVN update in same file path location
I need to parameterize the file path in all three builds from properties file.
Can anyone help me with possible approach?
Thanks In Advance
This answer could be a little high level. You can use Jenkins Pipeline as a code for this approach instead of using 3 freestyle jobs.
You can create 3 stages which performs these 3 steps. Pipeline as a code supports reading of properties from different file types (json, yaml etc.)
Look for the "EnvInject" plugin. This lets you inject properties into your build as environment variables; these assignments survive build step boundaries.
If the property file is checked in, you can load it in the Build Environment section before the build steps start executing. If the property file is generated during the build sequence, you can add a build step between where the property file is created and where it is used.
Once set, if the property file contains "FOO=/path/to/folder" then in configuring Jenkins things you would refer to $FOO or ${FOO} (for example, an Ant build step might specify "${FOO}/build.xml"; in Windows batch script execution FOO shows up as an environment variable and is referenced by %FOO% (i.e., "#echo Some_Useful_Piece_Of_Data > %FOO%\data.txt"
More information can be found here: https://wiki.jenkins.io/display/JENKINS/EnvInject+Plugin

Adding groovy file from workspace for seed job

I have a seed job in Jenkins to create a job to build the workspace. I want to add the job groovy file from the repository. How do I specify the file path for the grooovy on the seed job build step?
I am trying to add it from the file system on the configuration. I get the error file not found. I have tried adding the complete path and also the name of the file.
I have found the solution for it.
You can specify just the relative path to the workspace to access the groovy files.

How to get files in jenkins pipeline file?

I want to get all files name with some pattern from work-space through jenkins pipeline script.
Get list of pjs files from my work-space and store to array
You can use findFiles:
files = findFiles(glob: '**/.pjs')
Find files in the current working directory. The step returns an array
of file info objects.
These file info objects have name as a property, so you can easily get the name of the files as below:
eg. files[0].name
Note:
Make sure that you in the the correct directory (workspace) using pwd() and dir().

Access file in script-scm from groovy pipeline script

I have a groovy pipeline script checked into scm and loaded from jenkins.
The script is supposed to load a config file, fine-tuning its behavior. The config file is in the same directory as the script.
Can I somehow access this directory from the script? The working directory is set to the workspace, which is not where the script is located. Maybe I can somehow extract the directory of the script itself?

Resources