# Azure Pipelines - ARM Template Deployment
# This pipeline validates and deploys Azure Resource Manager (ARM) templates
# across development, staging, and production environments.

trigger:
  branches:
    include:
      - main
      - develop
  paths:
    include:
      - arm-templates/*
      - .workspace/azure-pipelines-agent-arm.yml

pr: none

variables:
  - name: azureSubscription
    value: 'azure-service-connection'
  - name: resourceGroupName
    value: 'app-resource-group'
  - name: location
    value: 'eastus'
  - name: armTemplatePath
    value: 'arm-templates/main.json'
  - name: armParametersPath
    value: 'arm-templates/parameters'
  - group: dev-env-variables

stages:
  - stage: Validate
    displayName: Validate ARM Templates
    jobs:
      - job: ValidateTemplate
        displayName: Validate ARM Template Syntax
        pool:
          vmImage: 'ubuntu-latest'
        steps:
          - task: AzureResourceManagerTemplateDeployment@3
            displayName: Validate ARM Template
            inputs:
              deploymentScope: 'Resource Group'
              azureResourceManagerConnection: $(azureSubscription)
              subscriptionId: '$(subscriptionId)'
              action: 'Validation only'
              resourceGroupName: $(resourceGroupName)
              location: $(location)
              templateLocation: 'Linked artifact'
              csmFile: $(armTemplatePath)
              csmParametersFile: '$(armParametersPath)/dev.parameters.json'
              overrideParameters: '-environment $(environment)'

          - task: PublishBuildArtifacts@1
            displayName: Publish ARM Templates
            inputs:
              pathToPublish: 'arm-templates'
              artifactName: 'arm-templates'
              publishLocation: 'Container'

  - stage: DeployDev
    displayName: Deploy to Development
    dependsOn: Validate
    condition: succeeded()
    variables:
      - group: dev-env-variables
    jobs:
      - deployment: DeployDev
        displayName: Deploy ARM to Dev
        environment: 'dev'
        pool:
          vmImage: 'ubuntu-latest'
        strategy:
          runOnce:
            deploy:
              steps:
                - download: current
                  artifact: arm-templates

                - task: AzureResourceManagerTemplateDeployment@3
                  displayName: Deploy ARM Template to Dev
                  inputs:
                    deploymentScope: 'Resource Group'
                    azureResourceManagerConnection: $(azureSubscription)
                    subscriptionId: '$(subscriptionId)'
                    action: 'Create Or Update Resource Group'
                    resourceGroupName: 'app-dev-rg'
                    location: $(location)
                    templateLocation: 'Linked artifact'
                    csmFile: '$(Pipeline.Workspace)/arm-templates/main.json'
                    csmParametersFile: '$(Pipeline.Workspace)/arm-templates/parameters/dev.parameters.json'
                    overrideParameters: >
                      -environment dev
                      -debug true
                      -logLevel debug
                    deploymentMode: 'Incremental'

  - stage: DeployStaging
    displayName: Deploy to Staging
    dependsOn: DeployDev
    condition: succeeded()
    variables:
      - group: staging-env-variables
    jobs:
      - deployment: DeployStaging
        displayName: Deploy ARM to Staging
        environment: 'staging'
        pool:
          vmImage: 'ubuntu-latest'
        strategy:
          runOnce:
            deploy:
              steps:
                - download: current
                  artifact: arm-templates

                - task: AzureResourceManagerTemplateDeployment@3
                  displayName: Deploy ARM Template to Staging
                  inputs:
                    deploymentScope: 'Resource Group'
                    azureResourceManagerConnection: $(azureSubscription)
                    subscriptionId: '$(subscriptionId)'
                    action: 'Create Or Update Resource Group'
                    resourceGroupName: 'app-staging-rg'
                    location: $(location)
                    templateLocation: 'Linked artifact'
                    csmFile: '$(Pipeline.Workspace)/arm-templates/main.json'
                    csmParametersFile: '$(Pipeline.Workspace)/arm-templates/parameters/staging.parameters.json'
                    overrideParameters: >
                      -environment staging
                      -debug false
                      -logLevel info
                    deploymentMode: 'Incremental'

  - stage: DeployProd
    displayName: Deploy to Production
    dependsOn: DeployStaging
    condition: succeeded()
    variables:
      - group: prod-env-variables
    jobs:
      - deployment: DeployProd
        displayName: Deploy ARM to Production
        environment: 'production'
        pool:
          vmImage: 'ubuntu-latest'
        strategy:
          runOnce:
            deploy:
              steps:
                - download: current
                  artifact: arm-templates

                - task: AzureResourceManagerTemplateDeployment@3
                  displayName: Deploy ARM Template to Production
                  inputs:
                    deploymentScope: 'Resource Group'
                    azureResourceManagerConnection: $(azureSubscription)
                    subscriptionId: '$(subscriptionId)'
                    action: 'Create Or Update Resource Group'
                    resourceGroupName: 'app-production-rg'
                    location: $(location)
                    templateLocation: 'Linked artifact'
                    csmFile: '$(Pipeline.Workspace)/arm-templates/main.json'
                    csmParametersFile: '$(Pipeline.Workspace)/arm-templates/parameters/production.parameters.json'
                    overrideParameters: >
                      -environment production
                      -debug false
                      -logLevel warn
                      -sslEnabled true
                    deploymentMode: 'Incremental'
