GitHub Actions で Azure Spring Apps の CI/CD を使用する

Note

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

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

この記事では、GitHub Actions を使用して Azure Spring Apps の CI/CD ワークフローを構築する方法について説明します。

GitHub Actions は、自動化されたソフトウェア開発ライフサイクル ワークフローをサポートします。 Azure Spring Apps 用の GitHub Actions を使用すると、リポジトリに構築、テスト、パッケージ化、リリース、および Azure へのデプロイを行うワークフローを作成できます。

前提条件

この例には Azure CLI が必要です。

GitHub リポジトリを設定して認証する

Azure ログイン アクションを承認するには、Azure サービス プリンシパルの資格情報が必要です。 Azure の資格情報を取得するには、ローカル マシン上で次のコマンドを実行します。

az login
az ad sp create-for-rbac \
    --role contributor \
    --scopes /subscriptions/<SUBSCRIPTION_ID> \
    --json-auth

特定のリソース グループにアクセスするには、範囲を縮小できます。

az ad sp create-for-rbac \
    --role contributor \
    --scopes /subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP> \
    --json-auth

このコマンドからは、次のような JSON オブジェクトが出力されます。

{
    "clientId": "<GUID>",
    "clientSecret": "<GUID>",
    "subscriptionId": "<GUID>",
    "tenantId": "<GUID>",
    ...
}

この例では、GitHub 上の steeltoe サンプルを使用しています。 リポジトリをフォークし、フォークの GitHub リポジトリ ページを開いて、[設定] タブを選択します。[シークレット] メニューを開き、[新しいシークレット] を選択します。

Screenshot of the GitHub Actions secrets and variables page with the New repository secret button highlighted.

シークレット名を AZURE_CREDENTIALS に設定し、その値を「Set up your GitHub repository and authenticate (GitHub リポジトリを設定して認証する)」という見出しの下にある JSON 文字列に設定します。

Screenshot of the GitHub Actions secrets / New secret page.

GitHub Actions で Key Vault を使用して Azure Spring を認証する方法の記事で説明されているように、GitHub Actions で Key Vault から Azure のログイン資格情報を取得することもできます。

サービス インスタンスをプロビジョニングする

Azure Spring Apps サービス インスタンスをプロビジョニングするには、Azure CLI を使用して次のコマンドを実行します。

az extension add --name spring
az group create \
    --name <resource-group-name> \
    --location eastus 
az spring create \
    --resource-group <resource-group-name> \
    --name <service-instance-name> 
az spring config-server git set \
    --name <service-instance-name> \
    --uri https://github.com/Azure-Samples/azure-spring-apps-samples \
    --label main \
    --search-paths steeltoe-sample/config

ワークフローを構築する

ワークフローは、次のオプションを使用して定義されます。

Azure CLI を使用してデプロイを準備する

コマンド az spring app create は現在べき等ではありません。 一度実行した後、同じコマンドを再度実行するとエラーが発生します。 既存の Azure Spring Apps アプリおよびインスタンスにはこのワークフローをお勧めします。

準備には、次の Azure CLI コマンドを使用します。

az config set defaults.group=<service-group-name>
az config set defaults.spring=<service-instance-name>
az spring app create --name planet-weather-provider
az spring app create --name solar-system-weather

Azure CLI を使用して直接デプロイする

次の内容の .github/workflows/main.yml ファイルをリポジトリに作成します。 <リソース グループ名> と <サービス名> を正しい値に置き換えます。

name: Steeltoe-CD

# Controls when the action runs. Triggers the workflow on push or pull request
# events but only for the main branch
on:
  push:
    branches: [ main]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job runs on
    runs-on: ubuntu-latest
    env:
      working-directory: ./steeltoe-sample
      resource-group-name: <your resource group name>
      service-name: <your service name>

    # Supported .NET Core version matrix.
    strategy:
      matrix:
        dotnet: [ '3.1.x' ]

    # Steps represent a sequence of tasks that is executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2

      # Set up .NET Core 3.1 SDK
      - uses: actions/setup-dotnet@v1
        with:
          dotnet-version: ${{ matrix.dotnet }}

      # Set credential for az login
      - uses: azure/login@v1.1
        with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}

      - name: install Azure CLI extension
        run: |
          az extension add --name spring --yes

      - name: Build and package planet-weather-provider app
        working-directory: ${{env.working-directory}}/src/planet-weather-provider
        run: |
          dotnet publish
          az spring app deploy -n planet-weather-provider --runtime-version NetCore_31 --main-entry Microsoft.Azure.SpringCloud.Sample.PlanetWeatherProvider.dll --artifact-path ./publish-deploy-planet.zip -s ${{ env.service-name }} -g ${{ env.resource-group-name }}
      - name: Build solar-system-weather app
        working-directory: ${{env.working-directory}}/src/solar-system-weather
        run: |
          dotnet publish
          az spring app deploy -n solar-system-weather --runtime-version NetCore_31 --main-entry Microsoft.Azure.SpringCloud.Sample.SolarSystemWeather.dll --artifact-path ./publish-deploy-solar.zip -s ${{ env.service-name }} -g ${{ env.resource-group-name }}

