Bytezar
Bytezar

Reputation: 53

How do I deploy to Azure App Service with PowerShell (az module)?

I want to deploy app services from Powershell. I would like to use only publish profile (no password for azure account).

I tried FTP service, but sometimes files are blocked by running users. I think I have to stop app service.

There is powershell command like:

Publish-AzWebApp

However first I need login with:

Connect-AzAccount

and pass credentials what I want to avoid.

There is any way to call Publish-AzWebApp based on only publish profile (no login by account)?

Connect-AzAccount has other options to login (token or certificate). Unfortunately I don't know how to generate it.

BTW There was a topic about it: How do I deploy to Azure App Service with PowerShell? But it is old and now module "az" is recommended.

Upvotes: 3

Views: 4657

Answers (3)

RBT
RBT

Reputation: 25945

Here is the step by step process of deploying and publishing an Azure app service with PowerShell (PS) using newly recommended Az module. You'll have to install Azure CLI before running the scripts:

$subscription = 'Visual Studio Enterprise – MPN' #Change it as per your Azure subscription
$location = 'Central US' #Change it according to the location where you want to host your web app
$resourceGroup = 'MyWebAppResourceGroup'

$ErrorActionPreference = "Stop" #this ensures that script stops executing at first error in the script.


Write-host "Installing nuget package provider"
Install-PackageProvider -Name NuGet -Scope CurrentUser -MinimumVersion 2.8.5.201 -Force
Write-Host "Setting Power Shell gallery"
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted

Write-Host "Installing missing PowerShell modules required for running this script.."
if ((Get-InstalledModule -Name "Az.Accounts" -ErrorAction SilentlyContinue) -eq $null) {
    Write-Host "Az.Accounts module missing. Now installing..."
    Install-Module -Name Az.Accounts -Scope CurrentUser
}


if ((Get-InstalledModule -Name "Az.Websites" -ErrorAction SilentlyContinue) -eq $null) {
    Write-Host "Az.Websites module missing. Now installing .."
    Install-Module -Name Az.Websites -Scope CurrentUser
}
Write-Host "Installing PowerShell modules completed."

Write-Host "Staring to import PowerShell modules in current session...."
Import-Module Az.websites
Write-Host "Importing PowerShell modules completed."


#This is required due to an issue due to which PowerShell fails to connect with online resources. This issue is machine specific. So you can comment it if not required.
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Write-Host "Connecting to Azure account..."
Connect-AzAccount 

Write-Host "Setting subscription for current session..."
az account set --subscription $subscription

Write-Host "Creating resource group..."
az group create --name $resourceGroup --location $location

$webAppName="WelcomeCloudApp"  
$appServicePlan="WelcomeCloudAppServicePlan"  

Write-Host "Creating WebApp Service plan..."
New-AzAppServicePlan  -Name $appServicePlan -ResourceGroupName $resourceGroup  -Location $location -Tier 'Free' #-Debug  

Write-Host "Creating WebApp..."
New-AzWebApp -Name $webAppName -Location $location -AppServicePlan $appServicePlan -ResourceGroupName $resourceGroup 

Write-Host "Publishing WebApp..."
Publish-AzWebApp -ResourceGroupName $resourceGroup -Name $webAppName -ArchivePath WelcomeCloudAppService.zip  

Write-Host "Finished installing your web app. Bye!"

Upvotes: 0

Joy Wang
Joy Wang

Reputation: 42133

There is any way to call Publish-AzWebApp based on only publish profile (no login by account)?

No, you can't. If you want to use the Publish-AzWebApp , you always need to login with Connect-AzAccount, whatever the parameters you use, examples here.

If you want to use powershell to deploy the web app based on only publish profile, the workaround is to use Kudu API via powershell.

$username = "`$webappname"
$password = "xxxxxxx"
# Note that the $username here should look like `SomeUserName`, and **not** `SomeSite\SomeUserName`
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username, $password)))
$userAgent = "powershell/1.0"

$apiUrl = "https://joywebapp.scm.azurewebsites.net/api/zipdeploy"
$filePath = "C:\Users\joyw\Desktop\testdep.zip"
Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method POST -InFile $filePath -ContentType "multipart/form-data"

Upvotes: 3

Related Questions