⚙️ Day 16: Environment Configuration - Hands-On Guide

Managing variables and configuration across environments (Windows)

Duration: 4 hours | Module: Release Management

Lab 1: Create Variable Groups 📦

Objective: Create variable groups for different environments
Time: 30 minutes

Exercise 1.1: Create Development Variable Group

1 Access Library
  • Go to Azure DevOps → Pipelines
  • Click "Library" (in left menu)
  • You'll see Variable groups page
2 Create Dev Variable Group
  • Click "+ Variable group"
  • Variable group name: dev-config
  • Description: Development environment configuration
3 Add Variables

Click "+ Add" and add these variables:

Name Value
dbServer dev-sql.database.windows.net
dbName CalculatorDB_Dev
apiUrl https://dev-api.example.com
logLevel DEBUG
environment Development
4 Save Variable Group
  • Review all variables
  • Click "Save" button (top-right)
  • Variable group is now created!

Exercise 1.2: Create Staging and Production Groups

1 Create Staging Variable Group

Name: staging-config

Name Value
dbServer staging-sql.database.windows.net
dbName CalculatorDB_Staging
apiUrl https://staging-api.example.com
logLevel INFO
environment Staging
2 Create Production Variable Group

Name: prod-config

Name Value
dbServer prod-sql.database.windows.net
dbName CalculatorDB_Production
apiUrl https://api.example.com
logLevel ERROR
environment Production
3 Verify All Groups Created
  • Go to Pipelines → Library
  • See 3 variable groups:
  • ✅ dev-config
  • ✅ staging-config
  • ✅ prod-config
✅ Checkpoint: Three variable groups created with environment-specific values

Lab 2: Use Variable Groups in Pipeline 🔗

Objective: Reference variable groups in multi-stage pipeline
Time: 35 minutes

Exercise 2.1: Update Pipeline with Variable Groups

1 Navigate to Project
cd $env:USERPROFILE\Desktop\testing-demo\calculator notepad azure-pipelines-multistage.yml
2 Add Variable Groups to Stages

Update your DeployDev stage:

- stage: DeployDev displayName: 'Deploy to Development' dependsOn: Build variables: - group: dev-config # Load dev variables jobs: - deployment: DeployDev environment: 'Development' strategy: runOnce: deploy: steps: - task: DownloadPipelineArtifact@2 inputs: artifactName: 'calculator-app' # Display configuration - script: | echo "===== DEPLOYMENT CONFIGURATION =====" echo "Environment: $(environment)" echo "Database Server: $(dbServer)" echo "Database Name: $(dbName)" echo "API URL: $(apiUrl)" echo "Log Level: $(logLevel)" echo "====================================" displayName: 'Show Configuration'
3 Add to Staging Stage
- stage: DeployStaging displayName: 'Deploy to Staging' dependsOn: DeployDev variables: - group: staging-config # Load staging variables jobs: - deployment: DeployStaging environment: 'Staging' strategy: runOnce: deploy: steps: - script: | echo "Environment: $(environment)" echo "Database: $(dbServer)" displayName: 'Show Staging Config'
4 Add to Production Stage
- stage: DeployProduction variables: - group: prod-config # Load production variables jobs: - deployment: DeployProduction environment: 'Production' strategy: runOnce: deploy: steps: - script: | echo "PRODUCTION DEPLOYMENT" echo "Database: $(dbServer)" echo "Log Level: $(logLevel)" displayName: 'Production Config'

Save and close

5 Commit and Run
git add . git commit -m "Add variable groups to pipeline" git push
6 Watch Different Configs
  • Pipeline runs
  • Dev stage shows: dev-sql.database.windows.net
  • Staging shows: staging-sql.database.windows.net
  • Production shows: prod-sql.database.windows.net
  • Same code, different configs! ✅
✅ Checkpoint: Variable groups used successfully in pipeline

Lab 3: Working with Secret Variables 🔒

Objective: Store and use sensitive data securely
Time: 25 minutes

Exercise 3.1: Add Secret Variable

1 Open Variable Group
  • Go to Pipelines → Library
  • Click on prod-config variable group
2 Add Secret Variable
  • Click "+ Add" to add new variable
  • Name: dbPassword
  • Value: MySecureP@ssw0rd123
  • 🔒 Click the lock icon to mark as secret
  • Click "Save"
