您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

教程:使用 Azure DevOps Services 和 IIS 创建持续集成管道Tutorial: Create a continuous integration pipeline with Azure DevOps Services and IIS

要将应用程序开发的生成、测试和部署阶段自动化,可以使用持续集成和部署 (CI/CD) 管道。To automate the build, test, and deployment phases of application development, you can use a continuous integration and deployment (CI/CD) pipeline. 本教程介绍如何在 Azure 中使用 Azure DevOps Services 和 Windows 虚拟机 (VM) 创建一个运行 IIS 的 CI/CD 管道。In this tutorial, you create a CI/CD pipeline using Azure DevOps Services and a Windows virtual machine (VM) in Azure that runs IIS. 学习如何:You learn how to:

  • 将 ASP.NET Web 应用程序发布到 Azure DevOps Services 项目Publish an ASP.NET web application to a Azure DevOps Services project
  • 创建由代码提交触发的生成管道Create a build pipeline that is triggered by code commits
  • 在 Azure 中的虚拟机上创建和配置 IISInstall and configure IIS on a virtual machine in Azure
  • 将 IIS 实例添加到 Azure DevOps Services 中的部署组Add the IIS instance to a deployment group in Azure DevOps Services
  • 创建发布管道,用于将新的 Web 部署包发布到 IISCreate a release pipeline to publish new web deploy packages to IIS
  • 测试 CI/CD 管道Test the CI/CD pipeline

本教程需要 Azure PowerShell 模块 5.7.0 或更高版本。This tutorial requires the Azure PowerShell module version 5.7.0 or later. 运行 Get-Module -ListAvailable AzureRM 即可查找版本。Run Get-Module -ListAvailable AzureRM to find the version. 如果需要升级,请参阅安装 Azure PowerShell 模块If you need to upgrade, see Install Azure PowerShell module.

在 Azure DevOps Services 中创建项目Create a project in Azure DevOps Services

使用 Azure DevOps Services 可以轻松进行协作和开发,而无需维护本地代码管理解决方案。Azure DevOps Services allows for easy collaboration and development without maintaining an on-premises code management solution. Azure DevOps Services 提供云代码测试、生成和应用程序见解信息。Azure DevOps Services provides cloud code testing, build, and application insights. 可以选择最适合用于代码开发的版本控制存储库和 IDE。You can choose a version control repo and IDE that best fits your code development. 对于本教程,可以使用免费组织来创建基本的 ASP.NET Web 应用和 CI/CD 管道。For this tutorial, you can use a free organization to create a basic ASP.NET web app and CI/CD pipeline. 如果没有 Azure DevOps Services 组织,请创建一个If you do not already have a Azure DevOps Services organization, create one.

若要管理代码提交过程、生成管道和发布管道,请按如下所述,在 Azure DevOps Services 中创建一个项目:To manage the code commit process, build pipelines, and release pipelines, create a project in Azure DevOps Services as follows:

  1. 在 Web 浏览器中打开 Azure DevOps Services 仪表板,并选择“新建项目”。Open your Azure DevOps Services dashboard in a web browser and choose New project.
  2. 为“项目名称”输入 myWebAppEnter myWebApp for the Project name. 保留其他所有默认值,即使用“Git”版本控制和“Agile”工作项过程。Leave all other default values to use Git version control and Agile work item process.
  3. 选择“与团队成员共享”选项,然后选择“创建”。Choose the option to Share with Team Members, then select Create.
  4. 创建项目后,请选择“使用自述文件或 gitignore 初始化”选项,并选择“初始化”。Once your project has been created, choose the option to Initialize with a README or gitignore, then Initialize.
  5. 在新项目中,选择顶部的“仪表板”,并选择“在 Visual Studio 中打开”。Inside your new project, choose Dashboards across the top, then select Open in Visual Studio.

创建 ASP.NET Web 应用程序Create ASP.NET web application

