教程:使用 Azure Pipelines 持续集成 Azure 资源管理器模板Tutorial: Continuous integration of Azure Resource Manager templates with Azure Pipelines

上一篇教程中,你部署了一个链接模板。In the previous tutorial, you deploy a linked template. 本教程介绍如何使用 Azure Pipelines 持续生成和部署 Azure 资源管理器模板项目。In this tutorial, you learn how to use Azure Pipelines to continuously build and deploy Azure Resource Manager template projects.

Azure DevOps 提供开发人员服务,以支持团队规划工作、协作开发代码以及构建和部署应用程序。Azure DevOps provides developer services to support teams to plan work, collaborate on code development, and build and deploy applications. 通过使用 Azure DevOps Services,开发人员能够在云中工作。Developers can work in the cloud using Azure DevOps Services. Azure DevOps 提供了一组集成的功能,可以通过 Web 浏览器或 IDE 客户端访问这些功能。Azure DevOps provides an integrated set of features that you can access through your web browser or IDE client. Azure Pipelines 是这些功能中的一项。Azure Pipeline is one of these features. Azure Pipelines 是一项别具特色的持续集成 (CI) 和持续交付 (CD) 服务。Azure Pipelines is a fully featured continuous integration (CI) and continuous delivery (CD) service. 它适用于你喜欢的 Git 提供程序,并且可以部署到大多数主要云服务。It works with your preferred Git provider and can deploy to most major cloud services. 然后,可以自动执行代码的生成、测试并将其部署到世纪互联 Azure 或 Amazon Web Services。Then you can automate the build, testing, and deployment of your code to 21Vianet Azure, or Amazon Web Services.

备注

选择项目名称。Pick a project name. 完成本教程后,请将任意“AzureRmPipeline”替换为自己的项目名称。When you go through the tutorial, replace any of the AzureRmPipeline with your project name. 此项目名称用于生成资源名称。This project name is used to generate resource names. 其中一个资源是存储帐户。One of the resources is a storage account. 存储帐户名称长度必须为 3 到 24 个字符,并且只能使用数字和小写字母。Storage account names must be between 3 and 24 characters in length and use numbers and lower-case letters only. 该名称必须是唯一的。The name must be unique. 在模板中,存储帐户名称是追加了“store”的项目名称,项目名称的长度必须介于 3 到 11 个字符之间。In the template, the storage account name is the project name with "store" appended, and the project name must be between 3 and 11 characters. 因此,项目名称必须符合存储帐户名称要求,且短于 11 个字符。So the project name must meet the storage account name requirements and has less than 11 characters.

本教程涵盖以下任务:This tutorial covers the following tasks:

  • 准备 GitHub 存储库Prepare a GitHub repository
  • 创建 Azure DevOps 项目Create an Azure DevOps project
  • 创建 Azure 管道Create an Azure pipeline
  • 验证管道部署Verify the pipeline deployment
  • 更新模板并重新部署Update the template and redeploy
  • 清理资源Clean up resources

如果没有 Azure 订阅,请在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

先决条件Prerequisites

若要完成本文,需要做好以下准备:To complete this article, you need:

准备 GitHub 存储库Prepare a GitHub repository

GitHub 用于存储项目源代码,包括资源管理器模板。GitHub is used to store your project source code including Resource Manager templates. 有关其他受支持的存储库,请参阅 Azure DevOps 支持的存储库For other supported repositories, see repositories supported by Azure DevOps.

创建 GitHub 存储库Create a GitHub repository

如果没有 GitHub 帐户,请参阅先决条件If you don't have a GitHub account, see Prerequisites.

  1. 登录 GitHubSign in to GitHub.

  2. 选择右上角的帐户图像,然后选择“你的存储库”。Select your account image on the upper right corner, and then select Your repositories.

    Azure 资源管理器 Azure DevOps Azure Pipelines 创建 GitHub 存储库

  3. 选择“新建”(一个绿色的按钮)。Select New, a green button.

  4. 在“存储库名称”中,输入存储库名称。In Repository name, enter a repository name. 例如,“AzureRmPipeline-repo”。For example, AzureRmPipeline-repo. 请将任意“AzureRmPipeline”替换为自己的项目名称。Remember to replace any of AzureRmPipeline with your project name. 可以选择“公共”或“专用”以完成本教程 。You can select either Public or private for going through this tutorial. 然后,选择“创建存储库”。And then select Create repository.

  5. 记下 URL。Write down the URL. 存储库 URL 的格式为:https://github.com/[YourAccountName]/[YourRepositoryName]The repository URL is the following format - https://github.com/[YourAccountName]/[YourRepositoryName].

