Working on my 6th or 7th Jenkins script now - and I already noticed they share a bit of code (essentially just the same groovy subroutines over and over again). I wouldn't like to continue with that and rather learn some best practices. It seems that "Shared Libraries" are the thing to do. (Or is there a better way when you just want to share groovy code, not script steps etc.?)
Those scripts are part of a larger repo (that contains the source of the entire project, including the other scripts), stored in a subfolder Jenkins/Library
with this structure:
Jenkins/Library
- vars
| common_code.groovy
There is only a vars
folder, no src
. The documentation said
For Shared Libraries which only define Global Variables (vars/), or a Jenkinsfile which only needs a Global Variable, the annotation pattern @Library('my-shared-library') _ may be useful for keeping code concise. In essence, instead of annotating an unnecessary import statement, the symbol _ is annotated.
so I concluded that I wouldn't need a src
folder and can do with vars
alone.
The library is made available via "Configure Jenkins" > "Global Pipeline Libraries" with SourcePath set to "/Jenkins/Library/
" and is brought in with the statement @Library('{name}') _
as first line of the script.
However, when attempting to use the library, I get the error shown in the subject. What's the problem? (I already searched around and found this instance of the problem, but that doesn't seem to fit for my issue - unless I misunderstood something.)
CodePudding user response:
To specify a name of the library you should set the same name in your jenkins settings:
Name. An identifier you pick for this library, to be used in the @Library annotation. An environment variable library.THIS_NAME.version will also be set to the version loaded for a build (whether that comes from the Default version here, or from an annotation after the @ separator).
Your '{name}'
parameter inside of @Library()
means you should add a library with the same name. Because it's not a variable like "${name}"
which is not a built in variable and undefined.
If you wish to set up your library with the same name as your jenkins pipleine you could use env.JOB_NAME
variable, or check the all environment and pre-defined variables:
println env.getEnvironment()
Or check job parameters only:
println params
Now step-by-step instructions:
Create your library, for example from Git SCM as shown on the screenshot.
Put your library code to the project, e.g:
<project_root_folder>/vars/common_code.groovy
. You don't need your additional pathJenkins/Library
. Also you have named your file in 'snake case' style, which is not usual for groovy:
The vars directory hosts scripts that define global variables accessible from Pipeline. The basename of each *.groovy file should be a Groovy (~ Java) identifier, conventionally
camelCased
.
So your file in 'camel case' should looks CommonCode.groovy
.
- Write your library code:
// vars/commonCode.groovy
// Define your method
def call() {
// do some stuff
return 'Some message'
}
- Write your pipeline. Example of scripted pipeline:
#!/usr/bin/env groovy
// yourPipeline.groovy file in your project
@Library('jenkins-shared-library') _
// Get the message from the method in your library
def messageText = commonCode() as String
println messageText
If you wish to define some global variables this answer also may help you.
PS: Using 'vars' folder allows you to load everything from your vars folder once at the same time. If you wish to load dynamically use import from src folder.