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

使用 Azure DevOps Projects 创建用于 Python 的 CI/CD 管道Create a CI/CD pipeline for Python with Azure DevOps Projects

Azure DevOps Projects 提供一种简化的体验,适合为 Python 应用创建 Azure 资源并设置持续集成 (CI) 和持续交付 (CD) 管道。Azure DevOps Projects presents a simplified experience that creates Azure resources and sets up a continuous integration (CI) and continuous delivery (CD) pipeline for your Python app.

如果没有 Azure 订阅,可以通过 Visual Studio Dev Essentials 免费获取一个。If you don't have an Azure subscription, you can get one for free through Visual Studio Dev Essentials.

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

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

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

  2. 在左窗格中选择“创建资源”图标,然后搜索“DevOps Projects”。Select the Create a resource icon in the left pane, and then search for DevOps Projects.

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


选择示例应用程序和 Azure 服务Select a sample application and Azure service

  1. 选择 Python 示例应用程序。Select the Python sample application. Python 示例包括一个选项,可以从多个应用程序框架中进行选择。The Python samples include a choice of several application frameworks.

  2. 默认的示例框架为 Django。The default sample framework is Django. 保留默认设置,然后选择“下一步”。Leave the default setting, and then select Next.
    “适用于容器的 Web 应用”是默认的部署目标。Web App For Containers is the default deployment target. 以前选择的应用程序框架规定了此处提供的 Azure 服务部署目标的类型。The application framework, which you chose previously, dictates the type of Azure service deployment target available here.

  3. 保留默认服务,然后选择“下一步”。Leave the default service, and then select Next.

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

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

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

    b.b. 选择 Azure 订阅和位置,输入应用程序的名称,然后选择“完成”。Select your Azure subscription and location, enter a name for your application, and then select Done.
    几分钟后,Projects 仪表板会显示在 Azure 门户中。After few minutes, the project dashboard is displayed in the Azure portal. 首先会在 Azure DevOps 组织的存储库中设置示例应用程序,然后执行生成并将应用程序部署到 Azure。A sample application is set up in a repository in your Azure DevOps organization, a build is executed, and your application is deployed to Azure. 可以通过此仪表板查看代码存储库、你的 CI/CD 管道以及 Azure 中的应用程序。This dashboard provides visibility into your code repository, your CI/CD pipeline, and your application in Azure.

  2. 选择“浏览”,查看正在运行的应用程序。Select Browse to view your running application.


    DevOps Projects 会自动配置 CI 生成和发布触发器。DevOps Projects automatically configures a CI build and release trigger. 现在,可以使用可将最新工作部署到网站的 CI/CD 过程,与某个团队展开 Python 应用协作。You're now ready to collaborate with a team on a Python app by using a CI/CD process that automatically deploys your latest work to your website.

提交代码更改并执行 CI/CDCommit code changes and execute CI/CD

DevOps Projects 在 Azure Repos 或 GitHub 中创建一个 Git 存储库。DevOps Projects creates a Git repository in Azure Repos or GitHub. 若要查看存储库并对应用程序进行代码更改,请执行以下操作:To view the repository and make code changes to your application, do the following:

  1. 在 DevOps Projects 仪表板的左侧,选择 master 分库的链接。On the left side of the DevOps Projects dashboard, select the link for your master branch.
    该链接会打开新建 Git 存储库的视图。This link opens a view to the newly created Git repository.

  2. 若要查看存储库克隆 URL,请在浏览器右上角选择“克隆”。To view the repository clone URL, select Clone from the top right of the browser.
    可以在最常用的 IDE 中克隆 Git 存储库。You can clone your Git repository in your favorite IDE. 在后续几个步骤中,可以使用 Web 浏览器直接对 master 分库进行代码更改并提交所做的更改。In the next few steps, you can use the web browser to make and commit code changes directly to the master branch.

  3. 在左侧转到 app/templates/app/index.html 文件。On the left, go to the app/templates/app/index.html file.

  4. 选择“编辑”,对某些文本进行更改。Select Edit, and make a change to some of the text. 例如,更改某个 div 标记的某些文本。For example, change some of the text for one of the div tags.

  5. 选择“提交”并保存更改。Select Commit, and then save your changes.

  6. 在浏览器中,转到 DevOps Projects 仪表板。In your browser, go to the DevOps Projects dashboard.
    此时会看到一个生成正在进行。You should now see a build in progress. 刚做的更改会自动通过 CI/CD 管道进行生成和部署。The changes you just made are automatically built and deployed via a CI/CD pipeline.

检查 CI/CD 管道Examine the CI/CD pipeline

在上一步中,DevOps Projects 自动配置了一个完整的 CI/CD 管道。In the previous step, DevOps Projects automatically configured a full CI/CD pipeline. 根据需要浏览和自定义管道。Explore and customize the pipeline as needed. 若要了解生成和发布管道,请执行以下操作:To familiarize yourself with the build and release pipelines, 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 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.
    此时会看到最近针对生成所做的更改的审核线索。You see an audit trail of your recent changes for the build. Azure DevOps 会跟踪对生成管道所做的任何更改,并允许进行版本比较。Azure DevOps 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 过程中包括或排除分库。You can optionally 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.

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

  11. 选择发布管道旁边的省略号,然后选择“编辑”。Select the ellipsis next to your release pipeline, and then select Edit.
    发布管道定义发布过程。The release pipeline defines the release process.

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

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

  14. 在左侧,选择“任务”。On the left, select Tasks.
    这些任务是部署过程执行的活动。The tasks are the activities that your deployment process performs. 在此示例中,已创建一个用于将项目部署到 Azure 应用服务的任务。In this example, a task was created to deploy to Azure App Service.

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

  16. 选择某个发布旁边的省略号 (...),然后选择“打开”。Select the ellipsis (...) next to one of your releases, and then select Open.
    可以在此视图中浏览多个菜单,例如发布摘要、关联的工作项、测试。There are several menus to explore from this view, such as a release summary, associated work items, and tests.

  17. 选择“提交”。Select Commits. 此视图显示与特定部署相关联的代码提交。This view shows the code commits that are associated with the specific deployment.

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

清理资源Clean up resources

不再需要 Azure 应用服务和相关资源时,可将其删除。You can delete Azure App Service and related resources when you don't need them anymore. 请使用 DevOps Projects 仪表板上的“删除”功能。Use the Delete functionality on the DevOps Projects dashboard.

后续步骤Next steps

当你配置 CI/CD 过程时,系统自动创建了生成和发布管道。When you configured your CI/CD process, build and release pipelines were automatically created. 可以根据你的团队的需求修改这些生成和发布管道。You can modify these build and release pipelines to meet the needs of your team. 若要详细了解 CI/CD 管道,请参阅:To learn more about the CI/CD pipeline, see: