Azure DevOps Services |Azure DevOps Server |Azure DevOps Server 2022 |Azure DevOps Server 2020
この記事では、Azure Pipelines を使用して .NET Framework プロジェクトをビルドする方法について説明します。 .NET Core プロジェクトについては、「 Build、テスト、および .NET Core アプリのデプロイ」を参照。
Azure DevOps プロジェクトの作成
- Azure DevOps 組織またはコレクションで、 [新しいプロジェクト または Create プロジェクトを選択します。
- プロジェクト名を入力します。
- プロジェクトの [可視性] を選択します。
- [作成] を選択します
サンプル アプリを入手する
サンプル アプリは、.NET 4.8 を使用する Visual Studio ソリューションです。 アプリを取得するには、次の GitHub リポジトリをフォークします。
https://github.com/Azure-Samples/app-service-web-dotnet-get-started
パイプラインを作成してビルドする
独自のリポジトリにサンプル コードを作成したら、「 最初のパイプラインを作成するの手順を使用して、Azure DevOps プロジェクトにパイプラインを作成します。
ASP.NET テンプレートを選択します。 この選択により、コードのビルドに必要なタスクを含む azure-pipelines.yml ファイルがサンプル リポジトリに自動的に追加されます。 テンプレートには、テストを実行するVSTest@3 タスクが含まれています。 サンプル リポジトリにはテストが含まれていないため、パイプラインからVSTest@3 タスクを削除できます。
パイプラインは次の例のようになります。
# ASP.NET
# Build and test ASP.NET projects.
# Add steps that publish symbols, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/apps/aspnet/build-aspnet-4
trigger:
- main
pool:
  vmImage: 'windows-latest'
variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'
steps:
- task: NuGetToolInstaller@1
- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'
- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
# ASP.NET
# Build and test ASP.NET projects.
# Add steps that publish symbols, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/apps/aspnet/build-aspnet-4
trigger:
- main
pool: 
  name: default
variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'
steps:
- task: NuGetToolInstaller@1
- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'
- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
保存して実行を選択し、Jobs を選択して、パイプラインの動作を確認します。
ビルド成果物を発行するには、YAML ファイルの末尾に次のタスクを追加します。
- task: PublishPipelineArtifact@1
  inputs:
    targetPath: '$(Pipeline.Workspace)'
    artifact: 'myartifact'
    publishLocation: 'pipeline'
- task: PublishBuildArtifacts@1
  inputs:
    pathToPublish: '$(Build.ArtifactStagingDirectory)'
    artifactName: drop
ビルド環境
Azure Pipelines を使って .NET Framework プロジェクトをビルドでき、独自のインフラストラクチャをセットアップする必要はありません。 Azure Pipelines の Microsoft でホストされるエージェント には、プロジェクトのビルドに役立ついくつかのリリースバージョンの Visual Studio がプレインストールされています。 Windows Server 2022 と Visual Studio 2022 の windows-2022 を使用します。
セルフホステッド エージェントを使ってビルドを実行することもできます。 セルフホステッド エージェントの使用は、大規模なリポジトリがあり、ビルドごとに新しいマシンにソース コードをダウンロードしないようにする場合に役立ちます。
ビルドはセルフホステッド エージェントで実行されます。 エージェントに必要なバージョンの Visual Studio がインストールされていることを確認します。
複数の構成のビルド
複数の構成でアプリをビルドすることが必要な場合があります。 次の手順では、 Debug, x86、 Debug, x64、 Release, x86、 Release, x64の 4 つの構成でサンプル アプリをビルドします。
- パイプライン UI で Variables タブを選択し、次の変数を変更します。 - BuildConfiguration=- debug, release
- BuildPlatform=- x86, x64
 
- Tasksを選択しagent jobを選択してジョブの次のオプションを変更します。 - [複数構成] を選びます。
- 
              Multipliers:を指定する BuildConfiguration, BuildPlatform
 
- 複数のビルド エージェントがあり、構成とプラットフォームのペアを並列でビルドしたい場合は、[並列] を選びます。 
依存関係を復元する
NuGet タスクを使うと、NuGet パッケージの依存関係をインストールおよび更新できます。 NuGet タスクを使用して、Azure Artifacts、NuGet.org、またはその他の外部または内部の NuGet リポジトリから NuGet パッケージをダウンロードすることもできます。
次の例では、同じ組織内のプロジェクト スコープ フィードからソリューションを復元します。
- task: NuGetCommand@2
  inputs:
    command: 'restore'
    feedsToUse: 'select'
    vstsFeed: 'my-project/my-project-scoped-feed'
    includeNuGetOrg: false
    restoreSolution: '**/*.sln'
メモ
Ubuntu 24.04 以降を使用している場合は、NuGetAuthenticate タスクではなく、.NET CLI で NuGetCommand@2 タスクを使用する必要があります。 詳細については、Ubuntu でホストされる新しいイメージ のサポートに関するページを参照してください。