snippetMinor
How do I load a Jenkins Shared Library in a Jenkins Job DSL seed?
Viewed 0 times
howsharedjenkinsseeddsllibraryloadjob
Problem
I have a seed job using the plugin Jenkins Job DSL. I also have a shared library.
I have tried using the
How do I load a Jenkins Shared Library in a Jenkins Job DSL seed?
I have tried using the
@Library annotation to load the script and the library method. It cannot find the annotation and using library yields the following error:No signature of method: simple_pipeline.library() is applicable for argument types: (java.lang.String) values: [platform-engineering-library@master]How do I load a Jenkins Shared Library in a Jenkins Job DSL seed?
Solution
It's possible to use jobDSL from Pipeline. (Im using multibranch pipeline as it allows to configure pipelineTriggers)
You can configure your seed job to be a pipeline like this:
This way we can configure our seed job to checkout shared libraries repo into workspace subdirectory shared-library and specify additionalClasspath for jobDSL groovy scripts
So in your groovy scripts you can simply use import without @Library annotation
You can configure your seed job to be a pipeline like this:
def gitCredentialsId = 'github-jenkins'
def jobsRepoName = 'https://github.com/my-jobs-repo.git'
def sharedLibraryRepoName = 'https://github.com/shared-library-repo.git'
properties([
pipelineTriggers([githubPush()])
])
pipeline {
agent any
stages{
stage('Seed Job') {
agent any
steps {
checkout([
$class: 'GitSCM',
branches: [[name: '*/main']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'shared-library']],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: gitCredentialsId, url: sharedLibraryRepoName ]]
])
git url: jobsRepoName, changelog: false, credentialsId: gitCredentialsId, poll: false, branch: 'main'
jobDsl targets: 'jobs/**/*_job.groovy', additionalClasspath: 'shared-library/src'
}
}
}
}This way we can configure our seed job to checkout shared libraries repo into workspace subdirectory shared-library and specify additionalClasspath for jobDSL groovy scripts
So in your groovy scripts you can simply use import without @Library annotation
Code Snippets
def gitCredentialsId = 'github-jenkins'
def jobsRepoName = 'https://github.com/my-jobs-repo.git'
def sharedLibraryRepoName = 'https://github.com/shared-library-repo.git'
properties([
pipelineTriggers([githubPush()])
])
pipeline {
agent any
stages{
stage('Seed Job') {
agent any
steps {
checkout([
$class: 'GitSCM',
branches: [[name: '*/main']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'shared-library']],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: gitCredentialsId, url: sharedLibraryRepoName ]]
])
git url: jobsRepoName, changelog: false, credentialsId: gitCredentialsId, poll: false, branch: 'main'
jobDsl targets: 'jobs/**/*_job.groovy', additionalClasspath: 'shared-library/src'
}
}
}
}Context
StackExchange DevOps Q#11833, answer score: 4
Revisions (0)
No revisions yet.