Azure Spring Apps へのアプリケーション デプロイを自動化する

Note

Azure Spring Apps は、Azure Spring Cloud サービスの新しい名前です。 サービスの名前は新しくなりましたが、スクリーンショット、ビデオ、図などの資産の更新に取り組んでいる間、場所によってはしばらく古い名前が表示されます。

この記事の適用対象: ✔️ Basic または Standard ✔️ Enterprise

この記事では、Azure Pipelines 用の Azure Spring Apps タスクを使用し、アプリケーションをデプロイする方法について説明します。

継続的インテグレーションと継続的デリバリーのツールを使用すると、最小限の労力とリスクで、既存のアプリケーションにすばやく更新プログラムをデプロイできます。 Azure DevOps は、これらの主要なジョブを整理および制御するのに役立ちます。

次の動画では、Azure Pipelines など、自分で選択したツールを使用したエンドツーエンドの自動化について説明します。


Azure Resource Manager サービス接続を作成する

まず、ご自分の Azure DevOps プロジェクトへの Azure Resource Manager サービス接続を作成します。 手順については、「Microsoft Azure に接続する」を参照してください。 Azure Spring Apps サービス インスタンスに使用しているものと同じサブスクリプションを必ず選択してください。

アプリをビルドして配置する

一連のタスクを使用し、プロジェクトをビルドしてデプロイできるようになりました。 次の Azure Pipelines テンプレートでは、変数のほかに、アプリケーションをビルドする .NET Core タスク、アプリケーションをデプロイする Azure Spring Apps タスクを定義しています。

variables:
  workingDirectory: './steeltoe-sample'
  planetMainEntry: 'Microsoft.Azure.SpringCloud.Sample.PlanetWeatherProvider.dll'
  solarMainEntry: 'Microsoft.Azure.SpringCloud.Sample.SolarSystemWeather.dll'
  planetAppName: 'planet-weather-provider'
  solarAppName: 'solar-system-weather'
  serviceName: '<your service name>'

steps:
# Restore, build, publish and package the zipped planet app
- task: DotNetCoreCLI@2
  inputs:
    command: 'publish'
    publishWebProjects: false
    arguments: '--configuration Release'
    zipAfterPublish: false
    modifyOutputPath: false
    workingDirectory: $(workingDirectory)

# Deploy the planet app
- task: AzureSpringCloud@0
  inputs:
    azureSubscription: '<Service Connection Name>'
    Action: 'Deploy'
    AzureSpringCloud: $(serviceName)
    AppName: 'testapp'
    DeploymentType: 'Artifacts'
    UseStagingDeployment: false
    DeploymentName: 'default'
    Package: $(workingDirectory)/src/$(planetAppName)/publish-deploy-planet.zip
    RuntimeVersion: 'NetCore_31'
    DotNetCoreMainEntryPath: $(planetMainEntry)

# Deploy the solar app
- task: AzureSpringCloud@0
  inputs:
    azureSubscription: '<Service Connection Name>'
    Action: 'Deploy'
    AzureSpringCloud: $(serviceName)
    AppName: 'testapp'
    DeploymentType: 'Artifacts'
    UseStagingDeployment: false
    DeploymentName: 'default'
    Package: $(workingDirectory)/src/$(solarAppName)/publish-deploy-solar.zip
    RuntimeVersion: 'NetCore_31'
    DotNetCoreMainEntryPath: $(solarMainEntry)

Azure Spring Apps インスタンスと Azure DevOps プロジェクトを設定する

まず次の手順に従い、Azure DevOps で使用する既存の Azure Spring Apps インスタンスを設定します。

  1. Azure Spring Apps インスタンスに移動して、新しいアプリを作成します。
  2. Azure DevOps ポータルに移動し、選択した組織で新しいプロジェクトを作成します。 Azure DevOps 組織がない場合は、無料で作成できます。
  3. [Repos] を選択し、Spring Boot のデモ コードをリポジトリにインポートします。

Azure Resource Manager サービス接続を作成する

次に、Azure DevOps プロジェクトへの Azure Resource Manager サービス接続を作成します。 手順については、「Microsoft Azure に接続する」を参照してください。 Azure Spring Apps サービス インスタンスに使用しているものと同じサブスクリプションを必ず選択してください。

アプリをビルドして配置する

