Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 823
Baharul Avatar asked Sep 18 '25 13:09

Baharul


1 Answers

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

like image 198
Michael Kemmerzell Avatar answered Sep 20 '25 10:09

Michael Kemmerzell