此存储库称为远程存储库。This repository is referred to as a remote repository. 同一项目中的每个开发人员都可以克隆自己的本地存储库,并将更改合并到远程存储库。Each of the developers of the same project can clone his/her own local repository, and merge the changes to the remote repository.

克隆远程存储库Clone the remote repository

  1. 打开 Git Shell 或 Git Bash。Open Git Shell or Git Bash. 请参阅先决条件See Prerequisites.

  2. 确认当前文件夹是否为 GitHubVerify your current folder is GitHub.

  3. 运行以下命令:Run the following command:

    git clone https://github.com/[YourAccountName]/[YourGitHubRepositoryName]
    cd [YourGitHubRepositoryName]
    mkdir CreateWebApp
    cd CreateWebApp
    pwd
    

    请将“[YourAccountName]”替换为自己的 GitHub 帐户名称,并将“[YourGitHubRepositoryName]”替换为在上述过程中创建的存储库名称 。Replace [YourAccountName] with your GitHub account name, and replace [YourGitHubRepositoryName] with your repository name you created in the previous procedure.

CreateWebApp 文件夹是存储模板的文件夹。The CreateWebApp folder is the folder where the template is stored. 使用“pwd”命令可显示文件夹路径。The pwd command shows the folder path. 在以下过程中,请将模板保存到该路径。The path is where you save the template to in the following procedure.

下载快速入门模板Download a Quickstart template

如果不创建模板的话,可以下载模板并将其保存到 CreateWebApp 文件夹。Instead of creating the templates, you can download the templates and save them to the CreateWebApp folder.

使用的文件夹名称和文件名与管道中的名称相同。Both the folder name and the file names are used as they are in the pipeline. 如果更改这些名称,则必须更新管道中使用的名称。If you change these names, you must update the names used in the pipeline.

将模板推送到远程存储库Push the template to the remote repository

azuredeploy.json 已添加到本地存储库。The azuredeploy.json has been added to the local repository. 下一步,将模板上传到远程存储库。Next, you upload the template to the remote repository.

  1. 请打开 Git Shell 或 Git Bash(如果未打开) 。Open Git Shell or Git Bash, if it is not opened.

  2. 将目录更改为本地存储库中的 CreateWebApp 文件夹。Change directory to the CreateWebApp folder in your local repository.

  3. 验证 azuredeploy.json 文件是否位于该文件夹中。Verify the azuredeploy.json file is in the folder.

  4. 运行以下命令:Run the following command:

    git add .
    git commit -m "Add web app templates."
    git push origin master
    

    可能会收到一条有关 LF 的警告。You might get a warning about LF. 可以忽略该警告。You can ignore the warning. “主干”表示主分支。master is the master branch. 通常,每次更新创建一个分支。You typically create a branch for each update. 若要简化本教程,请直接使用主分支。To simplify the tutorial, you use the master branch directly.

  5. 从浏览器浏览到 GitHub 存储库。Browse to your GitHub repository from a browser. 该 URL 为 https://github.com/[YourAccountName]/[YourGitHubRepository]The URL is https://github.com/[YourAccountName]/[YourGitHubRepository]. 应会看到 CreateWebApp 文件夹以及其中的三个文件。You shall see the CreateWebApp folder and the three files inside the folder.

  6. 选择“linkedStorageAccount.json”打开模板。Select linkedStorageAccount.json to open the template.

  7. 选择“原始”按钮。Select the Raw button. URL 以 raw.githubusercontent.com 开头。The URL is started with raw.githubusercontent.com.

  8. 复制 URL。Make a copy of the URL. 稍后在本教程中配置管道时需要提供此值。You need to provide this value when you configure the pipeline later in the tutorial.

到目前为止,你已创建一个 GitHub 存储库,并已将模板上传到其中。So far, you have created a GitHub repository, and uploaded the templates to the repository.

创建 DevOps 项目Create a DevOps project

