AnswerBun.com

How do I load a Jenkins Shared Library in a Jenkins Job DSL seed?

DevOps Asked by David West on August 22, 2021

I have a seed job using the plugin Jenkins Job DSL. I also have a shared library.

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: [[email protected]]

How do I load a Jenkins Shared Library in a Jenkins Job DSL seed?

3 Answers

There are two option

  1. Using jenkins global shared lib setting in gui see documation https://www.jenkins.io/doc/book/pipeline/shared-libraries/

  2. Importing shared library inside jenkins file similart like this follow this blog https://tomd.xyz/jenkins-shared-library/

    library identifier: '[email protected]',
         //'master' refers to a valid git-ref
         //'mylibraryname' can be any name you like
         retriever: modernSCM([
           $class: 'GitSCMSource',
           credentialsId: 'your-credentials-id',
           remote: 'https://git.yourcompany.com/yourrepo/private-library.git'
     ])
    
     pipeline {
         agent any
         stages {
             stage('Demo') {
                 steps {
                     echo 'Hello world'
                     yourCustomStep 'your_arg'
                 }
             }
         }
    

P.S. When using "@Library _" the this means importing all libraries

Answered by Galpha on August 22, 2021

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:

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

Answered by Silk0vsky on August 22, 2021

Setup pipeline name and default version (e.g. master) in "Global Pipeline Libraries" settings. Then use them in your Jenkinsfile as following

#!/usr/bin/env groovy
@Library('[email protected]') _

pipeline {

}

Answered by chaks on August 22, 2021

Add your own answers!

Related Questions

how to set a variable from an ini file in ansible?

2  Asked on August 22, 2021 by a1an

 

Create service per pod in statefulset

1  Asked on August 22, 2021 by flowerprogrammer

   

Clarification about Operational Support System

1  Asked on August 22, 2021 by hansinee-nawanjana

   

Ansible – Handler not found on first attempt

3  Asked on August 22, 2021 by eric-saindon

 

Run command from Jenkins to Linux Server

1  Asked on August 22, 2021 by ankit-thakkar

   

Azure Devops Pipelines Triggers not working

2  Asked on August 22, 2021 by pradeep-upadhyay

     

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP