1
我正在使用Jenkins共享庫。我應該在哪裏將函數放入管道文件中?
我的計劃是能夠將每個Jenkinsfile中的行數降至最低。
我希望能夠做一些事情,如:
buildProduct()
,它會發出它駐留在共享庫管道建設。
我standardPipeline.groovy文件看起來像這樣:
import com.company.utils.pipelineFunctions;
import com.company.utils.Git;
def ris = new pipelineFunctions()
def run_in_stage(String stage_name, Closure command){
ris.run_in_stage(stage_name, command, emailadd)
}
def call(body) {
def config = [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = config
body()
node {
// Clean workspace before doing anything
deleteDir()
try {
def ris = new pipelineFunctions()
//run_in_stage('Clone', {
run_in_stage('Clone', {
checkout scm
})
stage ('Build') {
sh "echo 'building ${config.projectName} ...'"
}
stage ('Tests') {
parallel 'static': {
sh "echo 'shell scripts to run static tests...'"
},
'unit': {
sh "echo 'shell scripts to run unit tests...'"
},
'integration': {
sh "echo 'shell scripts to run integration tests...'"
}
}
stage ('Deploy') {
sh "echo 'deploying to server ${config.serverDomain}...'"
sh "echo Itai ganot"
}
} catch (err) {
currentBuild.result = 'FAILED'
throw err
}
}
}
的pipelineFunctions.groovy文件看起來像這樣:
package com.company.utils;
import com.company.utils.Git;
def run_in_stage(String stage_name, Closure command, String sendTo){
def gitTool = new Git()
String ulink = gitTool.getCommitter()
String jlink = "(<${env.BUILD_URL}|Open>)"
println "============================================================"
stage (stage_name) {
try {
command()
if (currentBuild.result == 'FAILURE') {
error "Build failed, see log for further details."
}
println "============================================================"
} catch (Exception ex) {
def except = "${ex}"
String emailadd = ulink+'@company.com'
if (currentBuild.result == null) {
currentBuild.result = "FAILURE" }
this.notifyStatus(stage_name, currentBuild.result, except)
echo "Pipeline failed at stage: ${stage_name}"
throw ex
}
}
}
return this;
當我運行失敗,出現以下錯誤編譯:
groovy.lang.MissingPropertyException: No such property: ris for class: groovy.lang.Binding
我想了解我應該在哪裏放置run_in_stage函數def因爲我把它放在哪裏,這會導致構建失敗。
任何想法我做錯了什麼?
我仍然在run_in_stage函數內的「def ris = new pipelineFunctions()」中收到錯誤 –
@ItaiGanot我已經重新創建了上述和RU它。我得到的唯一錯誤是非常明確的:'groovy.lang.MissingPropertyException:沒有這樣的屬性:emailadd類:StandardPipeline'。我已經添加了上述修正(只是在範圍內聲明瞭一個名爲'emailadd'的東西)。這是你遇到的錯誤還是其他的東西? –