如何修复管道脚本“预期一步”错误

Sat*_*ras 26 groovy jenkins jenkins-pipeline

我正在尝试在 Jenkins 中运行一个简单的管道脚本,分为 2 个阶段。脚本本身会创建一个 textFile 并检查它是否存在。但是,当我尝试运行该作业时,出现“预期的步骤”错误。

我在某处读到过,你不能有一个if内部步骤,所以这可能是问题所在,但如果是这样,我如何在不使用的情况下进行检查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 :("
                }            
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望脚本在代理工作区中创建一个“NewFile.txt”并将文本打印到控制台以确认它存在。

但我实际上得到了两个“预期的一步”错误。在以Boolean bool = ... 和开始的线路上if(bool) ...

mke*_*erz 54

You are missing a script{} -step which is required in a declarative pipeline.

Quote:

脚本步骤采用一个脚本管道块,并在声明式管道中执行它。

stage('Check') {
    steps {        
        script {
            Boolean bool = fileExists 'NewFile.txt'
            if (bool) {
                println "The File exists :)"
            } else {
                println "The File does not exist :("
            }   
        }         
    }
}
Run Code Online (Sandbox Code Playgroud)