简体   繁体   中英

Azure Function App deployment with PowerShell but not through REST?

I currently have a bash script that uses dotnet to compile a Function App and the Azure CLI to push deploy a .zip file. The script is essentially:

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

Before this, an az login is done using a Service Principal that has permissions to deploy to the Function App.

I'd like to translate this into PowerShell. I can do the dotnet compilation and the creation of the zip file, but I haven't been able to figure out the deployment yet. I've tried using:

$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"

...with username and password being the Service Principal ID and secret, but that gives a 401 - Unauthorized: Access is denied due to invalid credentials error.

Is there some way to use PowerShell to zipdeploy to a Function App using a Service Principal?

If you are using PowerShell you can directly obtain the publish credentials during deployment.

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

Obtaining creds for deployment

$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)))

Now proceed to deployment

$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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM