【问题标题】:Create deployment slot for WebApp in Azure DevOps pipeline在 Azure DevOps 管道中为 WebApp 创建部署槽
【发布时间】:2020-06-27 16:03:20
【问题描述】:

我想从 Azure DevOps 管道执行以下操作:

  • 为现有的 WebApp (staging) 创建新的部署槽
  • 将应用程序部署到新插槽
  • staging 插槽与production 交换
  • 删除以前的production,现在删除staging

到目前为止我所拥有的是:

  • 将应用程序部署到新插槽
  • staging 插槽与生产交换
  • 删除以前的production,现在删除staging

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'

但是,AzureAppServiceManage 任务不提供创建部署槽的方法。

如何做到这一点?

【问题讨论】:

  • 通常这是通过在应用部署之前完成的 ARM 模板部署来实现的。 docs.microsoft.com/en-us/azure/azure-resource-manager/templates/…
  • 保留应用服务槽会消耗大量内存。我现在将把同样的东西应用到我的项目中。你能告诉我它到现在是否还能正常工作吗?
  • @ThiệnSinh 我在大约 10 个生产管道中使用这种方法,它对我来说非常有效。

标签: azure azure-devops yaml azure-pipelines


【解决方案1】:

我可以使用 powershell 和 Microsft 托管代理在 azure devops 管道中创建一个 WebbApp 插槽,这是任务:

根据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

对于内联脚本,我使用了“az webapp deployment slot create”Azure CLI Command:

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

这有帮助吗?

【讨论】:

    【解决方案2】:

    在 Azure DevOps 管道中为 WebApp 创建部署槽

    恐怕没有这种开箱即用的方式来为 Azure DevOps 管道中的 WebApp 创建部署槽。

    作为任务Azure App Service Managementstate,我们可以知道:

    Azure App Service Management 任务用于启动/停止/重启 应用服务、交换槽、安装扩展、启用连续 在 Azure 应用程序上监视或启动/停止所有连续的 WebJobs 服务。

    它不支持在 Azure devops 管道中为 WebApp 创建部署槽。而且 AFAIK,目前没有其他任务在 Azure devops 管道中支持此功能。

    作为这个问题的解决方案,就像 juunas 评论一样,通常这是通过 ARM 模板部署来实现的

    我们可以使用以下 ARM 模板为 Azure 应用服务提供部署槽:

    {
        "$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": []
            }
        ]
    }
    

    然后,我们可以使用 Azure devops 部署 ARM 模板。

    您可以查看this blogthis blog 了解更多详情。

    希望这会有所帮助。

    【讨论】:

    • 感谢您的回复!我选择了另一个答案,因为它对我来说似乎更直接一些。无论如何都非常感谢!
    • 我相信这应该是首选答案。资源应该部署在 arm 模板中,而不是管道本身。插槽是资源的一部分。
    【解决方案3】:

    扩展 @Mario Dietner 的答案:https://stackoverflow.com/a/60772743/343347(cmets 有最大长度)

    如果你生活在 powershell 世界但喜欢那个 cli.. 奖金,那就去吧/p>

    ##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'."
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 1970-01-01
      • 2020-10-22
      • 2019-10-20
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 2022-12-05
      • 2020-01-19
      相关资源
      最近更新 更多