Jenkinsfile - 调用多个 groovy 脚本

Jenkinsfile - calling multiple groovy scripts

我们在 gitlab 上有一个名为 mainlibrary 的共享库,它有很多 groovy 个文件。

mainlibrary gitlab repo 中的示例我们有以下文件。

startup_pipeline.groovy
cleanup_pipeline.groovy

在我们的一项 Jenkins 作业中,我们需要在 Jenkinsfile 中包含多个 groovy 文件。这可能吗?

这是 Jenkinsfile 的样子:

@Library('mainlibrary')_
startup_pipeline(email:'example@example.com')

我可以像这样将第二个 groovy 函数文件包含到这个 Jenkinsfile 中吗?

@Library('mainlibrary')_
startup_pipeline(email:'example@example.com'),
cleanup_pipeline(email:'example@example.com')

考虑到 cleanup_pipeline.groovy 位于 vars 文件夹下,并且您的代码内部已经有完整的声明,您的示例可能有效,并且可以包含第二个文件。唯一的修改是额外的逗号:

@Library('mainlibrary')_
startup_pipeline(email:'example@example.com')
cleanup_pipeline(email:'example@example.com')

或者可以在src下导入,不过我没用过这种方法。 通常,我把主要逻辑放在vars里面,其他复杂的东西去src.

基于 Jenkins documentation,请参阅 目录结构定义自定义步骤

视频step by step building a shared library,如果您不确定结构,可以构建和测试类似的东西。