How to fix Pipeline-Script “Expected a step” error

前端 未结 2 1945
梦谈多话
梦谈多话 2020-12-20 12:08

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

相关标签:
2条回答
  • 2020-12-20 12:19

    You are missing a script{} step.

    Quote:

    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 :("
                }   
            }         
        }
    }
    
    0 讨论(0)
  • 2020-12-20 12:27

    There are multiple reasons why you may get the "Expected a step" error.

    Mine occurred because I used single quotes ' to surround a step script instead of double quotes ". For example:

    stage("Build") {
        steps {
            sh "./build.sh ${SECRET_KEY}"
        }
    }
    

    The string used above uses string interpolation (or I guess it's called a "templateable string"?), which won't work for a single-quoted string.

    Thought I'd add this answer here in case someone comes from Google and the accepted answer doesn't work!

    0 讨论(0)
提交回复
热议问题