问题描述
我们的项目使用具有通用管道阶段的 jenkins-shared-library。我们正在考虑添加一个阶段,该阶段将检查代码覆盖率并在未达到覆盖率目标时使管道失败。 jenkins 提供的 Cobertura 插件能够做到这一点,但我在实现它时面临挑战。有没有办法在我们的 jenkinsfile 中添加自定义管道阶段,该阶段将在共享库代码作为同一管道的一部分运行之后运行?是否可以导入 2 个共享库并将它们一起用作同一管道的一部分?我对此比较陌生,非常感谢任何帮助。谢谢!
解决方法
回答您的问题:
有没有办法在我们的 jenkinsfile 中添加自定义管道阶段,该阶段将在共享库代码作为同一管道的一部分运行后运行?
是否可以导入 2 个共享库并将它们一起用作同一管道的一部分?
对这两个问题都是肯定的,您可以在管道中添加任意数量的自定义阶段,并且它也可以在共享库代码运行后运行。
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 file)
#!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