如何修复流水线脚本“预期的一步"错误 [英] How to fix Pipeline-Script "Expected a step" error

查看:10
本文介绍了如何修复流水线脚本“预期的一步"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 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 的情况下进行检查?

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 :("
            }   
        }         
    }
}

这篇关于如何修复流水线脚本“预期的一步"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