LoginSignup
0
0

More than 1 year has passed since last update.

【Azure DevOps】コンテナアプリケーションをWebApp for Containerにデプロイする

Last updated at Posted at 2022-08-29

Azure DevOps Organizationsを作成

Azureポータルから「Azure DevOps Organizations」リソースを選択
image.png

新規のOrganizationsを作成

image.png

プロジェクトを作成

image.png
※今回はシンプルなNuxtのSPAをリポジトリにPushしました

コンテナー レジストリ(ACR)を作成

  • 作成されたら管理者ユーザーを有効化しておきます。
    image.png

DevOpsでコンテナをACRにPushするパイプラインを作成

image.png

Azure Repos Gitを選択
image.png

Dockerビルド+ACRへのPushのテンプレートを選択し、対象のサブスクリプションを指定
image.png

先程作成したACRを選択
image.png

ベースのYamlファイルを生成してくれるのでNuxtアプリケーションのビルドコマンドを追記してSave and Run

# Docker
# Build and push an image to Azure Container Registry
# https://docs.microsoft.com/azure/devops/pipelines/languages/docker
trigger:
  - master

resources:
  - repo: self

variables:
  # Container registry service connection established during pipeline creation
  dockerRegistryServiceConnection: "8abda5f8-xxxx-xxxx-xxxx-a6d3b853a87c"
  imageRepository: "demo"
  containerRegistry: "dvosdemo.azurecr.io"
  dockerfilePath: "$(Build.SourcesDirectory)/Dockerfile"
  tag: "latest"
  # Agent VM image name
  vmImageName: "ubuntu-latest"

pool:
  vmImage: $(vmImageName)

steps:
  - script: npm install --save nuxt
    displayName: "npm install"

  - script: npm run generate
    displayName: "npm generate"

  - task: Docker@2
    displayName: Build and push an image to container registry
    inputs:
      command: buildAndPush
      repository: $(imageRepository)
      dockerfile: $(dockerfilePath)
      containerRegistry: $(dockerRegistryServiceConnection)
      tags: |
        $(tag)

実行が成功するとACRにPushされたコンテナを確認できる
image.png

サービスコネクションとは

dockerRegistryServiceConnectionはサービスコネクションと呼ばれるAzureリソースの認証情報を含んだ接続情報で、別のパイプラインから使用する等汎用的に利用できます。
今回の場合、対象のサブスクリプション指定した段階で作成されています。
コネクションの管理はDevOpsプロジェクトのService connectionsから確認できます。
image.png

サービスコネクションのについてもう少し深堀すると、実態はサービスプリンシパルです。
今回のケースだと対象のACRのIAMに共同作成者として作成されたサービスプリンシパルが追加されています。
したがってサービスコネクションの作成には、対象テナントのAzureADで「アプリケーションを作成できる権限」とAzureサブスクリプション側では「ユーザーアクセス管理者」(サービスプリンシパルにロールを割り当てるための権限)を持っているアカウントで実施する必要があることに注意です。
サブスクリプションやリソースの接続時にエラーになった場合はこの辺りを確認してみてください。

Azure Hosted Runnerを使う際の注意点

パイプライン実行時に以下のようなエラーに遭遇

No hosted parallelism has been purchased or granted. To request a free parallelism grant, please fill out the following form https://aka.ms/azpipelines-parallelism-request
原因:https://docs.microsoft.com/ja-jp/azure/devops/release-notes/2021/sprint-184-update
解決方法:
https://qiita.com/Hachiyou-Renge/items/2083f2ce9e8b38558805

WebApp for Containerの作成

続いてデプロイ先のWebAppを作成します。

基本設定

  • Linuxコンテナーを選択します
    image.png

  • 後々デプロイメントスロットを使いたいので P1v2を指定
    image.png

Docker

  • 事前に準備したACRのイメージを選択します
    image.png
    ※ACRで管理者ユーザーを有効化していないとエラーになるので注意

動作確認

作成されたWebAppにアクセスし、アプリケーションが動作していればOK
image.png

デプロイ用のReleaseを作成

DevOpsのプロジェクトに戻り、PipelinesのReleasesを選択⇒New pipeline
image.png

Artifacts

ArtifactのSouce typeはBuildを選択し、最新のPipeline完了後に起動するように設定します
image.png

トリガーを有効化
image.png

Stage

Agent Job:Stageが実行される環境の設定
この環境に対してTaskを追加していく
image.png

Azure App Service deploy
image.png

Azure App Service manage:WebAppの再起動
上記deployの実行のみでは明示的なイメージの再起動とプルは行いません
そのため、無理やりですがTaskとして再起動を実施しています。
※アプリケーションの瞬断が発生するので気になる方は一度デプロイスロットにデプロイ⇒スロットを再起動⇒スワップで回避できます(別記事で紹介)
image.png

動作確認

アプリケーションを修正してコミットするとPipeline⇒Releasesの順で実行され、WebAppが更新されれば完成です!

Pipelineのログ
image.png

Releases 各TaskのログはLogsから確認可能
image.png

WebAppも無事更新されました!
image.png

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0