title: Locking resource in Jekinsfile pipeline for parallel and sequential stages at one time
body: <p>I'm trying to run the following process in my Jenkinsfile:</p>
  
  <ol>
  <li>Build the app</li>
  <li>Trigger deploy of two components to the test environment, in parallel
  
  <ul>
  <li>foo deploy</li>
  <li>bar deploy</li>
  </ul></li>
  <li>Run tests on a deployed app</li>
  </ol>
  
  <p>The steps 2 and 3 require locking a resource because I have only one test environment available.</p>
  
  <p>There's no problem to achieve that w/o running step 2 in parallel, however, when I configure Jenkinsfile to run them together I get the following error from Jenkins:</p>
  
  <pre><code>WorkflowScript: 19: Parallel stages or branches can only be included in a top-level stage. @ line 19, column 7.
                      stage('Deploy Foo') {
       ^
  </code></pre>
  
  <p>Here's the full Jenkinsfile:</p>
  
  <pre><code>pipeline {
      agent any
  
      stages {
          stage('Build') {
              steps {
                  powershell(script: '.\\ci\\build.ps1 -Script .\\ci\\build\\build.cake')
              }
          }
  
          stage('Deploy and run tests') {
              when {
                  branch('develop')
              }
  
              options {
                  lock('test-env')
              }
  
              stages {
                  stage('Deploy') {
                      parallel {
                          stage('Deploy Foo') {
                              steps {
                                  build(job: 'Deploy_Foo')
                              }
                          }
  
                          stage('Deploy Bar') {
                              steps {
                                  build(job: 'Deploy_Bar')
                              }
                          }
                      }
                  }
  
                  stage('Run tests') {
                      steps {
                          powershell(script: '.\\ci\\build.ps1 -Script .\\ci\\test\\build.cake')
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>I have also tried a solution with locking <strong>test-env</strong> resource separately for Deploy and Test stages, however, this increases the risk of race condition when some other running job may wait for that resource and "jump in" between Deploy and Test stages of the current job.</p>
  
  <p>Is there any way to achieve such mix of sequential and parallel stages as described above in Jenkinsfile?</p>