简体   繁体   English

Jenkins管道文件夹级共享库如何使用?

[英]How to use Jenkins Pipeline Folder-Level Shared Library?

We have few components which is stored in their own git repositories.我们有一些组件存储在他们自己的 git 存储库中。 Specific combination of those components are built and delivered as solutions for different type of deployments/customers.这些组件的特定组合作为针对不同类型的部署/客户的解决方案构建和交付。 So, we have a pipeline git repository which has multiple Jenkinsfile (with different names - and so the build names).因此,我们有一个管道 git 存储库,它有多个 Jenkinsfile(具有不同的名称 - 构建名称也是如此)。

Obviously, there are many things common between these pipelines.显然,这些管道之间有许多共同之处。 I'm aware of Jenkins shared library and it works when they're given their own git repository.我知道 Jenkins 共享库,当它们被赋予自己的 git 存储库时它可以工作。 But, since my pipelines are already in dedicated git repository, I'm curious to know how to use "Folder-level Shared Libraries" explained here --> https://jenkins.io/doc/book/pipeline/shared-libraries/#folder-level-shared-libraries但是,由于我的管道已经在专用的 git 存储库中,我很想知道如何使用此处解释的“文件夹级共享库” --> https://jenkins.io/doc/book/pipeline/shared-libraries /#文件夹级共享库

But, I'm not able to figure out how to use this Folder-level shared libraries.但是,我不知道如何使用这个文件夹级共享库。 I couldn't find any examples/documentation for this style of libraries.我找不到这种类型的库的任何示例/文档。

Any pointers to documentation/example - or guidelines on how to go with this will be greatly appreciated.任何指向文档/示例的指针 - 或有关如何使用此 go 的指南将不胜感激。

Thanks.谢谢。

I guess that proper way to do that is to implement a custom SCMRetriever and use library step. 我想这样做的正确方法是实现自定义SCMRetriever并使用library步骤。

However, you can use the following hack: 但是,您可以使用以下hack:

Assuming jenkins/vars/log.groovy in your local repo contains: 假设您的本地 jenkins/vars/log.groovy中的jenkins/vars/log.groovy包含:

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

Your Jenkinsfile can load that shared library from the jenkins/ directory using library step: 您的Jenkinsfile可以使用library步骤从jenkins/目录加载该共享库:

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
    }
}

First: To use the Folder-level shared library, you have to create the standard library structure and commit it to a SCM repository.第一:要使用文件夹级共享库,您必须创建标准库结构并将其提交到 SCM 存储库。

Second: You have to configure jenkins to use the library in the options dialog of the folder where your pipeline is located.第二:您必须配置 jenkins 才能在管道所在文件夹的选项对话框中使用该库。

Simple example with scripted pipeline and no extra repository for the library:使用脚本管道的简单示例,没有额外的库存储库:

"<URL>/my-test-repo.git"
    |
    |- <JenkinsPipelineScript>
    |
    |- "jenkins_lib"
        |
        |- resources
        |- src
        |- vars
            |- "HelloWorld.groovy"

So in your JenkinsPipelineScript just import the library by所以在你的 JenkinsPipelineScript 中只需导入库

@Library('jenkins_lib') _

and call the "HelloWord.groovy" by并调用“HelloWord.groovy”

HelloWorld()

The HelloWorld.groovy can look like this: HelloWorld.groovy 可能如下所示:

def call() {
sh "echo Hello world from library-script!" 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM