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

教程:使用 Azure DevOps Projects 部署 ASP.NET 应用和 Azure SQL 数据库代码Tutorial: Deploy your ASP.NET app and Azure SQL Database code by using Azure DevOps Projects

Azure DevOps Projects 提供一种简化的体验,你在其中既可使用现有的代码和 Git 存储库,也可选择一个示例应用程序,以便创建连接到 Azure 的持续集成 (CI) 和持续交付 (CD) 管道。Azure DevOps Projects presents a simplified experience where you can bring your existing code and Git repo or choose a sample application to create a continuous integration (CI) and continuous delivery (CD) pipeline to Azure.

DevOps Projects 还可以:DevOps Projects also:

  • 自动创建 Azure 资源,例如 Azure SQL 数据库。Automatically creates Azure resources, such as an Azure SQL database.
  • 在 Azure Pipelines 中创建并配置一个发布管道,其中包括用于 CI 的生成管道。Creates and configures a release pipeline in Azure Pipelines that includes a build pipeline for CI.
  • 为 CD 设置发布管道。Sets up a release pipeline for CD.
  • 创建用于监视的 Azure Application Insights 资源。Creates an Azure Application Insights resource for monitoring.

在本教程中,将:In this tutorial, you will:

  • 使用 Azure DevOps Projects 部署 ASP.NET 应用和 Azure SQL 数据库代码Use Azure DevOps Projects to deploy your ASP.NET app and Azure SQL Database code
  • 配置 Azure DevOps 和 Azure 订阅Configure Azure DevOps and an Azure subscription
  • 检查 CI 管道Examine the CI pipeline
  • 检查 CD 管道Examine the CD pipeline
  • 提交对 Azure Repos 所做的更改并将其自动部署到 AzureCommit changes to Azure Repos and automatically deploy them to Azure
  • 连接到 Azure SQL 数据库Connect to the Azure SQL database
  • 清理资源Clean up resources

先决条件Prerequisites

在 DevOps Projects 中为 ASP.NET 应用和 Azure SQL 数据库创建项目Create a project in DevOps Projects for an ASP.NET app and an Azure SQL database

DevOps Projects 在 Azure Pipelines 中创建 CI/CD 管道。DevOps Projects creates a CI/CD pipeline in Azure Pipelines. 可以创建新的 Azure DevOps 组织,或使用现有的组织。You can create a new Azure DevOps organization or use an existing organization. DevOps Projects 还可在所选的 Azure 订阅中创建 Azure 资源,例如 Azure SQL 数据库。DevOps Projects also creates Azure resources, such as an Azure SQL database, in the Azure subscription of your choice.

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 在左窗格中,选择“创建资源”。In the left pane, select Create a resource.

  3. 在搜索框中键入 DevOps Projects,然后选择“创建”。In the search box, type DevOps Projects, and then select Create.

    DevOps Projects 仪表板

  4. 依次选择“.NET”、“下一步”。Select .NET, and then select Next.

  5. 在“选择应用程序框架”下,选择“ASP.NET”。Under Choose an application framework, select ASP.NET.

  6. 依次选择“添加数据库”、“下一步”。Select Add a database, and then select Next.
    在前述步骤中选择的应用程序框架规定了此处可用的 Azure 服务部署目标的类型。The application framework, which you chose in a previous step, dictates the type of Azure service deployment target that's available here.

  7. 选择“下一步”。Select Next.

配置 Azure DevOps 和 Azure 订阅Configure Azure DevOps and an Azure subscription

  1. 创建新的 Azure DevOps 组织,或选择现有的组织。Create a new Azure DevOps organization, or select an existing organization.

  2. 输入 Azure DevOps 项目的名称。Enter a name for your Azure DevOps project.

  3. 选择你的 Azure 订阅服务。Select your Azure subscription services.
    (可选)若要查看其他 Azure 配置设置并确定“数据库服务器登录详细信息”部分的用户名,可以选择“更改”。Optionally, to view additional Azure configuration settings and to identify the username in the Database Server Login Details section, you can select Change. 存储该用户名,以便在本教程的后续步骤中使用。Store the username for future steps in this tutorial. 如果执行此可选步骤,请退出 Azure 配置区域,然后选择“完成”。If you perform this optional step, exit the Azure configuration area before you select Done.

  4. 选择“完成”。Select Done.
    几分钟后,该过程将会完成,DevOps Projects 仪表板将在 Azure 门户中打开。After a few minutes, the process is completed and the DevOps Projects dashboard opens in the Azure portal. 也可以从 Azure 门户中的“所有资源”直接导航到仪表板。You can also navigate to the dashboard directly from All resources in the Azure portal. 在右侧,选择“浏览”可查看正在运行的应用程序。At the right, select Browse to view your running application.

