从本地 Git 存储库创建 Web 应用并部署代码Create a web app and deploy code from a local Git repository

此示例脚本使用其相关资源,在应用服务中创建 Web 应用,然后从本地 Git 存储库部署 Web 应用代码。This sample script creates a web app in App Service with its related resources, and then deploys your web app code from a local Git repository.

必要时,请遵照 Azure PowerShell 指南中的说明更新到最新版本的 Azure PowerShell,并运行 Connect-AzAccount -Environment AzureChinaCloud 来与 Azure 建立连接。If needed, update to the latest Azure PowerShell using the instruction found in the Azure PowerShell guide, and then run Connect-AzAccount -Environment AzureChinaCloud to create a connection with Azure. 此外,需将应用程序代码提交到本地 Git 存储库。Also, your application code needs to be committed into a local Git repository.

示例脚本Sample script

备注

本文已经过更新,以便使用 Azure Az PowerShell 模块。This article has been updated to use the Azure Az PowerShell module. 若要与 Azure 交互,建议使用的 PowerShell 模块是 Az PowerShell 模块。The Az PowerShell module is the recommended PowerShell module for interacting with Azure. 若要开始使用 Az PowerShell 模块,请参阅安装 Azure PowerShellTo get started with the Az PowerShell module, see Install Azure PowerShell. 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 AzTo learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.

$gitdirectory="<Replace with path to local Git repo>"
$webappname="mywebapp$(Get-Random)"

cd $gitdirectory

# Create a web app and set up Git deployement.
New-AzWebApp -Name $webappname

# Push your code to the new Azure remote
git push azure master

清理部署Clean up deployment

运行脚本示例后,可以使用以下命令删除资源组、Web 应用以及所有相关资源。After the script sample has been run, the following command can be used to remove the resource group, web app, and all related resources.

Remove-AzResourceGroup -Name $webappname -Force

脚本说明Script explanation

此脚本使用以下命令。This script uses the following commands. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

CommandCommand 说明Notes
New-AzWebAppNew-AzWebApp 使用所需的资源组和应用服务组创建 Web 应用。Creates a web app with necessary resource group and App Service group. 如果当前目录包含 Git 存储库,则还要添加 azure 远程控制。When the current directory contains a Git repository, also add an azure remote.

后续步骤Next steps

有关 Azure PowerShell 模块的详细信息,请参阅 Azure PowerShell 文档For more information on the Azure PowerShell module, see Azure PowerShell documentation.

可以在 Azure PowerShell 示例中找到 Azure 应用服务 Web 应用的其他 Azure Powershell 示例。Additional Azure Powershell samples for Azure App Service Web Apps can be found in the Azure PowerShell samples.