Azure Pipelines 自動デプロイ(Git + ASP.NET Core)

Tech Knowledge
公開: 2024-11-13

前提条件、注意事項

  • Azure 上に「Web アプリ」が作成済みであること。
  • Azure Pipeline を無料のまま使用する場合は申請が必要
    Azure Pipelines の無料付与に対する変更
    Azure Pipelines を無料枠で使おうとして躓いた話
  • 有料枠を使用するのであれば、無料枠の申請は不要
    Azure DevOps のトップ > Organization Settings(Project Settings ではない) > Billing > MS Hosted CI/CD の Paid parrallel job を 0 → 1 に変更して Save
    (Self-Hosted CI/CD を変更しても無意味なので注意)
  • ここに記載した手順が唯一の方法ではなく、YAML の定義も一例に過ぎない。
    ソリューションの構成によっては、Pipeline がエラーになる場合もある(例:ソリューションに React プロジェクトが含まれている、複数の Web アプリがあるなど)

手順

1. Service Connection の新規作成(作成していない場合)

Project Settings > Service connections > Create Service connection
Azure Resource Manager を選択して Next ボタン
Subscription を選択
Resource group を選択
Service connection name を入力(ここでは ServiceConnection という名前にした)
他は初期値のまま Save

2. Azure Pipelines の新規作成

Pipelines > Create Pipeline > Azure Repost Git > リポジトリを選択
Configure your pipeline で ASP.NET を選択
Review your pipeline YAML にテンプレートの YAML が表示される

# 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:
- master

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)'

- task: VSTest@2
  inputs:
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

「Save and Run」を押下し、正常終了することを確認。
この状態ではビルド(とテスト)だけが行われ、デプロイは行われない。

3. デプロイ設定の追加

Pipelines から該当 Pipeline の3点リーダーをクリック > Edit

![image][pasted-2025.01.25-22.17.58.png]

コードの最後に以下のコードを追加して、Validate and Save

- task: AzureRmWebAppDeployment@4
  inputs:
    azureSubscription: 'ServiceConnection'
    package: '$(build.artifactStagingDirectory)/**/*.zip'
    WebAppName: 'webapp1'
    TakeAppOfflineFlag: true
    XmlTransformation: true
    XmlVariableSubstitution: true

Pipeline が自動的に動きはじめる(master ブランチの *.yml ファイルを更新したため)が以下のように権限要求で停止する

![image][pasted-2025.01.25-22.21.19.png]

View ボタン > Permit ボタン > 確認ダイアログで Permit ボタン
これで Pipeline が動き出す。

VSBuild を使用しない例

trigger:
- feature-web-back-end

pool:
  vmImage: 'windows-latest'

variables:
  buildConfiguration: 'Release'
  projectPath: 'webapi/webapi.csproj'

steps:
- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(projectPath)'
- task: DotNetCoreCLI@2
  inputs:
    command: 'publish'
    projects: '$(projectPath)'
    publishWebProjects: false

- task: AzureWebApp@1
  inputs:
    azureSubscription: 'Azure サブスクリプション 1(hogehoge-fuga-fuga-hoge-hogehogehoge)'
    appType: 'webApp'
    appName: 'korochin-hoge'
    package: '$(System.DefaultWorkingDirectory)/**/*.zip'
# ASP.NET Core
# Build and test ASP.NET Core projects targeting .NET Core.
# Add steps that run tests, create a NuGet package, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core

trigger:
- feature-web-back-end

resources:
  repositories:
  - repository: self
    ref: refs/heads/feature-web-back-end

pool:
  vmImage: ubuntu-latest

variables:
  buildConfiguration: 'Release'

steps:
- checkout: self

- script: dotnet build --configuration $(buildConfiguration)
  displayName: 'dotnet build $(buildConfiguration)'
  
- task: DotNetCoreCLI@2
  inputs:
    command: 'publish'
    publishWebProjects: true
    arguments: '--configuration Release --output $(System.DefaultWorkingDirectory)'
  
- task: AzureRmWebAppDeployment@4
  inputs:
    ConnectionType: 'AzureRM'
    azureSubscription: 'Azure サブスクリプション 1(hogehoge-fuga-fuga-hoge-hogehogehoge)'
    appType: 'webApp'
    WebAppName: 'korochin-hoge'
    packageForLinux: '$(System.DefaultWorkingDirectory)/**/*.zip'