上一步骤已在 Azure DevOps Services 中创建一个项目。In the previous step, you created a project in Azure DevOps Services. 最后一个步骤是在 Visual Studio 中打开新项目。The final step opens your new project in Visual Studio. 可在“团队资源管理器”窗口中管理代码提交。You manage your code commits in the Team Explorer window. 创建新项目的本地副本,并通过模板创建 ASP.NET Web 应用程序,如下所示:Create a local copy of your new project, then create an ASP.NET web application from a template as follows:

  1. 选择“克隆”创建 Azure DevOps Services 项目的本地 git 存储库。Select Clone to create a local git repo of your Azure DevOps Services project.

    从 Azure DevOps Services 项目克隆存储库

  2. 在“解决方案”下面,选择“新建”。Under Solutions, select New.

    创建 Web 应用程序解决方案

  3. 选择“Web”模板,并选择“ASP.NET Web 应用程序”模板。Select Web templates, and then choose the ASP.NET Web Application template.

    1. 输入应用程序的名称(例如 myWebApp),并取消选中“创建解决方案的目录”对应的框。Enter a name for your application, such as myWebApp, and uncheck the box for Create directory for solution.
    2. 取消选中“将 Application Insights 添加到项目”对应的框(如果该选项可用)。If the option is available, uncheck the box to Add Application Insights to project. Application Insights 要求使用 Azure Application Insights 为 Web 应用程序授权。Application Insights requires you to authorize your web application with Azure Application Insights. 本教程为了保持内容简洁,已跳过此过程。To keep it simple in this tutorial, skip this process.
    3. 选择“确定”。Select OK.
  4. 从模板列表中选择“MVC”。Choose MVC from the template list.
    1. 依次选择“更改身份验证”、“无身份验证”、“确定”。Select Change Authentication, choose No Authentication, then select OK.
    2. 选择“确定”创建解决方案。Select OK to create your solution.
  5. 在“团队资源管理器”窗口中选择“更改”。In the Team Explorer window, choose Changes.

    将本地更改提交到 Azure Repos Git 存储库

  6. 在提交文本框中输入一条消息,例如“初始提交”。In the commit text box, enter a message such as Initial commit. 从下拉菜单中选择“全部提交并同步”。Choose Commit All and Sync from the drop-down menu.

创建生成管道Create build pipeline

在 Azure DevOps Services 中,可以使用生成管道来概述如何生成应用程序。In Azure DevOps Services, you use a build pipeline to outline how your application should be built. 本教程会创建一个基本的管道,它采用我们的源代码,生成解决方案,然后创建可用于在 IIS 服务器上运行 Web 应用的 Web 部署包。In this tutorial, we create a basic pipeline that takes our source code, builds the solution, then creates web deploy package we can use to run the web app on an IIS server.

  1. 在 Azure DevOps Services 项目的顶部选择“生成和发布”,并选择“生成”。In your Azure DevOps Services project, choose Build & Release across the top, then select Builds.
  2. 选择“+ 新建定义”。Select + New definition.
  3. 选择“ASP.NET (预览)”模板,并选择“应用”。Choose the ASP.NET (PREVIEW) template and select Apply.
  4. 保留所有默认任务值。Leave all the default task values. 在“获取源”下面,确保已选择“myWebApp”存储库和“master”分支。Under Get sources, ensure that the myWebApp repository and master branch are selected.

    在 Azure DevOps Services 项目中创建生成管道

  5. 在“触发器”选项卡上,将“启用此触发器”对应的滑块移动到“已启用”。On the Triggers tab, move the slider for Enable this trigger to Enabled.

  6. 保存生成管道,选择“保存并排队”将新的生成排入队列,并再次选择“保存并排队”。Save the build pipeline and queue a new build by selecting Save & queue , then Save & queue again. 保留默认值并选择“排队”。Leave the defaults and select Queue.

监视生成在托管代理上的计划过程,并开始生成。Watch as the build is scheduled on a hosted agent, then begins to build. 输出类似于以下示例:The output is similar to the following example:

成功生成 Azure DevOps Services 项目

创建虚拟机Create virtual machine

若要提供一个平台来运行 ASP.NET Web 应用,需要一个运行 IIS 的 Windows 虚拟机。To provide a platform to run your ASP.NET web app, you need a Windows virtual machine that runs IIS. 提交提交代码和触发生成时,Azure DevOps Services 使用代理来与 IIS 实例交互。Azure DevOps Services uses an agent to interact with the IIS instance as you commit code and builds are triggered.

