Load Jenkins Pipeline Shared Library from same repository

前端 未结 7 2133
北恋
北恋 2021-01-31 07:52

TL;DR Is there a way to import code into the Jenkinsfile from the local repository (other than the load step)?

Why?

I\'ve ex

7条回答
  •  再見小時候
    2021-01-31 08:53

    I guess that proper way to do that is to implement a custom SCMRetriever.

    However, you can use the following hack:

    Assuming jenkins/vars/log.groovy in your local repo contains:

    def info(message) {
        echo "INFO: ${message}"
    }
    

    Your Jenkinsfile can load that shared library from the jenkins/ directory using library step:

    node('node1') { // load library
        checkout scm
        // create new git repo inside jenkins subdirectory
        sh('cd jenkins && git init && git add --all . && git commit -m init &> /dev/null') 
        def repoPath = sh(returnStdout: true, script: 'pwd').trim() + "/jenkins"
        library identifier: 'local-lib@master', retriever: modernSCM([$class: 'GitSCMSource', remote: repoPath])
    }
    
    node('node2') {
        stage('Build') {
            log.info("called shared lib") // use the loaded library
        }
    }
    

提交回复
热议问题