import only specific module in jenkins groovy - jenkins

I have added some changes to x-jenkins groovy file which is to call y-jenkins functions (.groovy files under /var. in y-jenkins repo) - sorry if my terminology is correct as I am not familiar with jenkins. Now in my pipeline's jenkinsfile, used below to try to import from y-jenkinsfile
#Library('x-jenkinsfile#master') _
#Library('y-Jenkinsfile#master')
Due to above its importing all the functions in y-jenkins and conflicting with x-jenkins functions having same names. Is there any way to import specific functions/methods (.groovy files under /var. in y-jenkins repo) into my jenkinsfile?
Could not find the info I am looking for in this link - https://www.jenkins.io/doc/book/pipeline/shared-libraries/

Related

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/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.

Add dependency to jenkins job dsl

I'm trying to add a dependency to my seed job, but no matter what I try, I always get the exception in Jenkins that it can't find the classes that I import in my groovy job. I've tried adding the dependency as compile, testCompile, lib, everything in my build.gradle file, but it doesn't seem to do anything... I'm trying to import org.yaml.snakeyaml.Yaml from the org.yaml:snakeyaml:1.17 dependency.
Any idea on how I can somehow get jenkins to get a hold of that dependency when trying to execute that seed job?
Thanks!
Alternatively you might use Grape to download any dependency directly from your Jenkinsfile. If you add
#Grab(group='org.yaml', module='snakeyaml', version='1.20')
on top of your Jenkinsfile, Jenkins pipeline will download this dependency and it will get available in your pipeline script.
Never mind, I've found the solution. I copied the dependency to a specific folder during the gradle build and added that to the additional classpath of the job dsl. It works now!

Jenkins Pipeline - how to javadoc a global pipeline library?

Is there a way to run javadoc or the groovy equivalent against a global pipeline library? I out of habit wrote comments for every method that follow the javadoc format - would love if I could run javadoc against the library and have doc generated.
As described in the documentation, global variables (inside vars/) are documented in a <name>.txt file next to the <name>.groovy file.
Groovy classes within src/ are.. well.. just Groovy code and thus GroovyDoc should be used.

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