检查 CI 管道Examine the CI pipeline

DevOps Projects 会自动在 Azure Repos 中配置一个完整的 CI/CD 管道。DevOps Projects automatically configures a full CI/CD pipeline in Azure Repos. 可以浏览和自定义此管道。You can explore and customize the pipeline. 若要了解 Azure DevOps 生成管道,请执行以下操作:To familiarize yourself with the Azure DevOps build pipeline, do the following:

  1. 在 DevOps Projects 仪表板顶部,选择“生成管道”。At the top of the DevOps Projects dashboard, select Build pipelines.
    浏览器标签页会显示新项目的生成管道。A browser tab displays the build pipeline for your new project.

  2. 指向“状态”字段,然后选择省略号 (...)。Point to the Status field, and then select the ellipsis (...).
    菜单中会显示多个选项,例如,将新生成排队、暂停某个生成,以及编辑生成管道。A menu displays several options, such as queueing a new build, pausing a build, and editing the build pipeline.

  3. 选择“编辑”。Select Edit.

  4. 在此窗格中,可以检查生成管道的各种任务。In this pane, you can examine the various tasks for your build pipeline.
    该生成会执行各种任务,例如,从 Git 存储库提取源、还原依赖项、发布用于部署的输出。The build performs various tasks, such as fetching sources from the Git repository, restoring dependencies, and publishing outputs used for deployments.

  5. 在生成管道的顶部,选择生成管道名称。At the top of the build pipeline, select the build pipeline name.

  6. 将生成管道的名称更改为更具描述性的名称,选择“保存并排队”,然后选择“保存”。Change the name of your build pipeline to something more descriptive, select Save & queue, and then select Save.

  7. 在生成管道名称下,选择“历史记录”。Under your build pipeline name, select History.
    此窗格显示最近针对生成所做的更改的审核线索。This pane displays an audit trail of your recent changes for the build. Azure Pipelines 会跟踪对生成管道所做的任何更改,并允许进行版本比较。Azure Pipelines keeps track of any changes made to the build pipeline, and it allows you to compare versions.

  8. 选择“触发器”。Select Triggers.
    DevOps Projects 会自动创建一个 CI 触发器,每次向存储库提交内容都会启动新的生成。DevOps Projects automatically creates a CI trigger, and every commit to the repository starts a new build. (可选)可以选择在 CI 过程中包括或排除分库。Optionally, you can choose to include or exclude branches from the CI process.

  9. 选择“保留期”。Select Retention.
    可以根据方案指定策略,以保留或删除特定数目的生成。Depending on your scenario, you can specify policies to keep or remove a certain number of builds.

检查 CD 管道Examine the CD pipeline

