Azure Pipelines を使用してカスタム コンテナーを Azure App Service にデプロイする

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

Azure Pipelines を使用すると、Web アプリをビルド、テストし、Linux 上のAzure App Service Web アプリ コンテナーに自動的にデプロイできます。 この記事では、YAML またはクラシック パイプラインを使って次の操作を行う方法について説明します。

  • Docker イメージをビルドして Azure Container Registry に発行する
  • Azure Web アプリを作成する
  • コンテナーを Azure App Service にデプロイする
  • デプロイ スロットにデプロイする

前提条件

コードを取得する

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 にプッシュする] パイプライン テンプレートを選んでください。

    Docker パイプライン テンプレートの選択

  5. [Azure サブスクリプション] を選んでから [続行] を選んでください。

  6. ドロップダウン メニューから [コンテナー レジストリ] を選び、[検証と構成] を選びます。

    Docker の検証と構成

  7. パイプライン YAML テンプレートを確認し、[保存および実行] を選んで 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 portal のコンテナー レジストリに移動し、[リポジトリ] を選びます。

    Azure Container Registry に発行した Docker イメージ

  9. コンテナー レジストリからイメージをデプロイするには、管理者ユーザー アカウントを有効にする必要があります。 Azure portal でコンテナー レジストリに移動し、[アクセス キー] を選びます。 次に、トグル ボタンを選んで管理者ユーザーを有効にします。

    管理者ユーザーを有効にする

Web アプリを作成する

  1. Azure Portalに移動します。

  2. [リソースの作成]>[コンテナー] の順に選んで、[Web App for Containers] を選びます。

    コンテナー リソース用の Web アプリを作成する

  3. 新しい Web アプリの名前を入力し、新しいリソース グループを作成します。 [オペレーティング システム] として [Linux] を選択します。

    Web アプリを構成する

  4. [価格プラン] セクションで、[F1 Free plan] (F1 Free プラン) を選択します。

  5. [確認と作成] を選択します。 構成を確認し、完了したら [作成] を選びます。

Web App for Containers へのデプロイ

この YAML では、Docker イメージをビルドしてコンテナー レジストリにプッシュし、それを Azure Web App for Containers にデプロイします。 [ビルド] ステージでは、Docker@2 タスクを使って Docker イメージをビルドし、Azure Container Registry にプッシュします。 AzureWebAppContainer@1 タスクでは、イメージを Web App for Containers にデプロイします。


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

よく寄せられる質問

Q: Docker レジストリの資格情報を見つけるにはどうすればよいですか?

A: Azure portal に移動し、Web App for Containers を選びます。 [構成]>[アプリケーションの設定] の順に選んで、クリックして値を表示します。

Docker レジストリの資格情報を見つける方法を示したスクリーンショット。