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 にコンテナーをデプロイします。
- デプロイ スロットにデプロイします。
前提条件
製品 | 必要条件 |
---|---|
Azure DevOps | - Azure DevOps の組織とプロジェクト。
無料で作成できます。 - アクセス許可: - プロジェクト内のすべてのパイプラインへのアクセスを許可するには、プロジェクト 管理者グループのメンバーである必要があります。 - サービス接続を作成するには、サービス接続の管理者ロールまたは作成者ロールが必要です。 - Microsoft がホストするエージェントでパイプラインを実行する機能。 並列ジョブを購入することも、Free レベルを要求することもできます。 |
GitHubの | - GitHub アカウント。 |
紺碧 | - 1つのAzure Container Registry。 まだ作成していない場合は、Azure コンテナー レジストリを作成します。 |
コードを取得する
GitHub で次のサンプル アプリをフォークします。
https://github.com/spring-guides/gs-spring-boot-docker.git
Docker イメージをビルドして Azure Container Registry に発行する
このセクションを正常に完了するには、Azure Container Registry が必要です。 詳細については、「前提条件」セクションを参照してください。
Azure DevOps 組織にサインインし、プロジェクトに移動します。
[パイプライン] を選んでから [新しいパイプライン] を選びます。
ソース コードの場所の入力を求められたら [GitHub] を選び、リポジトリを選びます。
[Docker: イメージをビルドし、Azure Container Registry にプッシュする] パイプライン テンプレートを選んでください。
[Azure サブスクリプション] を選んでから [続行] を選んでください。
ドロップダウン メニューから [コンテナー レジストリ] を選び、[検証と構成] を選びます。
パイプライン 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)
パイプラインの実行が完了した後に発行された Docker イメージを表示するには、Azure portal でコンテナー レジストリに移動し、[ リポジトリ] を選択します。
コンテナー レジストリからイメージをデプロイするには、管理者ユーザー アカウントを有効にします。 Azure portal でコンテナー レジストリに移動し、[ アクセス キー] を選択します。 次に、トグル ボタンを選択して 管理者ユーザーを有効にします。
Web アプリを作成する
Azure ポータルに移動します。
[ リソースの作成>コンテナー] を選択し、[ Web App for Containers] を選択します。
新しい Web アプリの名前を入力し、新しいリソース グループを作成します。 [オペレーティング システム] として [Linux] を選択します。
[ 料金プラン ] セクションで、 F1 無料プランを選択します。
[確認と作成] を選択します。 構成を確認し、完了したら [作成 ] を選択します。
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 App コンテナーを構成します。 スロットを使用すると、アプリを安全にデプロイしてテストしてから、顧客が利用できるようにします。 詳細については、 ステージング環境の作成に関するページを参照してください。
次の 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 アプリを選択します。 [ 構成>アプリケーション設定] を選択し、値を表示する場合に選択します。