DevOps Projects 会自动创建并配置从 Azure DevOps 组织部署到 Azure 订阅所要执行的步骤。DevOps Projects automatically creates and configures the necessary steps to deploy from your Azure DevOps organization to your Azure subscription. 这些步骤包括配置 Azure 服务连接,以便在 Azure 订阅中进行 Azure DevOps 身份验证。These steps include configuring an Azure service connection to authenticate Azure DevOps to your Azure subscription. 自动化还会创建一个 CD 管道,用于提供到 Azure 虚拟机的 CD。The automation also creates a CD pipeline, which provides the CD to the Azure virtual machine. 若要详细了解 Azure DevOps CD 管道,请执行以下操作:To learn more about the Azure DevOps CD pipeline, do the following:

  1. 依次选择“生成和发布”、“发布”。Select Build and Release, and then select Releases.
    DevOps Projects 会创建一个发布管道,用于管理到 Azure 的部署。DevOps Projects creates a release pipeline to manage deployments to Azure.

  2. 选择发布管道旁边的省略号 (...),然后选择“编辑”。Select the ellipsis (...) next to your release pipeline, and then select Edit.
    发布管道包含一个管道,用于定义发布过程。The release pipeline contains a pipeline, which defines the release process.

  3. 在“项目”下选择“删除”。Under Artifacts, select Drop.
    在前述步骤中检查过的生成管道会生成用于项目的输出。The build pipeline you examined in the previous steps produces the output that's used for the artifact.

  4. 在“删除”图标的右侧,选择“持续部署触发器”。At the right of the Drop icon, select Continuous deployment trigger.
    此发布管道有一个已启用的 CD 触发器,每次有新的生成项目可用时,此触发器就会执行部署。This release pipeline has an enabled CD trigger, which executes a deployment every time a new build artifact is available. (可选)可以禁用此触发器,这样就需要手动执行部署。Optionally, you can disable the trigger so that your deployments require manual execution.

    DevOps Projects 会设置一个随机 SQL 密码并将它用于发布管道。DevOps Projects sets up a random SQL password and uses it for the release pipeline.

  5. 在左侧选择“变量”。At the left, select Variables.

    备注

    仅当更改了 SQL Server 密码时,才执行以下步骤。Perform the following step only if you changed the SQL Server password. 有一个 password 变量。There is a single password variable.

  6. 在“值”框的旁边选择挂锁图标,输入新密码,然后选择“保存”。Next to the Value box, select the padlock icon, enter the new password, and then select Save.

  7. 在左侧选择“任务”,然后选择你的环境。At the left, select Tasks, and then select your environment.
    任务是部署过程执行的活动,已按阶段分组。Tasks are the activities that your deployment process executes, and they are grouped in phases. 此发布管道只有一个阶段,此阶段包含“Azure 应用服务部署”和“Azure SQL 数据库部署”任务。This release pipeline has a single phase, which contains an Azure App Service Deploy and Azure SQL Database Deployment task.

  8. 选择“执行 Azure SQL”任务,并检查用于 SQL 部署的各个属性。Select the Execute Azure SQL task, and examine the various properties that are used for the SQL deployment.
    在“部署包”下,该任务使用 SQL DACPAC 文件。Under Deployment Package, the task uses a SQL DACPAC file.

  9. 在右侧选择“查看发布”,以显示发布历史记录。At the right, select View releases to display a history of releases.

  10. 选择某个发布旁边的省略号 (...),然后选择“打开”。Select the ellipsis (...) next to a release, and then select Open.
    可以浏览多个菜单,例如发布摘要、关联的工作项和测试。You can explore several menus, such as a release summary, associated work items, and tests.

  11. 选择“提交”。Select Commits.
    此视图显示与此部署关联的代码提交。This view shows code commits that are associated with this deployment. 请对发布进行比较,查看不同部署之间的提交差异。Compare releases to view the commit differences between deployments.

  12. 选择“日志”。Select Logs.
    日志包含有关部署过程的有用信息。The logs contain useful information about the deployment process. 可以在部署期间和之后查看日志。You can view them both during and after deployments.

提交对 Azure Repos 所做的更改并将其自动部署到 AzureCommit changes to Azure Repos and automatically deploy them to Azure

备注

以下过程通过进行简单的文本更改来测试 CI/CD 管道。The following procedure tests the CI/CD pipeline with a simple text change. 若要测试 SQL 部署过程,可以选择性地对表进行 SQL Server 架构更改。To test the SQL deployment process, you can optionally make a SQL Server schema change to the table.

现在,可以使用可将最新工作部署到网站的 CI/CD 过程,与某个团队展开应用协作。You're now ready to collaborate with a team on your app by using a CI/CD process that automatically deploys your latest work to your website. 每次对 Git 存储库进行更改都会在 Azure DevOps 中启动一个生成,CD 管道可以执行到 Azure 的部署。Each change to the Git repo starts a build in Azure DevOps, and a CD pipeline executes a deployment to Azure. 遵循本部分所述的过程,或使用其他方法提交对存储库所做的更改。Follow the procedure in this section, or use another technique to commit changes to your repository. 代码更改会启动 CI/CD 过程,并自动将更改部署到 Azure。The code changes initiate the CI/CD process and automatically deploy your changes to Azure.

  1. 在左窗格中选择“代码”,然后转到你的存储库。In the left pane, select Code, and then go to your repository.

  2. 转到 SampleWebApplication\Views\Home 目录,选择 Index.cshtml 文件旁边的省略号 (...),然后选择“编辑”。Go to the SampleWebApplication\Views\Home directory, select the ellipsis (...) next to the Index.cshtml file, and then select Edit.

  3. 对该文件进行更改,例如,在某个 div 标记内部添加一些文本。Make a change to the file, such as adding some text within one of the div tags.

  4. 在右上角选择“提交”,然后再次选择“提交”以推送更改。At the top right, select Commit, and then select Commit again to push your change.
    片刻之后,某个生成会在 Azure DevOps 中启动,并且某个发布将会执行以部署更改。After a few moments, a build starts in Azure DevOps and a release executes to deploy the changes. 使用 Azure DevOps 组织通过 DevOps Projects 仪表板或浏览器监视生成状态。Monitor the build status in the DevOps Projects dashboard or in the browser with your Azure DevOps organization.

  5. 发布完成后,刷新应用程序以验证更改。After the release is completed, refresh your application to verify your changes.

