Baharul
Baharul

Reputation: 143

Step expected in Jenkins Groovy script

I am having below groovy script for my Jenkins pipeline. But when running its giving error as step expected where my script already having step. Can anyone suggest what's wrong here..

Script file

pipeline {
agent any
stages {
    stage('Workspace Preparation') {
        steps {
            sh """
                rm -rf ${workspace}/*
            """
        }
    }
   stage('Get Deployment files') {
       steps {
            dir("${workspace}/deployfiles") {
                if("${params.componentType}"=="A") {
                    echo "A component deployment"
                    checkout(## necessary step)
                }
                else if ("${params.componentType}"=="B") {
                    echo "B component deployment"
                    checkout(## necessary step)
                }
                else  {
                    echo "Invalid"
                }


              }
       }
    }
}

}

Getting error as

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 19: Expected a step @ line 14, column 6.
                    if("${params.componentType}"=="A") {
        ^
enter code here
enter code here

Upvotes: 4

Views: 2308

Answers (1)

Michael Kemmerzell
Michael Kemmerzell

Reputation: 5256

You are missing a script-block. (Source)

Such a block gives you acces to execute groovy code (for, if-else etc. etc.)

stage('Check') {
        steps {        
            script { // Allows to execute groovy code
               dir (...) {
                 if (...)
               }    
            }         
        }

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

Upvotes: 4

Related Questions