使用 New-AzureRmVM 创建 Windows Server 2016 VM。Create a Windows Server 2016 VM with New-AzureRmVM. 以下示例在“East US”位置创建一个名为 myVM 的 VM。The following example creates a VM named myVM in the East US location. 此外,还会创建资源组 myResourceGroupAzureDevOpsServices 和支持性网络资源。The resource group myResourceGroupAzureDevOpsServices and supporting network resources are also created. 要允许 Web 流量,可向虚拟机打开 TCP 端口 80。To allow web traffic, TCP port 80 is opened to the VM. 出现提示时,请提供用作 VM 登录凭据的用户名和密码:When prompted, provide a username and password to be used as the login credentials for the VM:

# Create user object
$cred = Get-Credential -Message "Enter a username and password for the virtual machine."

# Create a virtual machine
New-AzureRmVM `
  -ResourceGroupName "myResourceGroupAzureDevOpsServices" `
  -Name "myVM" `
  -Location "East US" `
  -ImageName "Win2016Datacenter" `
  -VirtualNetworkName "myVnet" `
  -SubnetName "mySubnet" `
  -SecurityGroupName "myNetworkSecurityGroup" `
  -PublicIpAddressName "myPublicIp" `
  -Credential $cred `
  -OpenPorts 80

若要连接到 VM,请使用 Get-AzureRmPublicIpAddress 获取公共 IP 地址,如下所示:To connect to your VM, obtain the public IP address with Get-AzureRmPublicIpAddress as follows:

Get-AzureRmPublicIpAddress -ResourceGroupName "myResourceGroup" | Select IpAddress

与 VM 建立远程桌面会话。Create a remote desktop session to your VM:

mstsc /v:<publicIpAddress>

在 VM 上,打开“管理员 PowerShell”命令提示符。On the VM, open an Administrator PowerShell command prompt. 按如下所示安装 IIS 和所需的 .NET 功能:Install IIS and required .NET features as follows:

Install-WindowsFeature Web-Server,Web-Asp-Net45,NET-Framework-Features

创建部署组Create deployment group

若要将 Web 部署包推送到 IIS 服务器,请在 Azure DevOps Services 中定义一个部署组。To push out the web deploy package to the IIS server, you define a deployment group in Azure DevOps Services. 将代码提交到 Azure DevOps Services 以及完成生成时,可在此组中指定哪些服务器要用作新生成的目标。This group allows you to specify which servers are the target of new builds as you commit code to Azure DevOps Services and builds are completed.

  1. 在 Azure DevOps Services 中选择“生成和发布”,然后选择“部署组”。In Azure DevOps Services, choose Build & Release and then select Deployment groups.
  2. 选择“添加部署组”。Choose Add Deployment group.
  3. 输入组的名称(例如 myIIS),并选择“创建”。Enter a name for the group, such as myIIS, then select Create.
  4. 在“注册计算机”部分中,请确保已选择“Windows”,并选中“使用脚本中的个人访问令牌进行身份验证”对应的框。In the Register machines section, ensure Windows is selected, then check the box to Use a personal access token in the script for authentication.
  5. 选择“将脚本复制到剪贴板”。Select Copy script to clipboard.

将 IIS VM 添加到部署组Add IIS VM to the deployment group

创建部署组后,请将每个 IIS 实例添加到该组。With the deployment group created, add each IIS instance to the group. Azure DevOps Services 将生成一个脚本,用于在 VM 上下载并配置一个代理,以便接收新的 Web 部署包,然后将这些包应用到 IIS。Azure DevOps Services generates a script that downloads and configures an agent on the VM that receives new web deploy packages then applies it to IIS.

  1. 返回到 VM 上的“管理员 PowerShell”会话,粘贴并运行从 Azure DevOps Services 复制的脚本。Back in the Administrator PowerShell session on your VM, paste and run the script copied from Azure DevOps Services.
  2. 当系统提示配置代理的标记时,请选择“Y”,并输入 webWhen prompted to configure tags for the agent, choose Y and enter web.
  3. 当系统提示输入用户帐户时,请按 Return 接受默认值。When prompted for the user account, press Return to accept the defaults.
  4. 等待脚本完成并出现消息“服务 vstsagent.account.computername 已成功启动”。Wait for the script to finish with a message Service vstsagent.account.computername started successfully.
  5. 在“生成和发布”菜单的“部署组”页中,打开 myIIS 部署组。In the Deployment groups page of the Build & Release menu, open the myIIS deployment group. 在“计算机”选项卡上,检查 VM 是否已列出。On the Machines tab, verify that your VM is listed.

    VM 已成功添加到 Azure DevOps Services 部署组

