Forum Discussion

jbadam0312's avatar
jbadam0312
Copper Contributor
Sep 26, 2023

To write .yml file to deploy .net application in Azure Virtual Machine

Hi Team, 

 

I am planning to write .yml file to deploy the web app in Azure virtual Machine with different environments(Like Dev, QA , UAT and Prod). Here I want to use variable group as well, I can update the web.config file base environment. 

 

Highly appreciate , if any one has knowledge , please share sample example , that will be great help to me.

 

Thanks

Jagannath

 

1 Reply

  • Take this:

     

    Folder Structure Suggestion:

    /azure-pipelines
      ├── azure-pipelines.yml
      ├── templates/
      │   └── deploy-template.yml
      └── vars/
          ├── vars-dev.yml
          ├── vars-qa.yml
          ├── vars-uat.yml
          └── vars-prod.yml

     

    Sample Variable Group Template (vars/vars-dev.yml)

    variables:
      environmentName: 'Dev'
      webAppName: 'myapp-dev'
      vmGroup: 'vm-dev-group'
      configFilePath: 'Configs/web.dev.config'

    Repeat similarly for QA, UAT, and Prod.

     

    Main Pipeline (azure-pipelines.yml)

    trigger:
      branches:
        include:
          - main
    
    stages:
    - stage: DeployDev
      displayName: 'Deploy to Dev'
      jobs:
      - template: templates/deploy-template.yml
        parameters:
          env: 'dev'
          variableFile: 'vars/vars-dev.yml'
    
    - stage: DeployQA
      displayName: 'Deploy to QA'
      jobs:
      - template: templates/deploy-template.yml
        parameters:
          env: 'qa'
          variableFile: 'vars/vars-qa.yml'

     

    Deployment Template (templates/deploy-template.yml)

    parameters:
      env: ''
      variableFile: ''
    
    jobs:
    - job: DeployJob
      displayName: 'Deploy to ${{ parameters.env }}'
      variables:
        - template: ${{ parameters.variableFile }}
      steps:
        - task: AzureCLI@2
          inputs:
            azureSubscription: 'Your-Service-Connection'
            scriptType: 'pscore'
            scriptLocation: 'inlineScript'
            inlineScript: |
              Write-Host "Deploying to $env:environmentName"
              # Replace web.config based on environment
              Copy-Item $env:configFilePath -Destination '$(Build.ArtifactStagingDirectory)/web.config' -Force
              # Add your deployment logic here (e.g., Web Deploy, PowerShell Remoting)

     

Resources