使用 Azure CLI 通过 Azure DevOps 存储库的持续部署创建应用服务应用

此示例脚本使用其相关资源,在应用服务中创建应用,然后从 Azure DevOps 存储库设置持续部署。 在此示例中,需要以下项:

  • 包含应用程序代码且你对其具有管理权限的 Azure DevOps 存储库。
  • 用于 Azure DevOps 组织的个人访问令牌 (PAT)

如果没有 Azure 订阅,可在开始前创建一个试用帐户

先决条件

可以使用本地 Azure CLI。

  • 如果需要,请安装 Azure CLI 来运行 CLI 参考命令。

  • 本地 Azure CLI,请了解如何安装 Azure CLI。 如果在 Windows 或 macOS 上运行,请考虑在 Docker 容器中运行 Azure CLI。 有关详细信息,请参阅如何在 Docker 容器中运行 Azure CLI

    • 通过使用 az login 命令登录到 Azure CLI。 若要完成身份验证过程,请遵循终端中显示的步骤。 有关其他登录选项,请参阅使用 Azure CLI 登录

    • 出现提示时,请在首次使用时安装 Azure CLI 扩展。 有关扩展详细信息,请参阅使用 Azure CLI 的扩展

    • 运行 az version 以查找安装的版本和依赖库。 若要升级到最新版本,请运行 az upgrade

示例脚本

登录 Azure

使用以下脚本通过其他订阅登录,将 <Subscription ID> 替换为 Azure 订阅 ID。 如果没有 Azure 试用版订阅,请在开始前创建一个试用版订阅

az cloud set -n AzureChinaCloud
az login

subscription="<subscriptionId>" # add subscription here

az account set -s $subscription # ...or use 'az login'

有关详细信息,请参阅设置有效的订阅登录

创建 Web 应用

# Create an App Service app with continuous deployment from an Azure DevOps repository
# set -e # exit if error
# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="China East"
resourceGroup="msdocs-app-service-rg-$randomIdentifier"
tag="deploy-vsts-continuous-webapp-only.sh"
appServicePlan="msdocs-app-service-plan-$randomIdentifier"
webapp="msdocs-web-app-$randomIdentifier"

# Create a resource group.
echo "Creating $resourceGroup in "$location"..."
az group create --name $resourceGroup --location "$location" --tag $tag

# Create an App Service plan in `FREE` tier
echo "Creating $appServicePlan"
az appservice plan create --name $appServicePlan --resource-group $resourceGroup --sku FREE

# Create a web app.
echo "Creating $webapp"
az webapp create --name $webapp --resource-group $resourceGroup --plan $appServicePlan

# Copy the result of the following command into a browser to see the static HTML site.
site="http://$webapp.chinacloudsites.cn"
echo $site
curl "$site"

从 Azure DevOps 配置持续部署

创建包含以下包含 Azure DevOps 信息的变量。

gitrepo=<Replace with your Azure DevOps Services (formerly Visual Studio Team Services, or VSTS) repo URL>
token=<Replace with an Azure DevOps Services (formerly Visual Studio Team Services, or VSTS) personal access token>

从 Azure DevOps Services(以前称为 Visual Studio Team Services 或 VSTS)配置持续部署。 每个 Azure 帐户只需要一次 --git-token 参数(Azure 记得令牌)。

az webapp deployment source config --name $webapp --resource-group $resourceGroup \
--repo-url $gitrepo --branch master --git-token $token

清理资源

使用 az group delete 命令删除资源组以及与其关联的所有资源 - 除非你持续需要这些资源。 其中一些资源在创建和删除时可能要稍等片刻。

az group delete --name $resourceGroup

示例参考

此脚本使用以下命令。 表中的每条命令均链接到特定于命令的文档。

命令 说明
az group create 创建用于存储所有资源的资源组。
az appservice plan create 创建应用服务计划。
az webapp create 创建应用服务应用。
az webapp deployment source config 将应用服务应用与 Git 或 Mercurial 存储库相关联。

后续步骤

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档

可以在 Azure 应用服务文档中找到其他应用服务 CLI 脚本示例。