title: Set dependsOn to previous stage without hardcoding the value in Azure Pipelines
body: <p>Using Azure Pipelines, how can you set <code>dependsOn</code> to the previous stage without hardcoding the value?</p>
  <p>Is there some sort of pre-defined variable I can use? I only saw for the current stage and not previous stages.</p>
  <p>My pipeline is performing a Terraform deployment. The first (build) stages are running a <code>terraform plan</code> in parallel for each environment which would then be used by the (deploy) stages that are running <code>terraform apply</code> using the plan file in each environment sequentially.</p>
  <p>I am using two different templates for the plan (build) and apply (deploy) stages. I am currently using a parameter to specify the <code>dependsOn</code> list values where I need to specify all the plan (build) stages explicitly however for the apply (deploy) stages, I was hoping to default value of the parameter to something like <strong>$(Build.PreviousStage)</strong> to make it a bit more elegant.</p>
  <pre class="lang-yaml prettyprint-override"><code># azure-pipelines.yml
  
  # LINES REMOVED #
  
  stages:
    # Terraform Plan (DevTest)
    - template: templates/plan.yml
      parameters:
        az_service_connection: DevTest-Terraform
        environment: DevTest
  
    # Terraform Plan (Stage)
    - template: templates/plan.yml
      parameters:
        az_service_connection: Stage-Terraform
        environment: Stage
  
    # Terraform Plan (Prod)
    - template: templates/plan.yml
      parameters:
        az_service_connection: Prod-Terraform
        environment: Prod
  
    # Terraform Deploy (DevTest)
    - template: templates/deploy.yml
      parameters:
        az_service_connection: DevTest-Terraform
        depends_on:
          - DevTest_Plan
          - Stage_Plan
          - Prod_Plan
        environment: DevTest
  
    # Terraform Deploy (Stage)
    - template: templates/deploy.yml
      parameters:
        az_service_connection: Stage-Terraform
        depends_on:
          - DevTest_Deploy
        environment: Stage
  
    # Terraform Deploy (Prod)
    - template: templates/deploy.yml
      parameters:
        az_service_connection: Prod-Terraform
        depends_on:
          - Stage_Deploy
        environment: Prod
  </code></pre>
  <pre class="lang-yaml prettyprint-override"><code># ./templates/deploy.yml
  
  parameters:
    - name: az_service_connection
      type: string
    - name: depends_on
      type: object
    - name: environment
      type: string
  
  stages:
    # Terraform Deploy
    - stage: ${{ parameters.environment }}_Deploy
      displayName: Terraform Deploy (${{ parameters.environment }})
      condition: and(succeeded(), in(variables['Build.Reason'], 'IndividualCI', 'BatchedCI', 'Manual'))
      dependsOn:
        ${{ parameters.depends_on }}
  
  # LINES REMOVED #
  </code></pre>