0

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?

0

1 Answer 1

2

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"
Sign up to request clarification or add additional context in comments.

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.