一連のタスクを使用し、プロジェクトをビルドしてデプロイできるようになりました。 以降のセクションでは、Azure DevOps を使用してアプリをデプロイするためのさまざまな方法を紹介します。

パイプラインを使用してデプロイする

パイプラインを使用してデプロイするには、次の手順に従います。

  1. [パイプライン] を選択し、Maven テンプレートを使用して新しいパイプラインを作成します。

  2. azure-pipelines.yml ファイルを編集して、mavenPomFile フィールドを 'complete/pom.xml' に設定します。

  3. 右側の [アシスタントを表示する] を選択して、Azure Spring Apps テンプレートを選択します。

  4. 自分の Azure サブスクリプションに対して作成したサービス接続を選択し、Azure Spring Apps インスタンスとアプリ インスタンスを選択します。

  5. [Use Staging Deployment]\(ステージング デプロイを使用する\) を無効にします。

  6. [Package or folder]\(パッケージまたはフォルダー\)[complete/target/spring-boot-complete-0.0.1-SNAPSHOT.jar] に設定します。

  7. [追加] を選択して、このタスクをパイプラインに追加します。

    パイプラインの設定が次の画像と一致している必要があります。

    Azure DevOps のスクリーンショット。新しいパイプライン設定を確認できます。

    次のパイプライン テンプレートを使用してプロジェクトをビルド、デプロイすることもできます。 この例では、アプリケーションをビルドする Maven タスクが最初に定義され、次に Azure Pipelines 用の Azure Spring Apps タスクを使用して JAR ファイルをデプロイする 2 つ目のタスクが定義されています。

    steps:
    - task: Maven@3
      inputs:
        mavenPomFile: 'complete/pom.xml'
    - task: AzureSpringCloud@0
      inputs:
        azureSubscription: '<your service connection name>'
        Action: 'Deploy'
        AzureSpringCloud: <your Azure Spring Apps service>
        AppName: <app-name>
        DeploymentType: 'Artifacts'
        UseStagingDeployment: false
        DeploymentName: 'default'
        Package: ./target/your-result-jar.jar
    
  8. [保存および実行] を選択してジョブの完了を待ちます。

ブルーグリーン デプロイ

前のセクションで示されているデプロイでは、デプロイ時にすぐにアプリケーション トラフィックを受信します。 そのため、顧客のトラフィックをアプリケーションで受信する前に運用環境でアプリケーションをテストすることができます。

パイプライン ファイルを編集する

前述した同じ方法でアプリケーションをビルドし、それをステージング デプロイにデプロイするには、次のテンプレートを使用します。 この例では、ステージング デプロイが既に存在している必要があります。 別の方法については、ブルーグリーン デプロイの戦略に関するページを参照してください。

steps:
- task: Maven@3
  inputs:
    mavenPomFile: 'pom.xml'
- task: AzureSpringCloud@0
  inputs:
    azureSubscription: '<your service connection name>'
    Action: 'Deploy'
    AzureSpringCloud: <your Azure Spring Apps service>
    AppName: <app-name>
    DeploymentType: 'Artifacts'
    UseStagingDeployment: true
    Package: ./target/your-result-jar.jar
- task: AzureSpringCloud@0
  inputs:
    azureSubscription: '<your service connection name>'
    Action: 'Set Production'
    AzureSpringCloud: <your Azure Spring Apps service>
    AppName: <app-name>
    UseStagingDeployment: true

[リリース] セクションを使用する

