代码之家  ›  专栏  ›  技术社区  ›  CanSpice

使用powershell但不通过rest部署azure功能应用程序?

  •  0
  • CanSpice  · 技术社区  · 6 年前

    我目前有一个bash脚本,它使用dotnet来编译一个函数应用程序,使用azure cli来推动部署 .zip 文件。脚本基本上是:

    dotnet clean --configuration Release
    dotnet build --configuration Release
    cd bin/Release/netstandard2.0
    zip -r ${functionappName}.zip *
    az functionapp deployment source config-zip -g group -n functionapp --src ${functionappName}.zip
    

    在此之前, az login 使用具有部署到函数应用程序的权限的服务主体完成。

    我想把它转换成powershell。我能做到 dotnet 编译和创建zip文件,但我还没有弄清楚部署。我试过使用:

    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username, $password )))
    Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -UserAgent $userAgent -Method POST -InFile $filePath -ContentType "multipart/form-data"
    

    …… username password 作为服务主体ID和机密,但是 401 - Unauthorized: Access is denied due to invalid credentials 错误。

    有没有什么方法可以使用powershell使用服务主体对功能应用程序进行zipdeploy?

    1 回复  |  直到 6 年前
        1
  •  2
  •   HariHaran    6 年前

    如果您使用的是powershell,则可以在部署期间直接获取发布凭据。

    Login-AzureRmAccount
    Get-AzureRmSubscription | Select SubscriptionName, SubscriptionId
    Select-AzureRmSubscription -SubscriptionName "My Subscription"
    $resourceGroup = "MyResourceGroup"
    $functionAppName = "MyFunctionApp"
    

    获取用于部署的凭据

    $creds = Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroup -ResourceType Microsoft.Web/sites/config ` -ResourceName $functionAppName/publishingcredentials -Action list -ApiVersion 2015-08-01 -Force
    $username = $creds.Properties.PublishingUserName
    $password = $creds.Properties.PublishingPassword
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
    

    现在开始部署

    $apiUrl = "https://$functionAppName.scm.azurewebsites.net/api/zip/site/wwwroot"
    Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method PUT -InFile $zipFilePath -ContentType "multipart/form-data"