首页 > 解决方案 > 组合多个jenkins共享库

问题描述

是否可以组合多个共享的 Jenkins 库?

例如,我有一个公共共享库:my-shared-library(带有 maven 项目的 git 存储库)在包含一些作业的 jenkins 文件夹中定义。在该文件夹中运行的每个作业都可以使用 Jenkinsfile 中的共享库:

@Library("my-shared-library") _
import com.pipelines.Pipeline
new Pipeline().build()

现在我想创建另一个共享库:my-specialized-shared-library,其中包含一些专用管道(在另一个 git 存储库中也作为 maven 项目)。my-specialized-shared-library中的管道(groovy 类、脚本等)应该能够从以下位置使用/导入类、管道等: my-shared-library是可能的,如果可以,推荐的方法是什么?

标签: jenkins

解决方案


在管理 Jenkins > 配置系统中,我从不同的 URL 定义了 2 个不同的全局管道库。

  • 全球图书馆-1
  • 全球图书馆-2

.jenkins 文件:

@Library('GlobalLibrary-1@some_branch') l1 //you can write here _ (underscore) or any other string.
@Library('GlobalLibrary-2@any_branch') l2
import com.lib1.Class1;  // import from GlobalLibrary-1 (Jenkins automatically finds com.lib.Class1 inside GlobalLibrary-1)
import com.prodcode.Class2;  // import from GlobalLibrary-2 (Jenkins automatically finds com.prodcode.Class2 inside GlobalLibrary-2)

node {
    new Class1();
    new Class2();
}

我什至可以在Class2.groovy中使用导入来从 GlobalLibrary -1获取类。


推荐阅读