创建发布管道Create release pipeline

若要发布生成,请在 Azure DevOps Services 中创建一个发布管道。To publish your builds, you create a release pipeline in Azure DevOps Services. 成功生成应用程序时,会自动触发此管道。This pipeline is triggered automatically by a successful build of your application. 选择要将 Web 部署包推送到的部署组,然后定义相应的 IIS 设置。You choose the deployment group to push your web deploy package to, and define the appropriate IIS settings.

  1. 选择“生成和发布”,并选择“生成”。Choose Build & Release, then select Builds. 选择在上一步骤中创建的生成管道。Choose the build pipeline created in a previous step.
  2. 在“最近完成”下面选择最近的生成,并选择“发布”。Under Recently completed, choose the most recent build, then select Release.
  3. 选择“是”创建发布管道。Choose Yes to create a release pipeline.
  4. 选择“空”模板,并选择“下一步”。Choose the Empty template, then select Next.
  5. 检查项目中是否已填充项目与源生成管道。Verify the project and source build pipeline are populated with your project.
  6. 选中“持续部署”复选框,并选择“创建”。Select the Continuous deployment check box, then select Create.
  7. 选择“+ 添加任务”旁边的下拉列表框,并选择“添加部署组阶段”。Select the drop-down box next to + Add tasks and choose Add a deployment group phase.

    在 Azure DevOps Services 中将任务添加到发布管道

  8. 选择“IIS Web 应用部署(预览)”旁边的“添加”,并选择“关闭”。Choose Add next to IIS Web App Deploy(Preview), then select Close.

  9. 选择“在部署组上运行”父任务。Select the Run on deployment group parent task.

    1. 对于“部署组”,请选择前面创建的部署组,例如 myIISFor Deployment Group, select the deployment group you created earlier, such as myIIS.
    2. 在“计算机标记”框中,选择“添加”,并选择“web”标记。In the Machine tags box, select Add and choose the web tag.

      IIS 的发布管道部署组任务

  10. 选择“部署: IIS Web 应用部署”任务以配置 IIS 实例设置,如下所示:Select the Deploy: IIS Web App Deploy task to configure your IIS instance settings as follows:

    1. 对于“网站名称”,请输入“默认网站”。For Website Name, enter Default Web Site.
    2. 保留其他所有默认设置。Leave all the other default settings.
  11. 选择“保存”,并选择“确定”两次。Choose Save, then select OK twice.

创建版本并发布Create a release and publish

现在,可将 Web 部署包推送为新版本。You can now push your web deploy package as a new release. 此步骤将与构成部署组的每个实例上的代理通信,推送 Web 部署包,然后将 IIS 配置为运行更新的 Web 应用程序。This step communicates with the agent on each instance that is part of the deployment group, pushes the web deploy package, then configures IIS to run the updated web application.

  1. 在发布管道中选择“+ 发布”,然后选择“创建发布”。In your release pipeline, select + Release, then choose Create Release.
  2. 检查下拉列表中是否已选择最新的生成以及“自动化部署: 创建发布后”。Verify that the latest build is selected in the drop-down list, along with Automated deployment: After release creation. 选择创建Select Create.
  3. 发布管道的顶部会显示一个小标题,例如“已创建发布‘Release-1’”。A small banner appears across the top of your release pipeline, such as Release 'Release-1' has been created. 选择发布链接。Select the release link.
  4. 打开“日志”选项卡查看发布进度。Open the Logs tab to watch the release progress.

    已成功完成 Azure DevOps Services 发布并推送 Web 部署包

  5. 发布完成后,请打开 Web 浏览器并输入 VM 的公共 IIP 地址。After the release is complete, open a web browser and enter the public IIP address of your VM. ASP.NET Web 应用程序正在运行。Your ASP.NET web application is running.

    在 IIS VM 上运行的 ASP.NET Web 应用

