次の方法で共有


パイプライン定義を作成する

azd テンプレートに CI/CD パイプライン定義ファイルが含まれていない場合は、アプリケーションのビルドとデプロイを自動化するために作成できます。 適切に構造化されたパイプライン定義には、通常、次の 4 つの主要なセクションが含まれます。

  • トリガー: 特定のブランチへのコード プッシュ、プル要求、手動トリガーなど、パイプラインを実行する必要があるイベントを指定します。 トリガーを定義すると、開発アクティビティに応じてパイプラインが自動的に実行され、継続的インテグレーションとデプロイが可能になります。

  • アクセス許可: パイプラインがリソースと安全に対話するために必要なアクセスを指定します。 たとえば、リポジトリの内容を読み取ったり、ID トークンを要求したりするアクセス許可を付与します。 適切なアクセス許可は、セキュリティで保護された正常なデプロイに不可欠です。

  • オペレーティング システムまたはプール: 特定の仮想マシン イメージ ( ubuntu-latestなど) やエージェント プールなど、パイプライン ジョブの環境を設定します。 適切な環境を選択すると、アプリケーションのビルドとデプロイの要件との互換性が確保されます。

  • 手順: コードのチェックアウト、依存関係のインストール、インフラストラクチャの構築、プロビジョニング、Azure へのデプロイなど、パイプラインが実行するタスクを一覧表示します。 エンド ツー エンドのデプロイ プロセスを自動化するには、各手順を明確に定義する必要があります。

次の例では、GitHub Actions と Azure Pipelines のパイプライン定義ファイルと関連する構成を作成する方法を示します。

GitHub Actions で azd を実行するには、次の設定を構成します。

  • id-token: writecontents: readアクセス スコープを付与します。
  • がプレインストールされている Docker イメージを使用しない限り、azdをインストールします。

パイプライン定義の開始点として、次のテンプレートを使用します。

on:
  workflow_dispatch:
  push:
    # Run when commits are pushed to mainline branch (main or master)
    # Set this to the mainline branch you are using
    branches:
      - main
      - master

# Set this permission if you are using a Federated Credential.
permissions:
  id-token: write
  contents: read

jobs:
  build:
    runs-on: ubuntu-latest
    # azd build-in variables.
    # This variables are always set by `azd pipeline config`
    # You can set them as global env (apply to all steps) or you can add them to individual steps' environment
    env:
      AZURE_CLIENT_ID: ${{ vars.AZURE_CLIENT_ID }}
      AZURE_TENANT_ID: ${{ vars.AZURE_TENANT_ID }}
      AZURE_SUBSCRIPTION_ID: ${{ vars.AZURE_SUBSCRIPTION_ID }}
      AZURE_ENV_NAME: ${{ vars.AZURE_ENV_NAME }}
      AZURE_LOCATION: ${{ vars.AZURE_LOCATION }}
      ## Define the additional variables or secrets that are required globally (provision and deploy)
      # ADDITIONAL_VARIABLE_PLACEHOLDER: ${{ variables.ADDITIONAL_VARIABLE_PLACEHOLDER }}
      # ADDITIONAL_SECRET_PLACEHOLDER: ${{ secrets.ADDITIONAL_SECRET_PLACEHOLDER }}      
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      # using the install-azd action
      - name: Install azd
        uses: Azure/setup-azd@v1.0.0

      # # If you want to use azd-daily build, or install it from a PR, you can remove previous step and
      # # use the next one:
      # - name: Install azd - daily or from PR
      #  # Update this scrip based on the OS - pool of your pipeline. This example is for a linux pipeline installing daily build
      #  run: curl -fsSL https://aka.ms/install-azd.sh | bash -s -- --version daily
      #  shell: pwsh

      # azd set up Federated Credential by default. You can remove this step if you are using Client Credentials
      - name: Log in with Azure (Federated Credentials)
        if: ${{ env.AZURE_CLIENT_ID != '' }}
        run: |
          azd auth login `
            --client-id "$Env:AZURE_CLIENT_ID" `
            --federated-credential-provider "github" `
            --tenant-id "$Env:AZURE_TENANT_ID"
        shell: pwsh

      ## If you set up your pipeline with Client Credentials, remove previous step and uncomment this one
      # - name: Log in with Azure (Client Credentials)
      #   if: ${{ env.AZURE_CREDENTIALS != '' }}
      #   run: |
      #     $info = $Env:AZURE_CREDENTIALS | ConvertFrom-Json -AsHashtable;
      #     Write-Host "::add-mask::$($info.clientSecret)"

      #     azd auth login `
      #       --client-id "$($info.clientId)" `
      #       --client-secret "$($info.clientSecret)" `
      #       --tenant-id "$($info.tenantId)"
      #   shell: pwsh
      #   env:
      #     AZURE_CREDENTIALS: ${{ secrets.AZURE_CREDENTIALS }}

      - name: Provision Infrastructure
        run: azd provision --no-prompt
        env:
         #  # uncomment this if you are using infrastructure parameters
         #  AZD_INITIAL_ENVIRONMENT_CONFIG: ${{ secrets.AZD_INITIAL_ENVIRONMENT_CONFIG }}
         ## Define the additional variables or secrets that are required only for provision 
         #  ADDITIONAL_VARIABLE_PLACEHOLDER: ${{ variables.ADDITIONAL_VARIABLE_PLACEHOLDER }}
         #  ADDITIONAL_SECRET_PLACEHOLDER: ${{ secrets.ADDITIONAL_SECRET_PLACEHOLDER }}

      - name: Deploy Application
        run: azd deploy --no-prompt
        env:
         ## Define the additional variables or secrets that are required only for deploy
         #  ADDITIONAL_VARIABLE_PLACEHOLDER: ${{ variables.ADDITIONAL_VARIABLE_PLACEHOLDER }}
         #  ADDITIONAL_SECRET_PLACEHOLDER: ${{ secrets.ADDITIONAL_SECRET_PLACEHOLDER }}