需要 DevOps 组织才能继续进行下一步。A DevOps organization is needed before you can proceed to the next procedure. 如果没有组织,请参阅先决条件If you don't have one, see Prerequisites.

  1. 登录 Azure DevOpsSign in to Azure DevOps.

  2. 从左侧选择一个 DevOps 组织。Select a DevOps organization from the left.

    Azure 资源管理器 Azure DevOps Azure Pipelines 创建 GitHub 存储库

  3. 选择“新建项目”。 Select New project. 如果没有任何项目,将自动打开“创建项目”页面。If you don't have any projects, the create project page is opened automatically.

  4. 输入以下值:Enter the following values:

    • 项目名称:输入项目名称。Project name: enter a project name. 可以使用在本教程开头选择的项目名称。You can use the project name you picked at the very beginning of the tutorial.
    • 版本控制:选择“Git”。Version control: Select Git. 可能需要展开“高级”才能看到“版本控制” 。You might need to expand Advanced to see Version control.

    可以使用其他属性的默认值。Use the default value for the other properties.

  5. 选择“创建” 。Select Create.

创建服务连接,用于将项目部署到 Azure。Create a service connection that is used to deploy projects to Azure.

  1. 从左侧菜单的底部选择“项目设置”。Select Project settings from the bottom of the left menu.

  2. 在“管道”下,选择“服务连接” 。Select Service connections under Pipelines.

  3. 依次选择“新建服务连接”、“Azure 资源管理器”、“下一步”。 Select New Service connection, select Azure Resource Manager, and then select Next.

  4. 依次选择“服务主体(手动)”、“下一步”。 Select Service principal(manual), and then select Next.

    备注

    • 创建服务主体并提前配置其对 Azure 中国云上的 Azure 资源的访问,保存输出供下一步使用。Create a service principal and configure its access to Azure resources on Azure China Cloud in advanced, and save the output for using in the next step.
    az cloud set -n AzureChinaCloud
    az login
    az ad sp create-for-rbac
    

    Azure 资源管理器 Azure DevOps 创建服务主体

  5. 输入以下值:Enter the following values:

    • 环境:选择 AzureChinaCloudEnvironment: select AzureChinaCloud.
    • 范围级别:选择“订阅”。Scope Level: select Subscription.
    • 订阅 ID:输入订阅 ID。Subscription Id: enter your subscription id.
    • 订阅名称:输入订阅名称。Subscription Name: enter your subscription name.
    • 服务主体 ID:输入上面的 az ad sp create-for-rbac 步骤中的 appId 输出。Service Principal ID: enter appId output in the previous az ad sp create-for-rbac step.
    • 凭据:选择“服务主体密钥”。Credential: select Service principal key.
    • 服务主体密钥:输入上面的 az ad sp create-for-rbac 步骤中的 password 输出。Service Principal Key: enter password output in the previous az ad sp create-for-rbac step.
    • 租户 ID:输入上面的 az ad sp create-for-rbac 步骤中的 tenant 输出。Tenant ID: enter tenant output in the pervious az ad sp create-for-rbac step.
    • 连接名称:输入连接名称。Connection name: enter a connection name. 例如,“AzureRmPipeline-conn”。For example, AzureRmPipeline-conn. 请记下此名称,创建管道时需要使用它。Write down this name, you need the name when you create your pipeline.
    • 授予对所有管道的访问权限Grant access permission to all pipelines. (选定)(selected)

    Azure 资源管理器 Azure DevOps 创建 Azure 连接

  6. 选择“验证并保存”。Select Verify and save.

创建管道Create a pipeline

到目前为止,你已完成了以下任务。Until now, you have completed the following tasks. 如果由于能熟练使用 GitHub 和 DevOps 而跳过前面几节,则必须先完成这些任务,然后再继续。If you skip the previous sections because you are familiar with GitHub and DevOps, you must complete the tasks before you continue.

  • 创建 GitHub 存储库,并将模板保存到存储库中的 CreateWebApp 文件夹。Create a GitHub repository, and save the templates to the CreateWebApp folder in the repository.
  • 创建 DevOps 项目,并创建 Azure 资源管理器服务连接。Create a DevOps project, and create an Azure Resource Manager service connection.

