使用 Azure CLI,從 Azure DevOps 存放庫建立可持續部署的 App Service 應用程式

此範例指令碼會在 App Service 中建立應用程式及其相關資源,然後設定從 Azure DevOps 存放庫進行的持續部署。 針對此範例,您需要:

  • 一個您具備系統管理權限且含有應用程式程式碼的 Azure DevOps 存放庫。
  • 您 Azure DevOps 組織的個人存取權杖 (PAT)

如果您沒有 Azure 訂閱,請在開始之前,先建立 Azure 免費帳戶

必要條件

範例指令碼

啟動 Azure Cloud Shell

Azure Cloud Shell 是免費的互動式 Shell,可讓您用來執行本文中的步驟。 它具有預先安裝和設定的共用 Azure 工具,可與您的帳戶搭配使用。

若要開啟 Cloud Shell,只要選取程式碼區塊右上角的 [試試看] 即可。 您也可以移至 https://shell.azure.com ,從另一個瀏覽器索引標籤啟動 Cloud Shell。

當開啟 Cloud Shell 時,請確認已為您的環境選取 Bash。 後續的工作階段將會在 Bash 環境中使用 Azure CLI,請選取 [複製] 以複製程式碼區塊,並將其貼到 Cloud Shell 中,然後按 Enter 鍵加以執行。

登入 Azure

系統會在登入的初始帳戶下自動驗證 Cloud Shell。 使用下列指令碼透過不同的訂閱登入,並將 <Subscription ID> 取代為您的 Azure 訂用帳戶識別碼。 如果您沒有 Azure 訂用帳戶,請在開始之前先建立 Azure 免費帳戶

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="East US"
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.azurewebsites.net"
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

範例參考

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

Command 注意
az group create 建立用來存放所有資源的資源群組。
az appservice plan create 建立 App Service 方案。
az webapp create 建立 App Service 應用程式。
az webapp deployment source config 將 App Service 應用程式與 Git 或 Mercurial 存放庫建立關聯。

下一步

如需 Azure CLI 的詳細資訊,請參閱 Azure CLI 文件

您可以在 Azure App Service 文件中找到其他的 App Service CLI 指令碼範例。