我目前有一个bash脚本,该脚本使用dotnet编译Function App,并使用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通过服务主体将zip部署到功能应用程序?
答案 0 :(得分:2)
如果使用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"