title: No such DSL method 'when' found among steps in Jenkinsfile
body: <p>I want to execute some stage in loop. I have Jenkinsfile</p>
  
  <pre class="lang-groovy prettyprint-override"><code>pipeline {
    agent any
    tools {}
    parameters {}
    environment {}
    stages {
      stage('Execute') {
        steps {
          script {
            for (int i = 0; i &lt; hostnameMap.size; i++) {
  
              hostname = hostnameMap[i]
              echo 'Executing ' + hostname
  
              stage('Backup previous build ' + hostname) {
                backup(hostname, env.appHome)
              }
  
  
              stage('Deploy ' + hostname) {
                when {
                  expression { env.BRANCH_NAME ==~ /(dev|master)/ }
                }
                steps {
                  script {
                    deploy(hostname , env.appHome, env.appName)
                  }
                }
              }
  
              stage('Restart ' + hostname) {
                when {
                  expression { env.BRANCH_NAME ==~ /(dev|master)/ }
                }
                steps {
                  script {
                    restart(hostname , env.appName, env.port)
                  }
                }
              }
  
            }
          }
        }
      }
  
    }
  }
  </code></pre>
  
  <p>But got error </p>
  
  <blockquote>
    <p>java.lang.NoSuchMethodError: No such DSL method 'when' found among
    steps</p>
  </blockquote>
  
  <p>Separately all of this stage works fine. Why I got this error?</p>