如何使用Jenkins管道文件夹级共享库? [英] How to use Jenkins Pipeline Folder-Level Shared Library?

查看:679
本文介绍了如何使用Jenkins管道文件夹级共享库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们只有很少的组件存储在他们自己的git仓库中.这些组件的特定组合作为针对不同类型的部署/客户的解决方案而构建和交付.因此,我们有一个管道git存储库,其中包含多个Jenkinsfile(具有不同的名称-以及构建名称).

We have few components which is stored in their own git repositories. 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).

很显然,这些管道之间有很多共同点.我知道Jenkins共享库,当给他们自己的git仓库时,它就可以工作.但是,由于我的管道已经在专用的git存储库中,所以我很想知道如何使用此处解释的文件夹级共享库"->

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

但是,我无法弄清楚如何使用此文件夹级别的共享库.我找不到此类库的任何示例/文档.

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.

谢谢.

推荐答案

我猜想,实现此目标的正确方法是实现自定义SCMRetriever并使用library步骤.

I guess that proper way to do that is to implement a custom SCMRetriever and use library step.

但是,您可以使用以下技巧:

However, you can use the following hack:

假定本地存储区中的jenkins/vars/log.groovy 包含:

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

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

您的Jenkinsfile可以使用library步骤从jenkins/目录加载该共享库:

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

这篇关于如何使用Jenkins管道文件夹级共享库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