若要以包含部署模板步骤的方式创建管道,请执行以下操作:To create a pipeline with a step to deploy a template:

  1. 在左侧菜单中选择“管道”。Select Pipelines from the left menu.

  2. 选择“新建管道”。Select New pipeline.

  3. 从“连接”选项卡中,选择“GitHub” 。From the Connect tab, select GitHub. 如果系统要求输入 GitHub 凭据,然后按照说明进行操作。If asked, enter your GitHub credentials, and then follow the instructions. 如果看到以下屏幕,请选择“仅选择存储库”,并验证存储库是否位于列表中,然后选择“批准和安装” 。If you see the following screen, select Only select repositories, and verify your repository is in the list before you select Approve & Install.

    Azure 资源管理器 Azure DevOps Azure Pipelines 创建 GitHub 存储库

  4. 从“选择”选项卡中,选择自己的存储库。From the Select tab, select your repository. 默认名称为“[YourAccountName]/[YourGitHubRepositoryName]”。The default name is [YourAccountName]/[YourGitHubRepositoryName].

  5. 从“配置”选项卡中,选择“初级管道” 。From the Configure tab, select Starter pipeline. 它显示了“azure-pipelines.yml”管道文件,其中包含两个脚本步骤。It shows the azure-pipelines.yml pipeline file with two script steps.

  6. 从 yml 文件中删除两个脚本步骤。Delete the two script steps from the yml file.

  7. 将光标移到 steps: 后面的行。Move the cursor to the line after steps:.

  8. 在屏幕右侧选择“显示助手”打开“任务”窗格。 Select Show assistant on the right of the screen to open Tasks pane.

  9. 选择“ARM 模板部署”。Select ARM template deployment.

  10. 输入以下值:Enter the following values:

    • deploymentScope:选择“资源组”。deploymentScope: Select Resource Group.. 若要详细了解范围,请参阅部署范围To learn more about the scopes, see Deployment scopes.
    • Azure 资源管理器连接:选择前面创建的服务连接名称。Azure Resource Manager connection: Select the service connection name that you created earlier.
    • 订阅:指定目标订阅 ID。Subscription: Specify the target subscription ID.
    • 操作:选择“创建或更新资源组”操作,执行 2 项操作 - 1.Action: Select the Create Or Update Resource Group action does 2 actions - 1. 如果提供了新的资源组名称,则创建资源组;2.create a resource group if a new resource group name is provided; 2. 部署指定的模板。deploy the template specified.
    • 资源组:输入新的资源组名称。Resource group: Enter a new resource group name. 例如,“AzureRmPipeline-rg”。For example, AzureRmPipeline-rg.
    • 位置:选择资源组的位置,例如“中国北部”。Location: Select a location for the resource group, for example, China North.
    • 模板位置:选择“链接的项目”,表示任务将直接从连接的存储库中查找模板文件。Template location: Select Linked artifact, which means the task looks for the template file directly from the connected repository.
    • 模板:输入 CreateWebApp/azuredeploy.jsonTemplate: Enter CreateWebApp/azuredeploy.json. 如果更改了文件夹名称和文件名,则需要更改此值。If you changed the folder name and the file name, you need to change this value.
    • 模板参数:将此字段留空。Template parameters: Leave this field blank. 将在“替代模板参数”中指定参数值。You will specify the parameter values in the Override template parameters.
    • 替代模板参数:输入 -projectName [EnterAProjectName] -linkedTemplateUri [EnterTheLinkedTemplateURL]Override template parameters: Enter -projectName [EnterAProjectName] -linkedTemplateUri [EnterTheLinkedTemplateURL]. 替换项目名称和链接模板 URL。Replace the project name and the linked template url. 链接模板 URL 是在完成创建 GitHub 存储库部分时记下的 URL。The linked template URL is what you wrote down at the end of Create a GitHub repository. 它以 https://raw.githubusercontent.com 开头。It starts with https://raw.githubusercontent.com.
    • 部署模式:选择“增量”。Deployment mode: Select Incremental.
    • 部署名称:输入 DeployPipelineTemplateDeployment name: Enter DeployPipelineTemplate. 选择“高级”,然后可以看到“部署名称”。 Select Advanced before you can see Deployment name.

    Azure 资源管理器 Azure DevOps Azure Pipelines 创建 GitHub 存储库

  11. 选择 添加Select Add.

    有关任务的详细信息,请参阅 Azure 资源组部署任务Azure 资源管理器模板部署任务For more information about the task, see Azure Resource Group Deployment task, and Azure Resource Manager template deployment task

    yml 文件应如下所示:The yml file shall be similar to:

    Azure 资源管理器 Azure DevOps Azure Pipelines 创建 GitHub 存储库

  12. 选择“保存并运行”。Select Save and run.

  13. 在“保存并运行”窗格中,再次选择“保存并运行”。 From the Save and run pane, select Save and run again. YAML 文件的副本将保存到已连接的存储库中。A copy of the YAML file is saved into the connected repository. 浏览到存储库即可查看该 YAML 文件。You can see the YAML file by browse to your repository.

  14. 验证管道是否成功执行。Verify that the pipeline is executed successfully.

    Azure 资源管理器 Azure DevOps Azure Pipelines 创建 GitHub 存储库

