如何使用 Jenkins Pipeline 文件夹级共享库? [英] How to use Jenkins Pipeline Folder-Level Shared Library?
问题描述
我们的组件很少,它们存储在自己的 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 存储库中,我很想知道如何使用此处解释的文件夹级共享库"--> https://jenkins.io/doc/book/pipeline/shared-libraries/#folder-level-shared-libraries
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.
但是,您可以使用以下 hack:
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 Pipeline 文件夹级共享库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!