Declarative pipeline when condition in post : <p>As far as declarative pipelines go in Jenkins, I'm having trouble with the <em>when</em> keyword.</p>

<p>I keep getting the error <code>No such DSL method 'when' found among steps</code>.  I'm sort of new to Jenkins 2 declarative pipelines and don't think I am mixing up scripted pipelines with declarative ones.</p>

<p>The goal of this pipeline is to run <code>mvn deploy</code> after a successful Sonar run and send out mail notifications of a failure or success.  I only want the artifacts to be deployed when on master or a release branch.</p>

<p>The part I'm having difficulties with is in the <em>post</em> section.  The <strong>Notifications</strong> stage is working great.  Note that I got this to work without the <em>when</em> clause, but really need it or an equivalent.</p>

<pre><code>pipeline {
  agent any
  tools {
    maven 'M3'
    jdk 'JDK8'
  }
  stages {
    stage('Notifications') {
      steps {
        sh 'mkdir tmpPom'
        sh 'mv pom.xml tmpPom/pom.xml'
        checkout([$class: 'GitSCM', branches: [[name: 'origin/master']], doGenerateSubmoduleConfigurations: false, submoduleCfg: [], userRemoteConfigs: [[url: 'https://repository.git']]])
        sh 'mvn clean test'
        sh 'rm pom.xml'
        sh 'mv tmpPom/pom.xml ../pom.xml'
      }
    }
  }
  post {
    success {
      script {
        currentBuild.result = 'SUCCESS'
      }
      when { 
        branch 'master|release/*' 
      }
      steps {
        sh 'mvn deploy'
      }     
      sendNotification(recipients,
        null,             
        'https://link.to.sonar',
        currentBuild.result,
      )
    }
    failure {
      script {
        currentBuild.result = 'FAILURE'
      }    
      sendNotification(recipients,
        null,             
        'https://link.to.sonar',
        currentBuild.result
      )
    }
  }
}
</code></pre>
