您可以使用Pipeline Shared Groovy Library插件来拥有一个库,供您所有项目在git存储库中共享。在文档中,您可以详细了解它。
如果您有许多非常相似的管道,则全局变量机制提供了一种方便的工具来构建可捕获相似性的高级DSL。例如,所有Jenkins插件都是以相同的方式构建和测试的,因此我们可以编写一个名为buildPlugin的步骤:
// vars/buildPlugin.groovydef call(body) { // evaluate the body block, and collect configuration into the object def config = [:] body.resolveStrategy = Closure.DELEGATE_FIRST body.delegate = config body() // now build, based on the configuration provided node { git url: "https://github.com/jenkinsci/${config.name}-plugin.git" sh "mvn install" mail to: "...", subject: "${config.name} plugin build", body: "..." }}假设脚本已作为全局共享库或文件夹级共享库加载,则生成的Jenkinsfile将大大简化:
Jenkinsfile(脚本管道)
buildPlugin { name = 'git'}该示例显示jenkinsfile如何将name = git传递给库。我目前使用类似的设置,对此非常满意。



