使用 Azure Pipelines 将自定义容器部署到 Azure 应用服务

Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019

使用 Azure Pipelines,可以生成、测试 Web 应用并将其自动部署到 Linux 上的 Azure 应用服务 Web 应用容器。 本文介绍如何使用 YAML 或经典管道来执行以下操作:

  • 生成 Docker 映像并将其发布到 Azure 容器注册表
  • 创建 Azure Web 应用
  • 将容器部署到 Azure 应用服务
  • 部署到部署槽位

先决条件

获取代码

在 GitHub 创建以下示例应用的分支。

https://github.com/spring-guides/gs-spring-boot-docker.git

生成 Docker 映像并将其发布到 Azure 容器注册表

要成功完成本部分,必须具有 Azure 容器注册表。 有关详细信息,请参阅先决条件部分。

  1. 登录到 Azure DevOps 组织,并导航到你的项目。

  2. 选择“管道”,然后“新建管道”。

  3. 当系统提示输入源代码的位置时,选择“GitHub”,然后选择存储库。

  4. 选择“Docker: 生成映像并将其推送到 Azure 容器注册表”管道模板。

    选择 Docker 管道模板

  5. 选择你的 Azure 订阅,然后选择“继续”。

  6. 从下拉菜单中选择“容器注册表”,然后选择“验证和配置”。

    验证和配置 Docker

  7. 查看管道 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)
    
  8. 要在管道运行完成后查看已发布的 Docker 映像,请在 Azure 门户中导航到容器注册表,然后选择“存储库”。

    发布到 Azure 容器注册表的 Docker 映像

  9. 要从容器注册表部署映像,必须启用管理员用户帐户。 在 Azure 门户中导航到容器注册表,然后选择“访问密钥”。 接下来,选择“启用管理员用户”的切换按钮。

    启用管理员用户

创建 Web 应用

  1. 导航到 Azure 门户

  2. 选择“创建资源”>“容器”,然后选择“用于容器的 Web 应用”。

    为容器资源创建 Web 应用

  3. 输入新 Web 应用的名称,并创建新的资源组。 选择“Linux”作为“操作系统”。

    配置 Web 应用

  4. 在“定价计划”部分中,选择“F1 免费计划”

  5. 选择“查看并创建”。 查看配置,完成后选择“创建”。

部署到用于容器的 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 应用。 选择“配置”>“应用程序设置” ,然后单击以显示值。

显示如何查找 Docker 注册表凭据的屏幕截图。