GitHub リポジトリを設定して認証する

Azure ログイン アクションを承認するには、Azure サービス プリンシパルの資格情報が必要です。 Azure の資格情報を取得するには、ローカル マシン上で次のコマンドを実行します。

az login
az ad sp create-for-rbac \
    --role contributor \
    --scopes /subscriptions/<SUBSCRIPTION_ID> \
    --json-auth

特定のリソース グループにアクセスするには、範囲を縮小できます。

az ad sp create-for-rbac \
    --role contributor \
    --scopes /subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP> \
    --json-auth

このコマンドからは、次のような JSON オブジェクトが出力されます。

{
    "clientId": "<GUID>",
    "clientSecret": "<GUID>",
    "subscriptionId": "<GUID>",
    "tenantId": "<GUID>",
    ...
}

この例では、GitHub 上の PiggyMetrics サンプルを使用します。 サンプルをフォークし、[Azure ブランチのみをコピーする] チェック ボックスを外し、GitHub リポジトリ ページを開き、[設定] タブを選択します。[シークレット] メニューを開き、[新しいシークレットを追加] を選択します。

Screenshot of the GitHub Actions secrets and variables page with the New repository secret button highlighted.

シークレット名を AZURE_CREDENTIALS に設定し、その値を「Set up your GitHub repository and authenticate (GitHub リポジトリを設定して認証する)」という見出しの下にある JSON 文字列に設定します。

Screenshot of the GitHub Actions secrets / New secret page.

GitHub Actions で Key Vault を使用して Azure Spring を認証する方法の記事で説明されているように、GitHub Actions で Key Vault から Azure のログイン資格情報を取得することもできます。

サービス インスタンスをプロビジョニングする

Azure Spring Apps サービス インスタンスをプロビジョニングするには、Azure CLI を使用して次のコマンドを実行します。

az extension add --name spring
az group create --location eastus --name <resource group name>
az spring create -n <service instance name> -g <resource group name>
az spring config-server git set -n <service instance name> --uri https://github.com/xxx/piggymetrics --label config

エンドツーエンドのサンプル ワークフロー

次の例は、一般的な使用シナリオを示しています。

デプロイ中

以降のセクションでは、アプリをデプロイするためのさまざまな方法を紹介します。

運用環境

Azure Spring Apps は、ビルドされた成果物 (たとえば、JAR や .NET Core ZIP など) またはソース コード アーカイブを使用したデプロイへの展開をサポートしています。

次の例では、Maven によってビルドされた JAR ファイルを使用している Azure Spring Apps の既定の実稼働デプロイに展開します。 この例は、Basic SKU を使用する場合に考えられる唯一のデプロイ シナリオです。

Note

パッケージ検索パターンは、1 つのパッケージのみを返す必要があります。 ビルド タスクで sources.jarjavadoc.jar などの複数の JAR パッケージが生成される場合は、アプリケーションのバイナリ成果物にのみ一致するように検索パターンを絞り込む必要があります。

name: AzureSpringApps
on: push
env:
  ASC_PACKAGE_PATH: ${{ github.workspace }}
  AZURE_SUBSCRIPTION: <azure subscription name>

jobs:
  deploy_to_production:
    runs-on: ubuntu-latest
    name: deploy to production with artifact
    steps:
      - name: Checkout GitHub Action
        uses: actions/checkout@v2

      - name: Set up Java 11
        uses: actions/setup-java@v3
        with:
          distribution: 'temurin'
          java-version: '11'

      - name: maven build, clean
        run: |
          mvn clean package

      - name: Login via Azure CLI
        uses: azure/login@v1
        with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}

      - name: deploy to production with artifact
        uses: azure/spring-apps-deploy@v1
        with:
          azure-subscription: ${{ env.AZURE_SUBSCRIPTION }}
          action: Deploy
          service-name: <service instance name>
          app-name: <app name>
          use-staging-deployment: false
          package: ${{ env.ASC_PACKAGE_PATH }}/**/*.jar

