Jenkins - Shared Libraries
Create Shared Library
your-library
|
vars/
// this is where your steps go
buildExecutable.groovy
runTests.groovy
deployExecutable.groovy
src/
//
- Add Shared Library into Jenkins
- Pull the Shared Library into your pipeline
Register the Library in Jenkins
- Manage Jenkins > Configure System > Under Global Pipeline Libraries add a library
Use the Library in a Pipeline
- At the top of your
Jenkinsfile
, place the following: @Library('your-library')_
- don't forget the trailing underscore (it's not a typo)
- You can then reference your steps
buildExecutable('foo-bear')
@Library('your-library')_
stage ('Build')
{
buildExecutable ('foo-bear')
}
- Steps - custom steps that you want to be available to your Jenkins pipelines
vars/YourStepName.groovy
#!/usr/bin/env groovy
// vars/YourStepName.groovy
// you must implement the call() method
def call()
{
// ...
}
- Other Common Code - helper lasses or common code that you might want to include inside pipeline steps
- E.g. static constants used throughout your pipelines
src/your/package/name/Foo.groovy
- You can then import this class into your Jenkinsfile and reference the static variable as
Foo.bar
#!/usr/bin/env groovy
package your.package.name
class Foo
{
static String bar = "foo-bar!"
}
//
def call()
{
// ...
}
Acknowledgements