如何使用PowerShell部署到Azure App Service?

6
我已经查看了Azure和AzureRM命令集中的数千个命令,但仍然不确定如何操作。以下是我已经完成的部分:
  • 安装了Azure和AzureRM模块并将其导入到脚本中
  • 使用get-AzurePublishSettingsFile命令生成了“*.publishsettings”文件
  • 导入了“*.publishsettings”文件
  • 可以使用“Stop-AzureWebsite”和“Start-AzureWebsite”命令访问网站
我需要做的是创建一个新的部署并将文件推送到应用服务站点。
注:我没有Visual Studio项目和.csproj文件配置。我只想将文件夹的内容推送到网站上。
由于文档细节很差,而PowerShell中有数千个命令需要查阅,因此任何帮助都将非常有用。

你是否想将你的网站部署为Web Deploy包到Azure App Service网站? - juvchan
@juvchan: 基本上只是一堆文件(主要是*.js、*.css和*.html),而不是作为一个包。如果需要的话,我可以将它们压缩成zip文件,但它们并不是VS项目或其他项目的一部分。 - IronWilliamCash
1
如果您知道如何使用msdeploy命令将网站文件打包成Web Deploy包,那么这些步骤将非常简单。如果您认为这个解决方案可行,我可以从那里为您提供指导。 - juvchan
@juvchan:使用Walter的答案成功解决了问题,但还是感谢你的帮助! - IronWilliamCash
4个回答

8

您可以查看这个博客:使用Azure PowerShell将应用程序服务部署到部署槽中

Get-AzurePublishSettingsFile
Import-AzurePublishSettingsFile .\Your-Publish-Settings-credentials.publishsettings
Get-AzureSubscription
Select-AzureSubscription -SubscriptionName "The Subscription Name containing the slot"
Set-AzureSubscription -SubscriptionId "ID of subscription"

$WebAppName = "standard(staging)"
Get-AzureWebsite -Name $WebAppName
Publish-AzureWebsiteProject -Name $WebAppName -Package "C:\PowerShell\standard.zip" -Slot "staging"

那我可以使用“Publish-AzureWebsiteProject”并将一个zip文件作为Package传递吗?所有我看到的文档都提到了“*.csporj”包。 - IronWilliamCash
刚回来确认这个方法可以用于zip文件。问题终于解决了。感谢您的帮助。 - IronWilliamCash

3

在Stackoverflow上说“上面的链接”没有意义。每次刷新页面,答案都会重新排列。 - Andrew Shepherd
还在学习中。我已经包含了我所说的链接。谢谢你的纠正。 - Jay
@Jay 这本来是我的备选方案。感谢你的信息,但我已经通过Walter的答案解决了问题。 - IronWilliamCash

2
不幸的是,接受的答案给了我以下错误:

Get-AzureWebSite: 找不到请求的值“PremiumV2”

此StackOverflow答案建议使用Get-AzureRmWebApp 代替,但这会在身份验证方面引入一些挑战。 经过一些搜索,我发现以下文章正好解释了我所需要的:一种无需任何人工操作即可发布到Azure的方法。
请查看以下脚本的非常简化版本。
#In the Azure portal go to (search for) "Azure Active Directory" -> 
#"Properties" -> Directory ID
$TenantId = "<Azure Active Directory Id>"

#In the Azure portal go to (search for) "Subscriptions" -> Subscription ID
$SubscriptionId = "<Azure Subscription Id>"

#In the Azure portal go to (search for) "Azure Active Directory" -> "App registrations" -> 
#Create a new registration, this will give you the ID and Secret below.
#Make sure to give your new app registration sufficient rights to your app service 
$ServicePrincipleApplicationId = "<Service Principle Id>"
$ServicePrincipleApplicationSecret = "<Service Principle Secret>"

$WebAppPath = "<Local folder where your package is located>"
$ResourceGroupName = "<The name of the Azure resource group that contains your app service>"
$WebAppName = "<The name of your Azure app service>"
$WebAppSlot = "<The name of the deployment slot you want to publish to>"

$MSDeployPath = "C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe"
$source = "-source:contentPath=$WebAppPath"
$publishProfileOutputPath = Join-Path -Path $ENV:Temp -ChildPath 'publishprofile.xml'
$dest = "-dest:contentPath=d:\home\site\wwwroot\,publishSettings=$publishProfileOutputPath"
$SecurePassword = $ServicePrincipleApplicationSecret | ConvertTo-SecureString -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $ServicePrincipleApplicationId, $securePassword
$connectParameters = @{
    Credential     = $Credential
    TenantId       = $TenantId
    SubscriptionId = $SubscriptionId
}

Add-AzureRmAccount @connectParameters -ServicePrincipal

Get-AzureRmWebAppSlotPublishingProfile -OutputFile $publishProfileOutputPath -Format WebDeploy -ResourceGroupName $ResourceGroupName -Name $WebAppName -Slot $WebAppSlot

Stop-AzureRmWebAppSlot -ResourceGroupName $ResourceGroupName -Name $WebAppName -Slot $WebAppSlot

& $MSDeployPath @('-verb:sync', $source, $dest)

Start-AzureRmWebAppSlot -ResourceGroupName $ResourceGroupName -Name $WebAppName -Slot $WebAppSlot

0

使用 PowerShell 命令将您的 zip 包部署到 Azure Web 应用服务。

参考 MS Docs。

通过 PowerShell 连接到 Azure 订阅。执行 Publish-AzWebApp 来部署 Web 应用程序。

$webAppName = "<NameOfWebAppService>"
$resourceGroup = "<WebAppResourceGroupName>"
$zipArchiveFullPath = "<zip-package-filePath\FileName.zip>"
Publish-AzWebApp -ResourceGroupName "$resourceGroup" -Name "$webAppName" -ArchivePath "$($zipArchiveFullPath)" -Force

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接