次の例では、ソース コードを使用している Azure Spring Apps の既定の実稼働デプロイに展開します。

name: AzureSpringApps
on: push
env:
  ASC_PACKAGE_PATH: ${{ github.workspace }}
  AZURE_SUBSCRIPTION: <azure subscription name>

jobs:
  deploy_to_production:
    runs-on: ubuntu-latest
    name: deploy to production with source code
    steps:
      - name: Checkout GitHub Action
        uses: actions/checkout@v2

      - name: Login via Azure CLI
        uses: azure/login@v1
        with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}

      - name: deploy to production step with source code
        uses: azure/spring-apps-deploy@v1
        with:
          azure-subscription: ${{ env.AZURE_SUBSCRIPTION }}
          action: deploy
          service-name: <service instance name>
          app-name: <app name>
          use-staging-deployment: false
          package: ${{ env.ASC_PACKAGE_PATH }}

次の例では、Enterprise プランのソース コードを使用している Azure Spring Apps の既定の実稼働デプロイに展開します。 builder オプションを使用して、デプロイ アクションに使用するビルダーを指定できます。

name: AzureSpringApps
on: push
env:
  ASC_PACKAGE_PATH: ${{ github.workspace }}
  AZURE_SUBSCRIPTION: <azure subscription name>

jobs:
  deploy_to_production:
    runs-on: ubuntu-latest
    name: deploy to production with source code
    steps:
      - name: Checkout GitHub Action
        uses: actions/checkout@v2

      - name: Login via Azure CLI
        uses: azure/login@v1
        with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}

      - name: deploy to production step with source code in the Enterprise plan
        uses: azure/spring-apps-deploy@v1
        with:
          azure-subscription: ${{ env.AZURE_SUBSCRIPTION }}
          action: deploy
          service-name: <service instance name>
          app-name: <app name>
          use-staging-deployment: false
          package: ${{ env.ASC_PACKAGE_PATH }}
          builder: <builder>

次の例では、既存のコンテナー イメージを使用して Azure Spring Apps 内の既定の運用環境デプロイにデプロイします。

name: AzureSpringApps
on: push
env:
  ASC_PACKAGE_PATH: ${{ github.workspace }}
  AZURE_SUBSCRIPTION: <azure subscription name>

jobs:
  deploy_to_production:
    runs-on: ubuntu-latest
    name: deploy to production with source code
    steps:
      - name: Checkout GitHub Action
        uses: actions/checkout@v2

      - name: Login via Azure CLI
        uses: azure/login@v1
        with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}

      - name: Deploy Custom Image
        uses: Azure/spring-apps-deploy@v1
        with:
          azure-subscription: ${{ env.AZURE_SUBSCRIPTION }}
          action: deploy
          service-name: <service instance name>
          app-name: <app name>
          deployment-name: <deployment name>
          container-registry: <your container image registry>
          registry-username: ${{ env.REGISTRY_USERNAME }}
          registry-password: ${{ secrets.REGISTRY_PASSWORD }}
          container-image: <your image tag>

デプロイ中に、より多くの引数を使用することで、より多くの機能を実現できます。 詳細については、「Azure Spring Apps にデプロイするための GitHub アクション引数」セクションを参照してください。

ブルーグリーン

次の例では、既存のステージング デプロイに展開します。 このデプロイは、実稼働デプロイとして設定されるまで、実稼働トラフィックを受信しません。 use-staging-deployment true を設定して、ステージング デプロイを自動的に検索するか、特定のデプロイ名を割り当てるだけでできます。 この記事の残りの部分では、spring-apps-deploy アクションにのみ焦点を当て、準備ジョブは省きます。

# environment preparation configurations omitted
    steps:
      - name: blue green deploy step use-staging-deployment
        uses: azure/spring-apps-deploy@v1
        with:
          azure-subscription: ${{ env.AZURE_SUBSCRIPTION }}
          action: deploy
          service-name: <service instance name>
          app-name: <app name>
          use-staging-deployment: true
          package: ${{ env.ASC_PACKAGE_PATH }}/**/*.jar
