2
votes

Since conditional approval doesn't work in azure yaml pipeline i've been trying a workaround using 2 environment in deployment stage, shown in yaml. using a conditions in job and a variable i want to check if approval required or not but when i run the pipeline , i see its still asking for approval even though the condition is not satisfied for the deployment job that requires approval. Post approval though the job that required approval skips as expected. I dont understand why its asking for approval.

  1. Are approval executed first for a stage before jobs conditions are evaluated?
  2. Did i miss something in the yaml?

trigger:
- none


variables:
 - group: pipelinevariables
   # Agent VM image name
 - name: vmImageName
   value: 'ubuntu-latest'

stages:

- stage: Deploy
  displayName: Deploy stage
  jobs:  
  - deployment: DeployWebWithoutApprval
    displayName: deploy Web App without approval
    condition: and(succeeded(),ne(variables.DEV_APPROVAL_REQUIRED,'true'))
    pool:
      vmImage: $(vmImageName)
    # creates an environment if it doesn't exist
    environment: 'app-dev'
    strategy:
      runOnce:
        deploy:
          steps:
          - script: echo No approval

  - deployment: DeployWebWithApprval
    displayName: deploy Web App with approval
    dependsOn: DeployWebWithoutApprval
    condition: and(eq(dependencies.DeployWebWithoutApprval.result,'Skipped'),eq(variables.DEV_APPROVAL_REQUIRED,'true'))
    pool:
      vmImage: $(vmImageName)
    # creates an environment if it doesn't exist
    environment: 'app-dev-with-approval'
    strategy:
      runOnce:
        deploy:
          steps:
          - script: echo requires approval


skipped job but the check ran

update : this works if i define 2 stages and and same set of conditions but that would show 2 stages in build details page which we don't want

Another question is Can we conditionally insert stage template based on variable value from variable group

  • stages ​${{ifeq(variables['Policy_Approval_Required'],'true')}}:
2

2 Answers

0
votes

Insert template conditionally is supported, you can check the following link: https://github.com/microsoft/azure-pipelines-agent/issues/1749. Check the following example:

- ${{ if eq(variables['Build.Reason'], 'PullRequest') }}:
  - template: sharedstep.yml@templates
    parameters:
       value: true
0
votes

I have had the exact same issue with approval gates and conditions. It is unfortunately not supported as of yet, but it's reported to Microsoft (here). There is also this issue. Seems like an issue with the order of evaluating approvals vs conditions.