连接到 Azure SQL 数据库Connect to the Azure SQL database

需要相应的权限才能连接到 Azure SQL 数据库。You need appropriate permissions to connect to the Azure SQL database.

  1. 在 DevOps Projects 仪表板中选择“SQL 数据库”,转到 SQL 数据库的管理页。On the DevOps Projects dashboard, select SQL Database to go to the management page for the SQL database.

  2. 依次选择“设置服务器防火墙”、“添加客户端 IP”。Select Set server firewall, and then select Add client IP.

  3. 选择“保存”。Select Save.
    现在,该客户端 IP 可以访问 SQL Server Azure 资源。Your client IP now has access to the SQL Server Azure resource.

  4. 返回到“SQL 数据库”窗格。Go back to the SQL Database pane.

  5. 在右侧选择服务器名称,导航到 SQL Server 的配置页。At the right, select the server name to navigate to the configuration page for SQL Server.

  6. 选择“重置密码”,输入 SQL Server 管理员登录名的密码,然后选择“保存”。Select Reset password, enter a password for the SQL Server admin login, and then select Save.
    请务必保存此密码,因为本教程稍后需要用到。Be sure to keep this password to use later in this tutorial.

    现在可以选择性地使用客户端工具(例如 SQL Server Management Studio 或 Visual Studio)连接到 SQL Server 和 Azure SQL 数据库。You may now optionally use client tools such as SQL Server Management Studio or Visual Studio to connect to SQL Server and the Azure SQL database. 使用“服务器名称”属性进行连接。Use the Server name property to connect.

    如果最初在 DevOps Projects 中配置项目时未更改数据库用户名,则用户名为电子邮件地址的本地部分。If you didn't change the database username when you initially configured the project in DevOps Projects, your username is the local part of your email address. 例如,如果电子邮件地址为“johndoe@microsoft.com”,则用户名为“johndoe”。For example, if your email address is johndoe@microsoft.com, your username is johndoe.

    备注

    如果更改了 SQL 登录名的密码,则必须根据“检查 CD 管道”部分中所述,在发布管道变量中更改密码。If you change your password for the SQL login, you must change the password in the release pipeline variable, as described in the "Examine the CD pipeline" section.

清理资源Clean up resources

测试后,可以清理资源,以避免产生费用。If you are testing, you can avoid accruing billing charges by cleaning up your resources. 不再需要本教程中创建的 Azure SQL 数据库和相关资源时,可将其删除。When they are no longer needed, you can delete the Azure SQL database and related resources that you created in this tutorial. 为此,可以使用 DevOps Projects 仪表板上的“删除”功能。To do so, use the Delete functionality on the DevOps Projects dashboard.

重要

以下过程会永久删除资源。The following procedure permanently deletes resources. “删除”功能会销毁 DevOps Projects 中的项目在 Azure 和 Azure DevOps 中创建的数据,删除后无法检索这些数据。The Delete functionality destroys the data that's created by the project in DevOps Projects in both Azure and Azure DevOps, and you will be unable to retrieve it. 只能在仔细阅读提示后才使用此过程。Use this procedure only after you've carefully read the prompts.

  1. 在 Azure 门户中,转到 DevOps Projects 仪表板。In the Azure portal, go to the DevOps Projects dashboard.
  2. 在右上角选择“删除”。At the top right, select Delete.
  3. 出现提示时,请选择“是”以永久删除这些资源。At the prompt, select Yes to permanently delete the resources.

后续步骤Next steps

可以选择根据所在团队的需求修改这些生成和发布管道。You can optionally modify these build and release pipelines to meet the needs of your team. 也可以将此 CI/CD 模式用作其他管道的模板。You can also use this CI/CD pattern as a template for your other pipelines. 本教程介绍了如何:In this tutorial, you learned how to:

  • 使用 Azure DevOps Projects 部署 ASP.NET 应用和 Azure SQL 数据库代码Use Azure DevOps Projects to deploy your ASP.NET app and Azure SQL Database code
  • 配置 Azure DevOps 和 Azure 订阅Configure Azure DevOps and an Azure subscription
  • 检查 CI 管道Examine the CI pipeline
  • 检查 CD 管道Examine the CD pipeline
  • 提交对 Azure Repos 所做的更改并将其自动部署到 AzureCommit changes to Azure Repos and automatically deploy them to Azure
  • 连接到 Azure SQL 数据库Connect to the Azure SQL database
  • 清理资源Clean up resources

若要详细了解 CI/CD 管道,请参阅:To learn more about the CI/CD pipeline, see:

视频Videos