⚠️ After saving: Value shows as "***" - you can't view it again!
3 Use Secret in Pipeline
notepad azure-pipelines-multistage.yml

Update Production deployment:

- stage: DeployProduction variables: - group: prod-config jobs: - deployment: DeployProduction environment: 'Production' strategy: runOnce: deploy: steps: - script: | echo "Database Server: $(dbServer)" echo "Password: $(dbPassword)" displayName: 'Show Config with Secret'
4 Run and Verify Secret Masking
git add . git commit -m "Add secret variable usage" git push

In pipeline logs, you'll see:

Database Server: prod-sql.database.windows.net Password: ***
✅ Secret masked! Value is hidden in logs for security
✅ Checkpoint: Secrets stored and used securely

Lab 4: Environment-Specific Config Files 📄

Objective: Create and use different config files per environment
Time: 40 minutes

Exercise 4.1: Create Config Files (.NET Example)

1 Navigate to .NET Project
cd $env:USERPROFILE\Desktop\testing-demo\CalculatorApp
2 Create Base Configuration
notepad appsettings.json

Add base configuration:

{ "AppSettings": { "ApplicationName": "Calculator API", "Version": "1.0.0" }, "ConnectionStrings": { "DefaultConnection": "Server=localhost;Database=CalculatorDB" }, "Logging": { "LogLevel": { "Default": "Information" } } }

Save and close

3 Create Development Config
notepad appsettings.Development.json
{ "ConnectionStrings": { "DefaultConnection": "Server=dev-sql.database.windows.net;Database=CalculatorDB_Dev" }, "Logging": { "LogLevel": { "Default": "Debug" } }, "ApiSettings": { "BaseUrl": "https://dev-api.example.com" } }
4 Create Production Config
notepad appsettings.Production.json
{ "ConnectionStrings": { "DefaultConnection": "Server=prod-sql.database.windows.net;Database=CalculatorDB_Prod" }, "Logging": { "LogLevel": { "Default": "Error" } }, "ApiSettings": { "BaseUrl": "https://api.example.com" } }
5 Set Environment in Pipeline
notepad azure-pipelines.yml

Add ASPNETCORE_ENVIRONMENT variable:

- stage: DeployDev jobs: - deployment: DeployDev environment: 'Development' strategy: runOnce: deploy: steps: - script: | set ASPNETCORE_ENVIRONMENT=Development echo "Using appsettings.Development.json"
6 Commit and Test
git add . git commit -m "Add environment-specific config files" git push
✅ Checkpoint: Different config files for each environment

Lab 5: Replace Tokens in Config Files 🔄

Objective: Replace placeholders in config files with pipeline variables
Time: 35 minutes

Exercise 5.1: Create Config Template

1 Create Template File
notepad appsettings.template.json

Add template with tokens:

{ "AppSettings": { "ApplicationName": "Calculator API", "Environment": "__ENVIRONMENT__" }, "ConnectionStrings": { "DefaultConnection": "Server=__DB_SERVER__;Database=__DB_NAME__;User=__DB_USER__;Password=__DB_PASSWORD__" }, "ApiSettings": { "BaseUrl": "__API_URL__", "Timeout": 30 }, "Logging": { "LogLevel": { "Default": "__LOG_LEVEL__" } } }

Save and close

2 Update Production Variable Group
  • Go to Library → prod-config
  • Add these variables:
Name Value
DB_USER prod_admin
DB_PASSWORD (secret value) 🔒
3 Add Token Replacement Task
notepad azure-pipelines-multistage.yml

Add to deployment steps:

- task: replacetokens@5 displayName: 'Replace Config Tokens' inputs: rootDirectory: '$(Pipeline.Workspace)' targetFiles: '**/appsettings.template.json => appsettings.json' encoding: 'auto' tokenPattern: 'doubleUnderscore' writeBOM: true
💡 Install Extension:
Go to Marketplace and install "Replace Tokens" extension if needed
✅ Checkpoint: Token replacement working in deployment

🎯 Lab Summary

What You've Accomplished:

  • ✅ Created variable groups for each environment
  • ✅ Used variable groups in pipeline stages
  • ✅ Stored secret variables securely
  • ✅ Created environment-specific config files
  • ✅ Implemented token replacement
  • ✅ Verified different configs per environment

Configuration Management Mastered! ⚙️

🎉 Day 16 Complete!

You've mastered environment configuration!

Ready for Day 17: Deployment Strategies