0
votes

I have a question regarding yaml conditions. I followed this documentation: https://docs.microsoft.com/en-us/azure/devops/pipelines/process/conditions?view=azure-devops&tabs=yaml

I need to deploy to each environment based on some conditions. For example,

  • deploy to 'int' environment only if source branch is 'develop'
  • deploy to 'ua' environment only if source branch is 'master'
  • deploy to 'prod' environment only if source branch is 'master'

If any of these conditions is true, deploy to that environment.

I wrote a condition as follows:

condition: |
      or(
        and(
          eq(variables['Build.SourceBranch'], 'refs/heads/develop'),
          eq('${{ parameters.environmentAbbreviation }}', 'int')
        ),
        and(
          eq(variables['Build.SourceBranch'], 'refs/heads/master'),
          eq('${{ parameters.environmentAbbreviation }}', 'ua')
        ),
        and(
          eq(variables['Build.SourceBranch'], 'refs/heads/master'),
          eq('${{ parameters.environmentAbbreviation }}', 'prod')
        )
      )

This is what I get while running build/release:

Evaluating:

or(and(eq(variables['Build.SourceBranch'], 'refs/heads/develop'), eq('int', 'int')), and(eq(variables['Build.SourceBranch'], 'refs/heads/master'), eq('int', 'ua')), and(eq(variables['Build.SourceBranch'], 'refs/heads/master'), eq('int', 'prod')))

Expanded:

or(and(eq('refs/heads/develop', 'refs/heads/develop'), eq('int', 'int')), and(eq('refs/heads/develop', 'refs/heads/master'), eq('int', 'ua')), and(eq('refs/heads/develop', 'refs/heads/master'), eq('int, 'prod')))

Because the first condition is correct, my assumption is that the deployment would start in int environment. But this didn’t deploy to int environment. Why is that?

1
this condition doesnt make sense to me. it will fire in all of the 3 cases, so whats the point? shouldn't you have 3 different steps with 3 different conditions? also, what doesnt work, you didnt really specify that. how do you verify the condition is not working4c74356b41

1 Answers

1
votes

Not sure what is your complete YAML script, but the condition should work to deploy with int environment.

I just run it and it works fine. Here is what the test YAML I test with, and you can have a check with yours:

parameters:
- name: 'environmentAbbreviation'
  type: string
  default: int

stages:
- stage: DeployToDevelopment
  displayName: Deploy to 
  jobs:
  - deployment: DeployDev
    condition: or(and(eq(variables['Build.SourceBranch'], 'refs/heads/develop'), eq('${{ parameters.environmentAbbreviation }}', 'int')), and(eq(variables['Build.SourceBranch'], 'refs/heads/master'), eq('${{ parameters.environmentAbbreviation }}', 'ua')), and(eq(variables['Build.SourceBranch'], 'refs/heads/master'), eq('${{ parameters.environmentAbbreviation }}', 'prod')))
    environment: ${{ parameters.environmentAbbreviation }}
    strategy:
      runOnce:
        deploy:
          steps:
          - pwsh: |
              Write-Host ${{ parameters.environmentAbbreviation }}
            name: outputVars

enter image description here