如何修复 Pipeline-Script “预期步骤"错误 [英] How to fix Pipeline-Script "Expected a step" error
问题描述
我正在尝试在 Jenkins 中运行一个包含 2 个阶段的简单管道脚本.脚本本身会创建一个 textFile 并检查它是否存在.但是当我尝试运行该作业时,我得到一个 预期一步" 错误.
I am trying to run a simple pipeline-script in Jenkins with 2 stages. The script itself creates a textFile and checks if this one exists. But when i try to run the job I get an "Expected a step" error.
我在某处读到,您不能在步骤中包含 if
,所以这可能是问题所在,但如果是这样,我如何在不使用 if
的情况下进行检查?p>
I have read somewhere that you cant have an if
inside a step so that might be the problem but if so how can I check without using the if
?
pipeline {
agent {label 'Test'}
stages {
stage('Write') {
steps {
writeFile file: 'NewFile.txt', text:
'''Sample HEADLINE'''
println "New File created..."
}
}
stage('Check') {
steps {
Boolean bool = fileExists 'NewFile.txt'
if(bool) {
println "The File exists :)"
}
else {
println "The File does not exist :("
}
}
}
}
}
我希望脚本创建一个NewFile.txt";在代理工作区中并向控制台打印文本以确认它存在.
I expect the script to create a "NewFile.txt" in the agents workspace and print a text to the console confirming that it exists.
但我实际上得到了两个 预期一步" 错误.在以 Boolean bool = ...
开头的行并在 if(bool) ...
But I actually get two "Expected a step" errors.
At the Line starting with Boolean bool = ...
and at if(bool) ...
推荐答案
您缺少声明式管道中所需的 script{}
-step.
You are missing a script{}
-step which is required in a declarative pipeline.
引用:
脚本步骤采用脚本管道块并执行在声明式管道中.
The script step takes a block of Scripted Pipeline and executes that in the Declarative Pipeline.
stage('Check') {
steps {
script {
Boolean bool = fileExists 'NewFile.txt'
if (bool) {
println "The File exists :)"
} else {
println "The File does not exist :("
}
}
}
}
这篇关于如何修复 Pipeline-Script “预期步骤"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!