建立可從 GitHub 連續部署的 Web 應用程式

此範例指令碼會在 App Service 中建立 Web 應用程式及其相關資源,然後設定從 GitHub 存放庫進行的持續部署。 如需進行沒有連續部署的 GitHub 部署,請參閱建立 Web 應用程式並從 GitHub 部署程式碼

您可以視需要使用 Azure PowerShell 指南 \(英文\) 中的指示來安裝 Azure PowerShell,然後執行 Connect-AzAccount 來建立與 Azure 的連線。 此外,請確定:

範例指令碼

注意

建議您使用 Azure Az PowerShell 模組來與 Azure 互動。 請參閱安裝 Azure PowerShell 以開始使用。 若要了解如何移轉至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 移轉至 Az

$gitrepo="<replace-with-URL-of-your-own-GitHub-repo>"
$gittoken="<replace-with-a-GitHub-access-token>"
$webappname="mywebapp$(Get-Random)"
$location="West Europe"

# Create a resource group.
New-AzResourceGroup -Name myResourceGroup -Location $location

# Create an App Service plan in Free tier.
New-AzAppServicePlan -Name $webappname -Location $location `
-ResourceGroupName myResourceGroup -Tier Free

# Create a web app.
New-AzWebApp -Name $webappname -Location $location -AppServicePlan $webappname `
-ResourceGroupName myResourceGroup

# SET GitHub
$PropertiesObject = @{
    token = $gittoken;
}
Set-AzResource -PropertyObject $PropertiesObject `
-ResourceId /providers/Microsoft.Web/sourcecontrols/GitHub -ApiVersion 2015-08-01 -Force

# Configure GitHub deployment from your GitHub repo and deploy once.
$PropertiesObject = @{
    repoUrl = "$gitrepo";
    branch = "master";
}
Set-AzResource -PropertyObject $PropertiesObject -ResourceGroupName myResourceGroup `
-ResourceType Microsoft.Web/sites/sourcecontrols -ResourceName $webappname/web `
-ApiVersion 2015-08-01 -Force

清除部署

在執行過指令碼範例之後,您可以使用下列命令來移除資源群組、Web 應用程式和所有相關資源。

Remove-AzResourceGroup -Name myResourceGroup -Force

指令碼說明

此指令碼會使用下列命令。 下表中的每個命令都會連結至命令特定的文件。

Command 注意
New-AzResourceGroup 建立用來存放所有資源的資源群組。
New-AzAppServicePlan 建立 App Service 方案。
New-AzWebApp 建立 Web 應用程式。
Set-AzResource 修改資源群組中的資源。

下一步

如需有關 Azure PowerShell 模組的詳細資訊,請參閱 Azure PowerShell 文件

您可以在 Azure PowerShell 範例中找到適用於 App Service Web Apps 的其他 Azure PowerShell 範例。