Load groovy files into jenkins and invoke the files just like invoking shared library files - jenkins

I have multiple groovy files which are wrote as a library files and one file is invoked in another file using file name.
To avoid git dependency we are trying to load pipeline shared library from local repository.
I want to load all groovy files into jenkins runtime and invoke it using file name.
Can someone please help me on this?

Related

How to access a file from different folder in a Jenkins shared library?

I have a shared library with the folder structure like this:
vars/all groovy functions.
Test/a java file that does something.
I'm trying to run this file with the command 'java nameOfFile' in a groovy function inside the folder vars.
How can I do that?

Jenkins/Groovy move variables out to a config file

I've been asked to move some variable from a Groovy script out into a configuration file. I'm fine using something like :-
readFile('../xx-software.cfg').split('\n').each { fileName ->
sh "wget ${theURL}${fileName}"
}
However, even though I have added xx-software.cfg into the same directory as my Groovy script it does become available for use within that groovy script.
I hope this makes sense!?
How can I move my variables out into a config file to make it easier for the application support team to make future edits without changing the code?
There are a few approaches you could use.
Firstly, file format for the configuration and how to read the data into variables. You could use Java Properties format, YAML or JSON and these are all handled by the Pipeline Utility Steps plugin with steps here. You can read the file with these steps:
readProperties
readYaml
readJSON
Next problem, how to get the file available to your pipeline so it can be read from the workspace using these steps. Possibilities are:
In source control with your pipeline code. It can be fetched with the pipeline.
In a separate source control for configuration, your pipeline will need a step to fetch it.
Use the Jenkins Config File Provider plugin. It has a step to provide a config file managed in Jenkins.
Provide it as a Custom Tool zipped archive from a binary server like Artifactory. You can use custom tool definition pipeline steps to make this available to the pipeline.
The Config File Provider option might provide any easy way to have a file that can be updated, but there won't be any version control of it.

jenkins use shared library in non default location

Following
https://jenkins.io/blog/2017/02/15/declarative-notifications/
I use in my java project a shared library vars/sendNotification.groovy that is used in a jenkins pipeline in src/main/resources/pipeline.
I got this folder structure from
https://jenkins.io/doc/book/pipeline/shared-libraries/
Is it somehow possible to put the groovy script in src/main/resources/ as well? On jenkins -> configuration -> Global Pipeline Libraries it doesn't give me options to specify a path of the shared library (the groovy script)
Thanks!

Where does the jenkins shared library files get stored?

The shared library files in jenkins are loaded at the beginning of the job, where does it get stored? I am trying to access the dockerfile stored in the shared library, I need the path to give in the docker build command. Is there a way I can find out the place where the shared library files are loaded in jenkins?
If the shared library is loaded from SCM and your workspace path is jenkins/workspaces/jobName, then a copy is checked out to jenkins/workspaces/jobName#libs or similar (might be suffixed with a number if that path is occupied by another concurrent build).
However, there is another way, if I understand you correctly you wan't to retrieve a resource in this library? In that case you should use the libraryResource and writeFile steps. Like this:
writeFile file:'myFile.txt', text:libraryResource("path/to/myFile.txt")

How to pass job-specific files to Jenkins as part of job configuration?

I have a jenkins job that pulls source code from GitHub public repo. I need to pass some files such as instance-specific configuration files containing secrets to the job and merge with source code prior to running build because these files are obviously inappropriate to be put in public SCM. The Jenkins instance is a multi-tenanted shared service.
The config files don't change often so I don't want to implement using file parameter which forces user manually input the file on every run. Another reason file parameter doesn't work is some builds are triggered automatically by SCM.
I don't want to use Config File Provider Plugin either, because the plugin requires jenkins admin access but I want users with job-level privileges manage the files themselves.
Ideally the uploaded files are saved alongside with job config.xml instead of in workspace, because I would like to delete workspace after each build. I can write scripts to copy the files from job config folder to workspace.
Are there any solutions available? Thanks.
If the "special" files are being placed in a folder with say some access privileges to it, couldn't you either run a Pre-SCM-Buildstep to move the files with shell commands, or introduce a regular build step (i.e. after the SCM stuff and before the other build steps) that would also use shell commands to move files?

Resources