次の方法で共有


ステージを使用してパイプラインを構築する

Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019

この記事では、複数のステージと条件を持つより複雑な YAML パイプラインを作成して実行する方法について説明します。 サンプル パイプラインには、ビルド、テスト、デプロイの各ステージが含まれます。また、代替デプロイとロールバック用のオプションのステージもあります。 複数のステージを使用すると、パイプラインのさまざまな部分を分離し、品質管理とセキュリティを向上させ、パイプラインの進行状況をより適切に把握し、リスクを軽減できます。 ロールバック ステージを使用すると、問題が発生した場合に安定したバージョンにすばやく戻すことができます。これにより、信頼性と安定性が向上します。

このコードはほとんどのシナリオで機能しますが、言語やプラットフォーム固有の要件は含まれません。 次の手順として、特定の実装ニーズに合わせてパイプラインをカスタマイズします。

前提 条件

  • リポジトリを作成できる GitHub アカウント。 無料でを作成してください。
  • Azure DevOps の組織とプロジェクト。 無料で1つ作成します
  • Microsoft がホストするエージェントでパイプラインを実行する機能。 並列ジョブを購入するか、Free レベルを要求できます。
  • YAML と Azure Pipelines に関する基本的な知識。 詳細については、「最初のパイプラインを作成する」を参照してください。

1. ビルド ステージを作成する

Build ステージで、依存関係を復元し、単体テストを実行して、コードがテストとデプロイの準備ができていることを確認します。 アプリケーションでソース コードをコンパイルする必要がある場合は、ビルド ステージでコンパイルします。

trigger:
- main

pool:
  vmImage: 'ubuntu-latest'

stages:
- stage: Build
  displayName: 'Build Stage'
  jobs:
  - job: BuildJob
    displayName: 'Build Job'
    steps:
    - script: |
        echo "Restoring project dependencies..."
      displayName: 'Restore dependencies'
    - script: |
        echo "Running unit tests..."
      displayName: 'Run unit tests'

2. テスト ステージを追加する

Test ステージはプロジェクトでテストを実行し、通常はテスト結果を Azure DevOps に発行します。 テスト結果の発行の詳細については、「テスト結果の発行」タスク を参照してください。

テスト ステージは、ビルド ステージが正常に完了し、ステージをスキップできない場合にのみ実行されます。

isSkippablefalseに設定されているため、テスト ステージをスキップするオプションは Azure DevOps UI では使用できません。

スキップできないステージのスクリーンショット。

- stage: Test
  displayName: 'Test Stage'
  dependsOn: Build
  isSkippable: false
  jobs:
  - job: TestJob
    displayName: 'Test Job'
    steps:
    - script: |
        echo "Running unit tests..."
      displayName: 'Run unit tests'

3. ステージングへのデプロイ

DeployToStaging ステージは、実行する Test ステージによって異なります。 DeployStagingJobWithValidation ジョブには手動による承認が必要です。 手動検証タスク パイプラインの実行を一時停止し、手動操作を待機します。 ユーザーは、実行を続行する前にステージを検証する必要があります。 パイプラインで手動承認を行うと、別のレベルのセキュリティが追加され、リスクが軽減され、すべての変更が適切な利害関係者によって確認されます。

手動承認用のプールは server です。 手動検証は、サーバー プールでのみ実行されます。

- stage: DeployToStaging
  displayName: 'Deploy to Staging'
  dependsOn: Test
  jobs:
  - job: DeployStagingJob
    displayName: 'Deploy to Staging Job'
    pool:
      vmImage: 'ubuntu-latest'
    steps:
    - script: |
        echo "Build staging job..."
      displayName: 'Build and deploy to staging'

  - job: DeployStagingJobWithValidation
    pool: server
    timeoutInMinutes: 4320 # job times out in 3 days
    displayName: 'Deploy to Staging Job'
    steps:
    - task: ManualValidation@1
      timeoutInMinutes: 1440 # task times out in 1 day
      inputs:
        notifyUsers: user@example.com
        instructions: 'Please validate the stage configuration and resume'
        onTimeout: 'resume'

4. 運用環境にデプロイする

DeployToProduction ステージでは、アプリケーションは運用環境にデプロイされますが、DeployToStaging ステージが成功し、ソース ブランチが main または releaseである場合に限ります。

ここで 手動検証タスクでは、セキュリティと品質管理のための 2 つ目の人間による検証手順が追加されています。 また、DeployToStaging ステージで手動検証タスクを使用しました。

- stage: DeployToProduction
  displayName: 'Deploy to Production'
  dependsOn: DeployToStaging
  lockBehavior: sequential
  condition: and(succeeded(), in(variables['Build.SourceBranch'], 'refs/heads/main', 'refs/heads/release'))
  jobs:
  - job: DeployProductionJob
    displayName: 'Deploy to Production Job'
    steps:
    - script: |
        echo "Deploying to production..."
        # Add your deployment commands here
      displayName: 'Run deployment commands'
  - job: waitForValidation
    displayName: Wait for external validation
    pool: server
    timeoutInMinutes: 4320 # job times out in 3 days
    steps:
    - task: ManualValidation@1
      timeoutInMinutes: 1440 # task times out in 1 day
      inputs:
        notifyUsers: user@example.com
        instructions: 'Please validate the build configuration and resume'
        onTimeout: 'resume'

5. オプションの代替運用およびロールバック ステージを追加する

DeployToAlternateProductionRollbackの 2 つの省略可能なステージは、手動でキューに入れられます。 DeployToAlternateProduction ステージを使用すると、プライマリ環境で障害が発生した場合に備えて、バックアップ運用環境を準備できます。 これにより、アプリケーションの全体的な信頼性と可用性が向上します。 また、ディザスター リカバリーまたはテストと検証のための代替デプロイ環境を用意することもできます。 より複雑なデプロイ戦略については、「展開ジョブのステージ、依存関係、条件の追加を参照してください。

Rollback ステージでは、デプロイ中またはデプロイ後に問題が発生した場合に、アプリケーションを以前に安定した状態に戻すセーフティ ネットが提供されます。 これは、デプロイの失敗、バグ、コンプライアンス要件、ディザスター リカバリー、またはその他の問題が原因である可能性があります。 ロールバック ステージは、アプリケーションの安定性と信頼性を維持するために不可欠です。

- stage: DeployToAlternateProduction
  displayName: 'Deploy to Alternate Production'
  condition: succeeded()
  trigger: manual
  jobs:
  - job: DeployAlternateProductionJob
    displayName: 'Deploy to Alternate Production Job'
    steps:
    - script: |
        echo "Deploying to alternate production..."
        # Add your deployment commands here
      displayName: 'Run deployment commands'

- stage: Rollback
  displayName: 'Rollback Stage'
  trigger: manual
  jobs:
  - job: RollbackJob
    displayName: 'Rollback Job'
    steps:
    - script: |
        echo "Rolling back the deployment..."
        # Add your rollback commands here
      displayName: 'Run rollback commands'

次の手順

パイプライン実行シーケンスの について説明します