使用 Azure Pipelines 將自訂容器部署至 Azure App 服務

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

您可以使用 Azure Pipelines,在 Linux 上建置、測試及自動將 Web 應用程式部署至 Azure App 服務 Web 應用程式容器。 在本文中,您將瞭解如何使用 YAML 或傳統管線來:

  • 建置 Docker 映射並將其發佈至 Azure Container Registry
  • 建立 Azure Web 應用程式
  • 將容器部署至 Azure App 服務
  • 部署至部署位置

必要條件

取得程式碼

在 GitHub 派生下列範例應用程式。

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

建置 Docker 映射並將其發佈至 Azure Container Registry

若要順利完成本節,您必須擁有 Azure Container Registry。 如需詳細資訊,請參閱必要條件一節。

  1. 登入您的 Azure DevOps 組織,並流覽至您的專案。

  2. 選取 [管線],然後選取 [新增管線]。

  3. 當系統提示您輸入原始程式碼的位置時,請 選取 [GitHub ],然後選取您的存放庫。

  4. 選取 Docker:建置並將映像推送至 Azure Container Registry 管線範本。

    Select Docker pipeline template

  5. 選取您的 Azure 訂用帳戶,然後選取 [ 繼續]。

  6. 從下拉功能表中選取您的 容器登錄 ,然後選取 [ 驗證並設定]。

    Validate and configure Docker

  7. 檢閱管線 YAML 範本,然後選取 [ 儲存並執行 ] 以建置 Docker 映射,並將 Docker 映像發佈至您的 Azure Container Registry。

    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 入口網站 中的容器登錄,然後選取 [存放庫]。

    Docker image published to Azure Container Registry

  9. 若要從容器登錄部署映像,您必須啟用系統管理員用戶帳戶。 流覽至 Azure 入口網站 中的容器登錄,然後選取 [存取金鑰]。 接下來,選取切換按鈕以啟用 管理員 使用者

    Enable Admin user

建立 Web 應用程式

  1. 巡覽至 Azure 入口網站

  2. 選取 [建立資源>容器],然後選擇 [用於容器的 Web 應用程式]。

    Create a web app for containers resource

  3. 輸入新 Web 應用程式的名稱,然後建立新的資源群組。 針對 [操作系統] 選取 [Linux]。

    Configure the web app

  4. 在 [ 定價方案] 區段中,選擇 F1 免費方案

  5. 選取 [檢閱和建立]。 檢閱您的設定,然後在完成時選取 [ 建立 ]。

部署至適用於容器的 Web 應用程式

在此 YAML 中,您會建置 Docker 映像並將其推送至容器登錄,然後將它部署到適用於容器的 Azure Web 應用程式。 在 [建置] 階段中,您會使用 Docker@2 工作,建置 Docker 映射並推送至 Azure Container RegistryAzureWebAppContainer@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 應用程式。 選取 [組態>應用程式設定],然後按下以顯示值。

A screenshot showing how to find Docker registry credentials.