10

I would like to perform the following operations from an Azure DevOps pipeline:

  • Create new deployment slot for an existing WebApp (staging)
  • Deploy application to new slot
  • Swap staging slot with production
  • Delete former production, now staging slot

What I have so far is:

  • Deploy application to new slot
  • Swap staging slot with production
  • Delete former production, now staging slot

The YAML:

- task: AzureRmWebAppDeployment@4
  inputs:
    ConnectionType: 'AzureRM'
    azureSubscription: 'BizSpark(...)'
    appType: 'webApp'
    WebAppName: 'foo'
    deployToSlotOrASE: true
    ResourceGroupName: 'Default-WestEurope'
    SlotName: 'staging'
    packageForLinux: '$(Build.ArtifactStagingDirectory)/**/*.zip'

- task: AzureAppServiceManage@0
  inputs:
    azureSubscription: 'BizSpark(..)'
    Action: 'Swap Slots'
    WebAppName: 'foo'
    ResourceGroupName: 'Default-WestEurope'
    SourceSlot: 'staging'

- task: AzureAppServiceManage@0
  inputs:
    azureSubscription: 'BizSpark(..)'
    Action: 'Delete Slot'
    WebAppName: 'foo'
    ResourceGroupName: 'Default-WestEurope'
    Slot: 'staging'

However, AzureAppServiceManage task does not provide a method to create a deployment slot.

How can this be done?

3
  • 1
    Usually this is achieved with an ARM template deployment that is done before the app deployment. learn.microsoft.com/en-us/azure/azure-resource-manager/… Commented Mar 16, 2020 at 14:27
  • Keeping the app service slot would consume a lot of memory. I am going to apply the same thing to my project now. Could you let me know if it work fine until now? Commented Sep 23, 2021 at 13:45
  • 1
    @ThiệnSinh I am using this approach in about 10 pipelines in production and it works perfectly for me. Commented Sep 28, 2021 at 9:32

3 Answers 3

10

I could create a WebbApp slot in azure devops pipeline by using powershell and Microsft Hosted Agent, here is the task:

as per documentation example:

- task: AzureCLI@2
    displayName: Azure CLI   
     inputs:
      azureSubscription: <Name of the Azure Resource Manager service connection>
      scriptType: ps
      scriptLocation: inlineScript
      inlineScript: |
          az --version
          az account show

and for the inline script i used "az webapp deployment slot create" Azure CLI Command:

az webapp deployment slot create --name
                                 --resource-group
                                 --slot
                                 [--configuration-source]
                                 [--subscription]

does this help?

Sign up to request clarification or add additional context in comments.

Comments

5

Create deployment slot for WebApp in Azure DevOps pipeline

I am afraid there is no such out of box way to Create deployment slot for WebApp in Azure DevOps pipeline.

As the state of the task Azure App Service Management, we could to know:

The Azure App Service Management task is used to Start/Stop/Restart App Service, Swap Slot, Install Extentions, Enable Continuous Monitoring or Start/Stop all continuous WebJobs on an Azure App Service.

It does not support creating deployment slot for WebApp in the Azure devops pipeline. And AFAIK, no other task currently supports this feature in Azure devops pipeline.

As the solution for this question, just like juunas comment, Usually this is achieved with an ARM template deployment.

We could using the following ARM template to provision Deployment Slots for Azure App Service:

{
    "$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "siteName": {
            "type": "string"
        },
        "slotName": {
            "type": "string"
        }
    },
    "resources": [
        {
            "apiVersion": "2015-04-01",
            "type": "Microsoft.Web/Sites/Slots",
            "name": "[concat(parameters('siteName'), '/', parameters('slotName'))]",
            "location": "[resourceGroup().location]",
            "properties": {},
            "resources": []
        }
    ]
}

Then, we could deploy ARM templates using Azure devops.

You could check this blog and this blog for some more details.

Hope this helps.

2 Comments

Thanks for your reply! I've opted for the other answer as it seems to be a little more straight forward to me. Thank you very much anyways!
I believe this should be the preferred answer. Resources should be deployed in the arm template and not in the pipeline itself. The slot is part of the resource.
2

expanding on @Mario Dietner's answer: https://stackoverflow.com/a/60772743/343347 (comments have max length)

here ya go if you living in a powershell world but love that cli.. bonus adds a User Assigned Managed Identity to the slot (this does NOT copy over with settings from configuration-source)

##REQUIRED VARS
$rg = "myRG"
$app = "myApp"
$slotName = "staging"
$uamiName = "myUserAssignedMI"
##REQUIRED VARS


##SLOT CREATE
$slotConfig = az webapp deployment slot list --resource-group $rg --name $app --query "[?name=='$slotName']" | ConvertFrom-JSON

if($null -eq $slotConfig){
    Write-Host "Slot '$slotName' does not exist for rg/app '$rg/$app'."

    az webapp deployment slot create --name $app --resource-group $rg --slot $slotName --configuration-source $app

    Write-Host "Slot '$slotName' created."
}else{
    Write-Host "Slot '$($slotConfig.name)' already exists in app '$($slotConfig.repositorySiteName)'."
}

##MANAGED IDENTITY CREATE (singular... but identity assign supports a space delimited list)
$identityId = az identity list --query "[?name=='$uamiName'].id" -o tsv
$slotIdentity = az webapp identity show --resource-group $rg --name $app --slot $slotName | ConvertFrom-JSON

if($slotIdentity.userAssignedIdentities.psobject.properties.name -eq $identityId){
    Write-Host "Identity '$uamiName' exists for rg/app/slot '$rg/$app/$slotName'."
}else{
    Write-Host "Identity '$uamiName' does not exist for rg/app/slot '$rg/$app/$slotName'."
    
    az webapp identity assign -g $rg -n $app -s $slotName --identities $identityId

    Write-Host "Identity '$uamiName' added to rg/app/slot '$rg/$app/$slotName'."
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.