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

使用 Azure DevOps Projects 创建适用于 Go 的 CI/CD 管道Create a CI/CD pipeline for Go by using Azure DevOps Projects

使用 Azure DevOps Projects 为 Go 应用配置持续集成 (CI) 和持续交付 (CD)。Configure continuous integration (CI) and continuous delivery (CD) for your Go app by using Azure DevOps Projects. DevOps Projects 可以简化 Azure DevOps 生成和发布管道的初始配置。DevOps Projects simplifies the initial configuration of an Azure DevOps build and release pipeline.

如果没有 Azure 订阅,可以通过 Visual Studio Dev Essentials 免费获取一个。If you don't have an Azure subscription, you can get one 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 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. 登录到 Azure 门户Sign in to the Azure portal.

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

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

    DevOps Projects 仪表板

选择示例应用和 Azure 服务Select a sample app and Azure service

  1. 选择 Go 示例应用,然后选择“下一步”。Select the Go sample app, and then select Next.

  2. 示例 Go 应用是默认的框架。Simple Go app is the default framework. 选择“下一步”。Select Next.
    前面选择的应用框架规定了可用于部署的 Azure 服务部署目标的类型。The app framework, which you chose previously, dictates the type of Azure service deployment targets that are available for deployment.

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

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

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

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

  3. 选择 Azure 订阅和位置,输入应用的名称,然后选择“完成”。Select your Azure subscription and location, enter a name for your app, and then select Done.
    几分钟后,DevOps Projects 仪表板会显示在 Azure 门户中。After a few minutes, the DevOps Projects dashboard is displayed in the Azure portal. 将在 Azure DevOps 组织的存储库中设置一个示例应用,执行生成,并将应用部署到 Azure。A sample app is set up in a repo in your Azure DevOps organization, a build is executed, and your app is deployed to Azure.

    在此仪表板中可以查看代码存储库、CI/CD 管道,以及 Azure 中的应用。The dashboard provides visibility into your code repo, your CI/CD pipeline, and your app in Azure. 在右侧,选择“浏览”即可查看正在运行的应用。At the right, select Browse to view your running app.

    仪表板视图

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

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

  1. 在 DevOps Projects 的左侧,选择主分支的链接。On the DevOps Projects, at the left, select the link for your master branch.
    该链接会打开新建的 Git 存储库的视图。The link opens a view to the newly created Git repo.

  2. 若要查看存储库克隆 URL,请在右上角选择“克隆”。To view the repo clone URL, select Clone at the top right.
    可以在常用的 IDE 中克隆 Git 存储库。You can clone your Git repo 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. 在左侧,转到“views/index.html”文件并选择“编辑”。At the left, go to the views/index.html file, and then select Edit.

  4. 对该文件进行更改。Make a change to the file. 例如,在某个 div 标记内部修改某些文本。For example, modify some text within 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.
    此时应有一个生成正在进行。A build should be in progress. 所做的更改会自动通过 CI/CD 管道进行生成和部署。The changes you made are automatically built and deployed via a CI/CD pipeline.

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

DevOps Projects 会自动在 Azure Repos 中配置一个完整的 CI/CD 管道。DevOps Projects automatically configures a full CI/CD pipeline in Azure Repos. 根据需要浏览和自定义管道。Explore and customize the pipeline as needed. 若要了解 Azure DevOps 生成和发布管道,请执行以下操作:To familiarize yourself with the Azure DevOps build and release pipelines, do the following:

  1. 转到 DevOps Projects 仪表板。Go to the DevOps Projects dashboard.

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

  3. 指向“状态”字段,然后选择省略号 (...)。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.

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

  5. 在此窗格中,可以检查生成管道的各种任务。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 repo, restoring dependencies, and publishing outputs used for deployments.

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

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

  8. 在生成管道名称下,选择“历史记录”。Under your build pipeline name, select History.
    此窗格显示最近针对生成所做的更改的审核线索。This pane displays 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.

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

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

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

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

  13. 在“项目”下选择“删除”。Under Artifacts, select Drop.
    前面检查过的生成管道将生成用于项目的输出。The build pipeline you examined previously produces the output that's used for the artifact.

  14. 在“删除”图标的右侧,选择“持续部署触发器”。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.

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

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

  17. 选择某个发布旁边的省略号 (...),然后选择“打开”。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.

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

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

清理资源Clean up resources

不再需要本快速入门中创建的 Azure 应用服务实例和相关资源时,可将其删除。When they are no longer needed, you can delete the Azure App Service instance and related resources that you created in this quickstart. 为此,可以使用 DevOps Projects 仪表板上的“删除”功能。To do so, use the Delete functionality on the DevOps Projects dashboard.

后续步骤Next steps

若要详细了解如何根据团队的需求修改生成和发布管道,请参阅:To learn more about modifying the build and release pipelines to meet the needs of your team, see: