jenkins use shared library in non default location - jenkins

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!

Related

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

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?

How can I have a non-global (locally scoped) shared library for a declarative pipeline?

I would like to define some groovy code that is imported into one or more declarative pipelines that are all stored in the same git repo.
I do NOT want to create a global shared library that different pipelines from different repos share.
For example, in a repo I might have the following files:
shared-library.groovy
pr-pipeline-unit-tests.groovy
pr-pipeline-ui-tests.groovy
I want both pr-pipeline-unit-tests.groovy and pr-pipeline-ui-tests.groovy to be able to import shared-library.groovy.
These pipelines are executed on PRs, and updates to shared-library.groovy should only affect that PR - this is why I do not want a jenkins globally stored shared library.
As bonus, it would be cool if shared-library.groovy could be a standalone gradle project that is "imported" into the pipelines, similar to how buildSrc is imported into gradle project configuration files. Even better if the shared code could be Kotlin!

Jenkins shared library include java jar file

In my Jenkins shared library how can I import java jar class file. I have helper function written in Java and have it as jar fie. How can I include this jar file part of my Jenkins shared library?
Included jar file in resources Directory but pipeline script not finding the class file. giving unable to resolve class error message.
Thanks
SR
This is the Generic way of referring a Jenkins shared library.
// Jenkinsfile
#Library('first-shared-lib')
You may try using libraryResource step in your pipeline script as:
libraryResource './resource/abc.jar'

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 Pipeline - Call functions in shared jar

So here is my project setup
A separate groovy project
Multiple pipelines
All the pipeline scripts refer to the shared groovy project. I went through the shared libraries and all of the needs to be registered in Jenkins global configuration.
Is there any way to do without it? I tried using Grab but ended up with the error
java.lang.RuntimeException: No suitable ClassLoader found for grab
Firstly for Grab to work your Jenkins needs to have access to the internet.
Shared Libraries are definitely the way to go here.
Like many things the secret sauce is in the syntax.

Resources