Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019
使用 Azure Pipelines,可以生成、测试 Web 应用并将其自动部署到 Linux 上的 Azure 应用服务 Web 应用容器。 本文将介绍如何使用 YAML 或经典管道来执行以下操作:
- 生成 Docker 映像并将其发布到 Azure 容器注册表。
- 创建 Azure Web 应用。
- 将容器部署到 Azure 应用服务。
- 部署到部署槽位。
先决条件
产品 | 要求 |
---|---|
Azure DevOps | - Azure DevOps 组织和项目。
免费创建一个。 - 权限: - 若要授予对项目中所有管道的访问权限:必须是 项目管理员组的成员。 - 若要创建服务连接:必须具有服务连接的管理员或创建者角色。 - 能够在 Microsoft 托管的代理上运行管道。 可以购买 并行作业 ,也可以请求免费层。 |
GitHub | - GitHub 帐户。 |
天蓝色 | - Azure 容器注册表。 创建 Azure 容器注册表(如果还没有)。 |
获取代码
在 GitHub 创建以下示例应用的分支。
https://github.com/spring-guides/gs-spring-boot-docker.git
生成 Docker 映像并将其发布到 Azure 容器注册表
要成功完成本部分,必须具有 Azure 容器注册表。 有关详细信息,请参阅先决条件部分。
登录到 Azure DevOps 组织,并导航到你的项目。
选择“管道”,然后选择“新建管道”。
当系统提示输入源代码的位置时,选择“GitHub”,然后选择存储库。
选择“Docker: 生成映像并将其推送到 Azure 容器注册表”管道模板。
选择你的 Azure 订阅,然后选择“继续”。
从下拉菜单中选择“容器注册表”,然后选择“验证和配置”。
查看管道 YAML 模板,然后选择“ 保存并运行 ”以生成 Docker 映像并将其发布到 Azure 容器注册表。
trigger: - main resources: - repo: self variables: # Container registry service connection established during pipeline creation dockerRegistryServiceConnection: '{{ containerRegistryConnection.Id }}' imageRepository: 'javascriptdocker' containerRegistry: 'sampleappcontainerregistry.azurecr.io' dockerfilePath: '$(Build.SourcesDirectory)/app/Dockerfile' tag: '$(Build.BuildId)' # Agent VM image name vmImageName: 'ubuntu-latest' stages: - stage: Build displayName: Build and push stage jobs: - job: Build displayName: Build pool: vmImage: $(vmImageName) steps: - task: Docker@2 displayName: Build and push an image to container registry inputs: command: buildAndPush repository: $(imageRepository) dockerfile: $(dockerfilePath) containerRegistry: $(dockerRegistryServiceConnection) tags: | $(tag)
若要在管道运行完成后查看已发布的 Docker 映像,请转到 Azure 门户中的容器注册表,然后选择 “存储库”。
若要从容器注册表部署映像,请启用管理员用户帐户。 转到 Azure 门户中的容器注册表,然后选择 “访问密钥”。 然后,选择切换按钮以 启用管理员用户。
创建 Web 应用
转到 Azure 门户。
选择“ 创建资源>容器”,然后选择 用于容器的 Web 应用。
输入新 Web 应用的名称并创建新的资源组。 选择“Linux”作为“操作系统”。
在 “定价计划 ”部分,选择 F1 免费计划。
选择“查看并创建”。 查看您的配置,在完成后选择创建。
部署到用于容器的 Web 应用
在此 YAML 中,将生成 Docker 映像并将其推送到容器注册表,然后将其部署到用于容器的 Azure Web 应用。 在“生成”阶段,将使用 Docker@2 任务生成 Docker 映像并将其推送到 Azure 容器注册表。 AzureWebAppContainer@1 任务可将映像部署到用于容器的 Web 应用。
trigger:
- main
resources:
- repo: self
variables:
## Add this under variables section in the pipeline
azureSubscription: <Name of the Azure subscription>
appName: <Name of the Web App>
containerRegistry: <Name of the Azure container registry>
dockerRegistryServiceConnection: '4fa4efbc-59af-4c0b-8637-1d5bf7f268fc'
imageRepository: <Name of image repository>
dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
tag: '$(Build.BuildId)'
vmImageName: 'ubuntu-latest'
stages:
- stage: Build
displayName: Build and push stage
jobs:
- job: Build
displayName: Build
pool:
vmImage: $(vmImageName)
steps:
- task: Docker@2
displayName: Build and push an image to container registry
inputs:
command: buildAndPush
repository: $(imageRepository)
dockerfile: $(dockerfilePath)
containerRegistry: $(dockerRegistryServiceConnection)
tags: |
$(tag)
## Add the below snippet at the end of your pipeline
- task: AzureWebAppContainer@1
displayName: 'Azure Web App on Container Deploy'
inputs:
azureSubscription: $(azureSubscription)
appName: $(appName)
containers: $(containerRegistry)/$(imageRepository):$(tag)
部署到部署槽位
将 Azure Web 应用容器配置为具有多个槽。 插槽能够让您安全地部署应用并进行测试,在将其提供给客户之前。 在 “创建过渡环境”中了解详细信息。
以下 YAML 代码片段演示如何部署到过渡槽,然后交换到生产槽:
- task: AzureWebAppContainer@1
inputs:
azureSubscription: '<Azure service connection>'
appName: '<Name of the web app>'
containers: $(containerRegistry)/$(imageRepository):$(tag)
deployToSlotOrASE: true
resourceGroupName: '<Name of the resource group>'
slotName: staging
- task: AzureAppServiceManage@0
inputs:
azureSubscription: '<Azure service connection>'
WebAppName: '<name of web app>'
ResourceGroupName: '<name of resource group>'
SourceSlot: staging
SwapWithProduction: true
常见问题解答
问:如何查找 Docker 注册表凭据?
答:转到 Azure 门户,然后选择用于容器的 Web 应用。 选择 “配置>应用程序”设置,然后选择以显示值。