验证部署Verify the deployment

  1. 登录 Azure 门户Sign in to the Azure portal.
  2. 打开资源组。Open the resource group. 名称是在管道 YAML 文件中指定的名称。The name is what you specified in the pipeline YAML file. 你将看到创建了一个存储帐户。You shall see one storage account created. 存储帐户名称以“存储”开头。The storage account name starts with store.
  3. 选择存储帐户名称以将其打开。Select the storage account name to open it.
  4. 选择“属性”。Select Properties. 注意“复制”是“本地冗余存储(LRS)” 。Notice the Replication is Locally-redundant storage (LRS).

更新并重新部署Update and redeploy

更新模板并将更改推送到远程存储库时,管道会自动更新资源,在本例中即存储帐户。When you update the template and push the changes to the remote repository, the pipeline automatically updates the resources, the storage account in this case.

  1. 在 Visual Studio Code 或任何文本编辑器中,打开本地存储库中的 linkedStorageAccount.jsonOpen linkedStorageAccount.json from your local repository in Visual Studio Code or any text editor.

  2. 将“storageAccountType”的“defaultValue”更新为“Standard_GRS” 。Update the defaultValue of storageAccountType to Standard_GRS. 请参阅下面的屏幕截图:See the following screenshot:

    Azure 资源管理器 Azure DevOps Azure Pipelines 创建 GitHub 存储库

  3. 保存更改。Save the changes.

  4. 从 Git Bash/Shell 运行以下命令,将更改推送到远程存储库。Push the changes to the remote repository by running the following commands from Git Bash/Shell.

    git pull origin master
    git add .
    git commit -m "Update the storage account type."
    git push origin master
    

    第一个命令 (pull) 将本地存储库与远程存储库同步。The first command (pull) syncs the local repository with the remote repository. 管道 YAML 文件只会添加到远程存储库。The pipeline YAML file was only added to the remote repository. 运行 pull 命令会将 YAML 文件的副本下载到本地分支。Running the pull command downloads a copy of the YAML file to the local branch.

    第四个命令 (push) 将修改后的 linkedStorageAccount.json 文件上传到远程存储库。The fourth command (push) uploads the revised linkedStorageAccount.json file to the remote repository. 更新远程存储库的主分支后,将再次发送管道。With the master branch of the remote repository updated, the pipeline is fired again.

若要验证所做的更改,可以检查存储帐户的“复制”属性。To verify the changes, you can check the Replication property of the storage account. 请参阅验证部署See Verify the deployment.

清理资源Clean up resources

不再需要 Azure 资源时,请通过删除资源组来清理部署的资源。When the Azure resources are no longer needed, clean up the resources you deployed by deleting the resource group.

  1. 在 Azure 门户上的左侧菜单中选择“资源组” 。From the Azure portal, select Resource group from the left menu.
  2. 在“按名称筛选”字段中输入资源组名称。Enter the resource group name in the Filter by name field.
  3. 选择资源组名称。Select the resource group name.
  4. 在顶部菜单中选择“删除资源组”。 Select Delete resource group from the top menu.

你可能还需要删除 GitHub 存储库和 Azure DevOps 项目。You might also want to delete the GitHub repository and the Azure DevOps project.

后续步骤Next steps

祝贺你完成本资源管理器模板部署教程。Congratulations, you've finished this Resource Manager template deployment tutorial. 如果你有任何意见和建议,请在反馈部分告知我们。Let us know if you have any comments and suggestions in the feedback section. 谢谢!Thanks! 你现在可以学习有关模板的更高级概念了。You're ready to jump into more advanced concepts about templates. 下一教程会更详细地探讨如何根据模板参考文档来定义要部署的资源。The next tutorial goes into more detail about using template reference documentation to help with defining resources to deploy.