在导入jenkins共享库代码的同时,向jenkinsfile添加额外的stage



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

要回答您的问题:
有没有办法在我们的jenkinsfile中添加一个自定义管道阶段,该阶段将在共享库代码作为同一管道的一部分运行后运行
是否可以导入两个共享库,并将它们作为同一管道的一部分一起使用
对这两个问题都是,您可以在管道中添加任意数量的自定义阶段,也可以在共享库代码运行后运行
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

最新更新