如何修复Pipeline-Script“预期步骤"错误 [英] How to fix Pipeline-Script "Expected a step" error

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

问题描述

我试图在jenkins中运行一个简单的管道脚本,分为两个阶段. 脚本本身会创建一个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.

('Write')阶段似乎工作得很好,因此与('Check')阶段一样.

the ('Write') stage seems to work perfectly fine so its something with the ('Check') stage.

我读过某个地方的某个步骤中不能有if的信息,这可能是该问题或一个问题,但是如果是这样,我如何不使用if进行检查?

I have read somewhere that you cant have an if inside a step so that might be the or one 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
                This is the secondary HEADLINE ...
                In this third Line below the HEADLINE we will write some larger Text, to give the HEADLINE some Context lets see how that ends up looking. HEADLINE ... HEADLINE ... This should be long enough ...'''
                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",然后将文本打印到控制台以确认其存在.

I expect the script to create a "NewFile" 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" error. At the Line starting with Boolean bool = ... and at if(bool) ...

推荐答案

您缺少script块. 引用(来源):

You are missing a script block. Quote (Source):

脚本步骤需要一段脚本管道,并执行该脚本 在声明式管道中.

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

基本上,您可以在脚本块中使用所需的所有内容. Groovy,if,try-catch等等等.

Basically in the script block you can use everything you want. Groovy, if, try-catch etc. etc.

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

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