How to dynamically change variables in YAML pipeline based on trigger

Viewed 10

I am new to YAML. I have environments and variables specified per environment. However, I have couple of items where I could use help.

  1. I am trying to understand how do I specify variable groups and environments based on trigger or target branch? Here is what I am looking for.
  2. How do I trigger deployment to two environments based on one trigger?
    1. If trigger is "master" then deploy to "Prod" and "Dev" environments
    2. If trigger is "feature/*" then deploy to "QA" environment

Follows the psudo code. I understand that "Build.TargetBranchName" is not a variable, but I am looking for something similar to achieve selective deployment.

trigger:
  branches:
    include:
      - master
      - feature/*
      - bundle/*

variables:
- ${{if eq(variables['Build.TargetBranchName'], 'feature/123-mybranch')}}: 
  - group: dw-qa
  - name: 'sql-server'
    value: 'QA001.SEMPRA.COM'
  - name: 'environment'
    value: 'QA'
- ${{if eq(variables['Build.TargetBranchName'], 'master')}}: 
  - group: dw-dev
  - name: 'sql-server'
    value: 'DV001.SEMPRA.COM'
  - name: 'environment'
    value: 'DEV'
- ${{if eq(variables['Build.TargetBranchName'], 'master')}}: 
  - group: dw-prod
  - name: 'sql-server'
    value: 'PD001.SEMPRA.COM'
  - name: 'environment'
    value: 'PROD'

stages:
- stage: Build
  jobs:
  - job: Build_Solution
    displayName: 'Build Solution'
    pool:
      name: 'Shared Server Pool'   
    steps:
      - script: md BuildOutput
        displayName: 'Create BuildOutput folder'
  
      - task: VSBuild@1
        displayName: 'Visual Studio Build'
        inputs:
          solution: 'DataWarehouse/DataWarehouse.sln'
          platform: 'Any CPU'
          configuration: 'Debug'
  
      - task: PublishPipelineArtifact@1
        displayName: 'Publish artifacts from BuildOutput folder'
        inputs:
          targetPath: 'BuildOutput'
          artifact: 'DacpacAndConfig'
          publishLocation: 'pipeline'

- stage: Deploy
  jobs:
  - deployment:
    pool:
      name: 'SEU WinTel VX Shared'   
    environment: '$(environment)'
    strategy:
      runOnce:
        deploy:
          steps:
          - script: md DeploymentSource
            displayName: 'Create DeploymentSource folder'
      
          - task: DownloadPipelineArtifact@2
            displayName: 'Download artifacts to DeploymentSource folder'
            inputs:
              buildType: 'current'
              artifactName: 'DacpacAndConfig'
              targetPath: 'DeploymentSource'
      
          - task: SqlAzureDacpacDeployment@1
            displayName: 'Deploy STAGE'
            inputs:
              azureSubscription: 'My-MSSQL-Subscription'
              ServerName: '$(sql-server)'
              SqlUsername: 'ado-dw'
              SqlPassword: '$(sql-password)'
              DatabaseName: 'STAGE'
              PublishProfile: 'DeploymentSource\STAGE.publish.xml'
              DacpacFile: 'DeploymentSource\STAGE.dacpac'

Thanks in advance.

Palak

0 Answers
Related