添加附加阶段到 jenkinsfile 以及导入 jenkins-shared-libraries 代码

Add additional stage to jenkinsfile along with importing jenkins-shared-libraries code

我们的项目使用具有通用管道阶段的 jenkins-shared-library。我们正在考虑添加一个阶段,该阶段将检查代码覆盖率并在未达到覆盖率目标时使管道失败。 jenkins 提供的 Cobertura 插件能够做到这一点,但我在实施它时面临挑战。有没有办法在我们的 jenkinsfile 中添加一个自定义管道阶段,它将 运行 在共享库代码 运行 之后全部作为同一管道的一部分?是否可以导入 2 个共享库并将它们作为同一管道的一部分一起使用?我对此比较陌生,非常感谢任何帮助。谢谢!

回答您的问题:
有没有办法在我们的 jenkinsfile 中添加一个自定义管道阶段,在共享库代码 运行 之后 运行 全部作为同一管道的一部分?
是否可以导入 2 个共享库并将它们作为同一管道的一部分一起使用?
对这两个问题都是肯定的,您可以在管道中添加任意数量的自定义阶段,也可以 运行 在共享库代码 运行ning 之后.
Jenkinsfile 和新的共享库文件示例如下:

# stagelibrary variable will be used later to contain old_stagelibraries and is filled in # stage ('Old stage')
def oldstagelibrary

# newstagelibrary variable will contain path of your new sharedlibrary
def newstagelibrary
stage('Old stage') {   
            steps {
              script {
                        // Load Shared library Groovy file old_stagelibraries.Give your path of old_stagelibraries file which is created
                        oldstagelibrary = load 'C:\Jenkins\old_stagelibraries'
                        // Execute your function available in old_stagelibraries.groovy file.
                        oldstagelibrary.MyOld_library()       
                      }               
                  }
        }
# Add your new stage in the Jenkinsfile and use your new_stagelibraries file that is created
stage('New stage') {   
            steps {
              script {
                        // Load Shared library Groovy file new_stagelibraries which will contain your new functions.Give your path of new_stagelibraries file which is created
                        newstagelibrary = load 'C:\Jenkins\new_stagelibraries'
                        // Execute your function MyNew_library available in new_stagelibraries.groovy file.
                        newstagelibrary.MyNew_library()       
                      }               
                  }
        }

创建一个名为:new_stagelibraries(groovy 文件)

的文件
#!groovy
// Write or add Functions(definations of stages) which will be called from your jenkins file
def MyNew_library()
 {
     echo "Function execution of MyNew_library"
     // You can add yoiur functionality here
 }

return this