以下の手順は、[リリース] セクションからブルーグリーン デプロイを有効にする方法を示しています。

  1. [パイプライン] を選択し、Maven のビルドと成果物の発行のために新しいパイプラインを作成します。

    1. コードの場所に [Azure Repos Git] を選択します。
    2. コードが置かれているリポジトリを選択します。
    3. [Maven] テンプレートを選択し、そのファイルの mavenPomFile フィールドを complete/pom.xml に設定します。
    4. 右側の [アシスタントを表示する] を選択し、[Publish build artifacts]\(ビルド成果物の発行\) テンプレートを選択します。
    5. [Path to publish]\(発行するためのパス\) を「complete/target/spring-boot-complete-0.0.1-SNAPSHOT.jar」に設定します。
    6. [保存および実行] を選択します。
  2. [リリース][リリースの作成] の順に選択します。

  3. 新しいパイプラインを追加し、[空のジョブ] を選択してジョブを作成します。

  4. [ステージ][1 job, 0 task]\(1 ジョブ、0 タスク\) という行を選択します。

    Azure DevOps のスクリーンショット。[パイプライン] タブでジョブが 1、タスク リンク 0 になっています。

    1. [+] を選択してジョブにタスクを追加します。
    2. Azure Spring Apps テンプレートを検索し、[追加] を選択してジョブにタスクを追加します。
    3. [Azure Spring Apps Deploy:]\(Azure Spring Apps デプロイ:\) を選択してタスクを編集します。
    4. このタスクに自分のアプリの情報を入力し、[Use Staging Deployment]\(ステージング デプロイを使用する\) を無効にします。
    5. [Create a new staging deployment if one does not exist]\(まだ存在しない場合は新しいステージング デプロイを作成する\) を有効にして、[デプロイ] に名前を入力します。
    6. [保存] を選択してこのタスクを保存します。
    7. [OK] を選択します。
  5. [パイプライン] を選択し、[Add an artifact]\(成果物の追加\) を選択します。

    1. [Source (build pipeline)]\(ソース (ビルド パイプライン)\) で、前に作成したパイプラインを選択します。
    2. [追加][保存] の順に選択します。
  6. [ステージ][1 job, 1 task]\(1 ジョブ、1 タスク\) を選択します。

  7. ステージ 1[Azure Spring Apps Deploy]\(Azure Spring Apps デプロイ\) タスクに移動し、[Package or folder]\(パッケージまたはフォルダー\) の横の省略記号を選択します。

  8. ダイアログの [spring-boot-complete-0.0.1-SNAPSHOT.jar] を選択し、[OK] を選択します。

    Azure DevOps のスクリーンショット。[ファイルまたはフォルダーを選択します] ダイアログ ボックスを確認できます。

  9. + を選択して、別の Azure Spring Apps タスクをジョブに追加します。

  10. アクションを [Set Production Deployment]\(運用デプロイの設定\) に変更します。

  11. [保存] を選択し、[リリースの作成] を選択すると、自動的にデプロイが開始されます。

アプリの最新のリリース状態を確認するには、[View release]\(リリースの表示\)を選択します。 このタスクが完了したら、Azure portal にアクセスしてアプリの状態を確認してください。

ソースからデプロイする

別途ビルド ステップを使用せずに直接 Azure にデプロイするには、次のパイプライン テンプレートを使用します。

- task: AzureSpringCloud@0
  inputs:
    azureSubscription: '<your service connection name>'
    Action: 'Deploy'
    AzureSpringCloud: <your Azure Spring Apps service>
    AppName: <app-name>
    DeploymentType: 'Artifacts'
    UseStagingDeployment: false
    DeploymentName: 'default'
    Package: $(Build.SourcesDirectory)

カスタム イメージからデプロイする

既存のコンテナー イメージから直接デプロイするには、次のパイプライン テンプレートを使用します。

- task: AzureSpringCloud@0
  inputs:
    azureSubscription: '<your service connection name>'
    Action: 'Deploy'
    AzureSpringCloud: '<your Azure Spring Apps service>'
    AppName: '<app-name>'
    DeploymentType: 'CustomContainer'
    UseStagingDeployment: false
    DeploymentName: 'default'
    ContainerRegistry: 'docker.io'  # or your Azure Container Registry, e.g: 'contoso.azurecr.io'
    RegistryUsername: '$(username)'
    RegistryPassword: '$(password)'
    ContainerImage: '<your image tag>'

ビルダーをデプロイして指定する (Enterprise プランのみ)

Azure Spring Apps Enterprise プランを使用している場合は、次の例に示すように、builder オプションを使用して、デプロイ アクションに使用するビルダーを指定することもできます。 詳細については、「Tanzu Build Service を使用する」を参照してください。

- task: AzureSpringCloud@0
  inputs:
    azureSubscription: '<your-service-connection-name>'
    Action: 'Deploy'
    AzureSpringCloud: '<your-Azure-Spring-Apps-service-instance-name>'
    AppName: '<app-name>'
    UseStagingDeployment: false
    DeploymentName: 'default'
    Package: './target/your-result-jar.jar'
    Builder: '<your-Tanzu-Build-Service-Builder-resource>'

次のステップ