# environment preparation configurations omitted
    steps:
      - name: blue green deploy step with deployment-name
        uses: azure/spring-apps-deploy@v1
        with:
          azure-subscription: ${{ env.AZURE_SUBSCRIPTION }}
          action: deploy
          service-name: <service instance name>
          app-name: <app name>
          deployment-name: staging
          package: ${{ env.ASC_PACKAGE_PATH }}/**/*.jar

代替アプローチなど、ブルーグリーン デプロイの詳細については、「ブルーグリーン デプロイ戦略」を参照してください。

新規運用環境の設定

次の例では、現在のステージング デプロイを運用として設定し、運用トラフィックを受信するデプロイを効果的にスワップします。

# environment preparation configurations omitted
    steps:
      - name: set production deployment step
        uses: azure/spring-apps-deploy@v1
        with:
          azure-subscription: ${{ env.AZURE_SUBSCRIPTION }}
          action: set-production
          service-name: <service instance name>
          app-name: <app name>
          use-staging-deployment: true

ステージング環境のデプロイを削除する

Delete Staging Deployment アクションを使用すると、運用トラフィックを受信していないデプロイを削除できます。 この削除により、そのデプロイで使用されるリソースが解放され、新しいステージング デプロイのための領域が作り出されます:

# environment preparation configurations omitted
    steps:
      - name: Delete staging deployment step
        uses: azure/spring-apps-deploy@v1
        with:
          azure-subscription: ${{ env.AZURE_SUBSCRIPTION }}
          action: delete-staging-deployment
          service-name: <service instance name>
          app-name: <app name>

ビルドの作成または更新 (Enterprise プランのみ)

次の例では、Enterprise プランでビルド リソースを作成または更新します。

# environment preparation configurations omitted
    steps:
      - name: Create or update build
        uses: azure/spring-apps-deploy@v1
        with:
          azure-subscription: ${{ env.AZURE_SUBSCRIPTION }}
          action: build
          service-name: <service instance name>
          build-name: <build name>
          package: ${{ env.ASC_PACKAGE_PATH }}
          builder: <builder>

ビルドの削除 (Enterprise プランのみ)

次の例では、Enterprise プランのビルド リソースを削除します。

# environment preparation configurations omitted
    steps:
      - name: Delete build
        uses: azure/spring-apps-deploy@v1
        with:
          azure-subscription: ${{ env.AZURE_SUBSCRIPTION }}
          action: delete-build
          service-name: <service instance name>
          build-name: <build name>

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

もう 1 つの方法は、Jar のデプロイとアプリ設定の更新に Maven プラグインを使用することです。 コマンド mvn azure-spring-apps:deploy はべき等であり、必要に応じて自動的にアプリを作成します。 対応するアプリを事前に作成する必要はありません。

name: AzureSpringApps
on: push

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:

    - uses: actions/checkout@main

    - name: Set up Java 11
      uses: actions/setup-java@v3
      with:
        distribution: 'temurin'
        java-version: '11'

    - name: maven build, clean
      run: |
        mvn clean package -DskipTests

    # Maven plugin can cosume this authentication method automatically
    - name: Azure Login
      uses: azure/login@v1
      with:
        creds: ${{ secrets.AZURE_CREDENTIALS }}

    # Maven deploy, make sure you have correct configurations in your pom.xml
    - name: deploy to Azure Spring Apps using Maven
      run: |
        mvn azure-spring-apps:deploy

ワークフローを実行する

.github/workflow/main.yml を GitHub にプッシュすると、GitHub Actions が自動的に有効になります。 このアクションは、新しいコミットがプッシュされたときにトリガーされます。 ブラウザーでこのファイルを作成する場合、アクションが既に実行されている必要があります。

アクションが有効になっていることを確認するには、GitHub リポジトリ ページの [Actions]\(アクション\) タブを選択します。

Screenshot of the GitHub Actions tab showing the All workflows section.

アクションの実行結果がエラーになる場合、たとえば、Azure の資格情報を設定していなかった場合は、エラーを修正した後にチェックを再実行できます。 GitHub リポジトリ ページの [Actions]\(アクション\) を選択し、特定のワークフロー タスクを選択し、[Rerun checks]\(チェックの再実行\) ボタンを選択してチェックを再実行します。

Screenshot of the GitHub Actions tab with the Re-run checks button highlighted.

次のステップ