测试整个 CI/CD 管道Test the whole CI/CD pipeline

在 IIS 上运行 Web 应用程序后,请尝试测试整个 CI/CD 管道。With your web application running on IIS, now try the whole CI/CD pipeline. 在 Visual Studio 中进行更改并提交代码后,将触发生成,然后触发将更新的 Web 部署包发布到 IIS 的过程:After you make a change in Visual Studio and commit your code, a build is triggered which then triggers a release of your updated web deploy package to IIS:

  1. 在 Visual Studio 中打开“解决方案资源管理器”窗口。In Visual Studio, open the Solution Explorer window.
  2. 导航到“myWebApp”|“视图”|“主页”|“Index.cshtml”并将它打开Navigate to and open myWebApp | Views | Home | Index.cshtml
  3. 编辑第 6 行,如下所示:Edit line 6 to read:

    <h1>ASP.NET with Azure DevOps Services and CI/CD!</h1>

  4. 保存文件。Save the file.

  5. 打开“团队资源管理器”窗口,选择“myWebApp”项目,并选择“更改”。Open the Team Explorer window, select the myWebApp project, then choose Changes.
  6. 输入提交消息(例如“测试 CI/CD 管道”),并从下拉菜单中选择“全部提交并同步”。Enter a commit message, such as Testing CI/CD pipeline, then choose Commit All and Sync from the drop-down menu.
  7. 在 Azure DevOps Services 工作区中,已通过代码提交触发了新的生成。In Azure DevOps Services workspace, a new build is triggered from the code commit.
    • 选择“生成和发布”,并选择“生成”。Choose Build & Release, then select Builds.
    • 选择生成管道,并选择“已排队和正在运行”生成来监视生成进度。Choose your build pipeline, then select the Queued & running build to watch as the build progresses.
  8. 成功生成后,将触发新的发布。Once the build is successful, a new release is triggered.
    • 选择“生成和发布”,并选择“发布”查看已推送到 IIS VM 的 Web 部署包。Choose Build & Release, then Releases to see the web deploy package pushed to your IIS VM.
    • 选择“刷新”图标更新状态。Select the Refresh icon to update the status. 如果“环境”列显示绿色的复选标记,则表示已成功将发布部署到 IIS。When the Environments column shows a green check mark, the release has successfully deployed to IIS.
  9. 若要查看已应用的更改,请在浏览器中刷新 IIS 网站。To see your changes applied, refresh your IIS website in a browser.

    通过 CI/CD 管道在 IIS VM 上运行的 ASP.NET Web 应用

后续步骤Next steps

本教程已在 Azure DevOps Services 中创建一个 ASP.NET Web 应用程序,并配置了生成和发布管道,以便每次提交代码时会新的 Web 部署包部署到 IIS。In this tutorial, you created an ASP.NET web application in Azure DevOps Services and configured build and release pipelines to deploy new web deploy packages to IIS on each code commit. 你已了解如何:You learned how to:

  • 将 ASP.NET Web 应用程序发布到 Azure DevOps Services 项目Publish an ASP.NET web application to a Azure DevOps Services project
  • 创建由代码提交触发的生成管道Create a build pipeline that is triggered by code commits
  • 在 Azure 中的虚拟机上创建和配置 IISInstall and configure IIS on a virtual machine in Azure
  • 将 IIS 实例添加到 Azure DevOps Services 中的部署组Add the IIS instance to a deployment group in Azure DevOps Services
  • 创建发布管道,用于将新的 Web 部署包发布到 IISCreate a release pipeline to publish new web deploy packages to IIS
  • 测试 CI/CD 管道Test the CI/CD pipeline

转到下一教程,了解如何在一对 Windows VM 上安装 SQL\IIS\.NET 网络堆栈。Advance to the next tutorial to learn how to install a SQL\IIS\.NET stack on a pair of Windows VMs.