Please change the values of the variables accordingly.
The entire YAML pipeline is build using Runtime Parameters and Variables. No Values are Hardcoded.
PART #3:-
This is a 2 Stage Pipeline.
Stage #1 of the Pipeline "Validate_ResourceGroup_AppServicePlans_WebApp" has 6 Pipeline Tasks.
Stage #2 of the Pipeline "Migrate_WebApp_Between_AppServicePlans" has 2 Pipeline Tasks.
STAGE #1 - PIPELINE TASK #1:-
Set Azure Account.
- stage: Validate_ResourceGroup_AppServicePlans_WebApp
jobs:
- job: Validate_ResourceGroup_AppServicePlans_WebApp
displayName: Validate Resource Group App Service Plans and WebApp
steps:
- task: AzureCLI@2
displayName: Set Azure Account
inputs:
azureSubscription: $(ServiceConnection)
scriptType: ps
scriptLocation: inlineScript
inlineScript: |
az --version
az account set --subscription ${{ parameters.SubscriptionID }}
az account show
STAGE #1 - PIPELINE TASK #2:-
Validate if Resource Group exists. If not, Pipeline will fail.
- task: AzureCLI@2
displayName: Validate Resource Group
inputs:
azureSubscription: $(ServiceConnection)
scriptType: ps
scriptLocation: inlineScript
inlineScript: |
$i = az group exists -n ${{ parameters.RGName }}
if ($i -eq "true") {
echo "#####################################################"
echo "Resource Group ${{ parameters.RGName }} exists!!!"
echo "#####################################################"
}
else {
echo "#############################################################"
echo "Resource Group ${{ parameters.RGName }} DOES NOT exists!!!"
echo "#############################################################"
exit 1
}
STAGE #1 - PIPELINE TASK #3:-
Validate if Source App Service Plan exists. If not, Pipeline will fail.
- task: AzureCLI@2
displayName: Validate Source App Service Plan
inputs:
azureSubscription: $(ServiceConnection)
scriptType: ps
scriptLocation: inlineScript
inlineScript: |
$j = az appservice plan list --query "[?name=='${{ parameters.AppServiceSourcePlanName }}'].name" -o tsv
if ($j -eq "${{ parameters.AppServiceSourcePlanName }}") {
echo "##############################################################################"
echo "Source App Service Plan ${{ parameters.AppServiceSourcePlanName }} exists!!!"
echo "##############################################################################"
}
else {
echo "#######################################################################################"
echo "Source App Service Plan ${{ parameters.AppServiceSourcePlanName }} DOES NOT exists!!!"
echo "########################################################################################"
exit 1
}
STAGE #1 - PIPELINE TASK #4:-
Validate if Destination App Service Plan exists. If not, Pipeline will fail.
- task: AzureCLI@2
displayName: Validate Destination App Service Plan
inputs:
azureSubscription: $(ServiceConnection)
scriptType: ps
scriptLocation: inlineScript
inlineScript: |
$k = az appservice plan list --query "[?name=='${{ parameters.AppServiceDestinationPlanName }}'].name" -o tsv
if ($k -eq "${{ parameters.AppServiceDestinationPlanName }}") {
echo "#########################################################################################"
echo "Destination App Service Plan ${{ parameters.AppServiceDestinationPlanName }} exists!!!"
echo "#########################################################################################"
}
else {
echo "#################################################################################################"
echo "Destination App Service Plan ${{ parameters.AppServiceDestinationPlanName }} DOES NOT exists!!!"
echo "##################################################################################################"
exit 1
}
STAGE #1 - PIPELINE TASK #5:-
Validate if Web App exists. If not, Pipeline will fail.
- task: AzureCLI@2
displayName: Validate Webspace
inputs:
azureSubscription: $(ServiceConnection)
scriptType: ps
scriptLocation: inlineScript
inlineScript: |
$srcplanwebspace = az appservice plan show --name ${{ parameters.AppServiceSourcePlanName }} --resource-group ${{ parameters.RGName }} --query ["properties.webSpace"] -o tsv
$destplanwebspace = az appservice plan show --name ${{ parameters.AppServiceDestinationPlanName }} --resource-group ${{ parameters.RGName }} --query ["properties.webSpace"] -o tsv
if ($srcplanwebspace -eq $destplanwebspace) {
echo "################################################"
echo "Webspace of both App Service Plans matches!!!"
echo "################################################"
}
else {
echo "##############################################################################################################################"
echo "Webspace of both App Service Plans DOES NOT match and hence App Service ${{ parameters.WebAppName }} cannot be Migrated !!!"
echo "##############################################################################################################################"
exit 1
}
IMPORTANT TO NOTE: WHY WE NEED TO VALIDATE WEBSPACE ?
Validating webspace ensures whether we can migrate the Web App between the mentioned App Service Plans (Source and Destination).
Azure deploys each new App Service plan into a deployment unit, internally called a webspace. Each region can have many webspaces, but your app can only move between plans that are created in the same webspace.
It is not possible to specify the webspace when we are creating a plan, but itβs possible to ensure that a plan is created in the same webspace as an existing plan. In brief, all plans created with the same resource group, region and operating system are deployed into the same webspace.
Webspace Information of Source App Service Plan.
Webspace Information of Destination App Service Plan.
STAGE #2 - PIPELINE TASK #1:-
Stage #2 will only execute if both the below conditions are met.
Condition 1: If the Previous Stage executed successfully.
Condition 2: If the Source Branch from where the Pipeline is executed is the "Main" Branch.
Once both the conditions are, Stage #2 will start executing but will wait for Pipeline Approval first.
Post Pipeline Approval, Stage #2, Pipeline Task # will execute which is "Settings Azure Account"
- stage: Migrate_WebApp_Between_AppServicePlans
condition: |
and(succeeded(),
eq(variables['build.sourceBranch'], 'refs/heads/main')
)
jobs:
- deployment:
displayName: Migrate WebApp Between App Service Plans
environment: '$(envName)'
pool:
vmImage: $(BuildAgent)
strategy:
runOnce:
deploy:
steps:
- task: AzureCLI@2
displayName: Set Azure Account
inputs:
azureSubscription: $(ServiceConnection)
scriptType: ps
scriptLocation: inlineScript
inlineScript: |
az --version
az account set --subscription ${{ parameters.SubscriptionID }}
az account show
STAGE #2 - PIPELINE TASK #2:-
Migrate Web App from Source to Destination App Service Plan.
The "serverFarmId" parameter in "az webapp update" command was found in the "Web App Resource JSON".
- task: AzureCLI@2
displayName: Migrate WebApp
inputs:
azureSubscription: $(ServiceConnection)
scriptType: ps
scriptLocation: inlineScript
inlineScript: |
az webapp update -g ${{ parameters.RGName }} -n ${{ parameters.WebAppName }} --set serverFarmId=/subscriptions/${{ parameters.SubscriptionID }}/resourceGroups/${{ parameters.RGName }}/providers/Microsoft.Web/serverfarms/${{ parameters.AppServiceDestinationPlanName }}
echo "###########################################################################################################################################################################################################"
echo "WebApp ${{ parameters.WebAppName }} migrated from Source App Service Plan ${{ parameters.AppServiceSourcePlanName }} to Destination App Service Plan ${{ parameters.AppServiceDestinationPlanName }}!!!"
echo "###########################################################################################################################################################################################################"
NOW ITS TIME TO TEST!!!
TEST CASES:-
1. Pipeline executed successfully.
2. Web App is successfully migrated to Destination App Service Plan.
3. Source App Service Plan is now empty.
Hope You Enjoyed the Session!!!
Stay Safe | Keep Learning | Spread Knowledge
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)