Jenkinsfile管道错误:和“未定义的部分" [英] Jenkinsfile Pipeline errors: "expected a symbol" and "undefined section"
问题描述
任何人都可以解释为什么我得到以下错误,以及对他们来说可能的解决方法是什么?
Can anyone explain why I get the following errors, and what can be a possible solution for them?
org.codehaus.groovy.control.MultipleCompilationErrorsException: 启动失败:WorkflowScript:20:应在第20行输入符号, 专栏4. 环境{
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: WorkflowScript: 20: Expected a symbol @ line 20, column 4. environment {
WorkflowScript:17:未定义的部分错误" @第17行,第1列.
管道{
WorkflowScript: 17: Undefined section "error" @ line 17, column 1.
pipeline {
Jenkins文件中的代码如下:
The code in the Jenkinsfile is as follows:
#!groovy
def application, manifest, git, environment, artifactory, sonar
fileLoader.withGit('git@<reducted>', 'v1', 'ssh-key-credential-id-number') {
application = fileLoader.load('<reducted>');
manifest = fileLoader.load('<reducted>');
git = fileLoader.load('<reducted>');
environment = fileLoader.load('<reducted>');
}
pipeline {
agent { label 'cf_slave' }
environment {
def projectName = null
def githubOrg = null
def gitCommit = null
}
options {
skipDefaultCheckout()
}
stages {
stage ("Checkout SCM") {
steps {
checkout scm
script {
projectName = git.getGitRepositoryName()
githubOrg = git.getGitOrgName()
gitCommit = manifest.getGitCommit()
}
}
}
stage ("Unit tests") {
steps {
sh "./node_modules/.bin/mocha --reporter mocha-junit-reporter --reporter-options mochaFile=./testResults/results.xml"
junit allowEmptyResults: true, testResults: 'testResults/results.xml'
}
}
//stage ("SonarQube analysis") {
//...
//}
// stage("Simple deploy") {
// steps {
// // Login
// sh "cf api <reducted>"
// sh "cf login -u <reducted> -p <....>"
//
// // Deploy
// sh "cf push"
// }
// }
}
post {
// always {
// }
success {
sh "echo 'Pipeline reached the finish line!'"
// Notify in Slack
slackSend color: 'yellow', message: "Pipeline operation completed successfully. Check <reducted>"
}
failure {
sh "echo 'Pipeline failed'"
// Notify in Slack
slackSend color: 'red', message: "Pipeline operation failed!"
//Clean the execution workspace
//deleteDir()
}
unstable {
sh "echo 'Pipeline unstable :-('"
}
// changed {
// sh "echo 'Pipeline was previously failing but is now successful.'"
// }
}
}
推荐答案
您的管道通常都很好-在Declarative pipeline
块之前添加脚本化管道元素通常不是问题.
Your Pipeline is mostly fine — adding Scripted Pipeline elements before the Declarative pipeline
block is generally not a problem.
但是,从一开始,您就定义了一个名为environment
(和git
)的变量,该变量基本上覆盖了各种Pipeline插件所声明的元素.
However, at the very start, you're defining an variable called environment
(and git
), which are basically overriding the elements declared by the various Pipeline plugins.
即当您尝试执行pipeline { environment { … } }
时,environment
指的是变量声明,这会导致出错.
i.e. When you attempt to do pipeline { environment { … } }
, the environment
is referring to your variable declaration, which causes things to go wrong.
重命名这两个变量,您将修复第一条错误消息.
Rename those two variables, and you'll fix the first error message.
要修复第二条错误消息,请从environment
块中删除声明变量的尝试-该块仅用于
To fix the second error message, remove the attempts to declare variables from the environment
block — this block is only intended for exporting environment variables for use during the build steps, e.g.:
environment {
FOO = 'bar'
BAR = 'baz'
}
如果没有这些声明,您拥有的script
块将正常工作.另外,您可以将这些变量声明移到脚本的顶层.
The script
block you have will work fine without these declarations. Alternatively, you can move those variable declarations to the top level of your script.
这篇关于Jenkinsfile管道错误:和“未定义的部分"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!