LoginSignup
1
2

More than 1 year has passed since last update.

.NET Webアプリのビルド(Web Deploy Packageの作成)とデプロイ(配置)

Last updated at Posted at 2023-01-18

概要

  • .NET Webアプリのビルド(Web Deploy Packageの作成)、およびサーバーでのデプロイ(IISへの配置)手順を記載する
  • 本番環境がAzure DevOps Serverとは別環境の場合を想定し、Azure DevOps Serverからのデプロイではなく手作業での手順としている

自動化

  • 本手順では次は行っていないが可能であれば、上記手順に従い行ったほうが良い
    • Azure DevOps Serverでのビルド(Web Deploy Packageの作成)
    • Azure DevOps Serverでのデプロイの自動化

検証した環境

ビルド環境

  • Visual Studio 2022
  • .NET Fx 4.8

デプロイ環境

  • Windows Server 2019
  • IIS 10.X

手順

  • プロジェクトファイル名:SampleWeb.csproj

(1)【ビルド環境】パッケージの作成(ビルド)

1. コマンドラインから.NET Webアプリをビルド(Web Deploy Packageの作成)

  1. コマンドプロンプトを開く

  2. .NET Webアプリのプロジェクトファイルがあるフォルダーに移動

    cd <.NET Webアプリのプロジェクトファイルがあるフォルダー>
    
  3. 次のコマンドを実行

    %comspec% /k "C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\Tools\VsDevCmd.bat"
    msbuild SampleWeb.csproj /p:Configuration=Release /p:Platform=AnyCPU /p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:PackageLocation=".\publish\web.zip"
    
    • %comspec% /k "C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\Tools\VsDevCmd.bat"
      • Visual Studio 2022のDeveloper Command Promptの内容
    • msbuild SampleWeb.csproj /p:Configuration=Release /p:Platform=AnyCPU /p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:PackageLocation=".\publish\web.zip"
      • SampleWebをReleaseビルドし、publishというフォルダーにWeb Package 形式でweb.zipというファイル名で発行する

2. 作成されたpublishフォルダーをサーバーへのデプロイに利用する

(1’)【ビルド環境】パッケージの作成(ビルド) Azure DevOps (Server)版

手作業でなくAzure DevOps (Server)で実施時のビルド定義を記載(抜粋)

variables:
  webProject: 'src\SampleSolution\SampleWeb\SampleWeb.csproj'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'
  copyTargetProjectsWeb: '?(SampleWeb)'

# Webをビルド(Web Deploy Package作成)
# Projectをビルドするので、MSBuildタスクを利用する
# https://learn.microsoft.com/ja-jp/azure/devops/pipelines/tasks/reference/msbuild-v1?view=azure-pipelines
# platformは引数指定でないとエラーになることがある
# https://bartwullems.blogspot.com/2021/05/azure-devops-pipelines-outputpath.html

- task: MSBuild@1
  inputs:
    solution: '$(webProject)'
    configuration: '$(buildConfiguration)'
    msbuildArguments: '/p:Platform=AnyCPU /p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:PackageLocation=".\publish\web.zip"'

# 1つのフォルダにまとめる(SampleWeb)
- task: CopyFiles@2
  displayName: 'ファイルのコピー先: $(build.artifactstagingdirectory)'
  inputs:
    SourceFolder: '$(system.defaultworkingdirectory)'
    Contents: 'src\SampleSolution\$(copyTargetProjectsWeb)\publish\*'
    OverWrite: true
    flattenFolders: true
    TargetFolder: '$(build.artifactstagingdirectory)\SampleWeb\publish'
  condition: succeededOrFailed()

(2)【デプロイ環境】サーバーへのデプロイ(配置)

1. (初回のみ)Webサーバーに必要なパッケージ Web Deploy 3.6 をインストール

  1. 次からダウンロード

    https://www.iis.net/downloads/microsoft/web-deploy

  2. IISのバージョンが適合していることを確認

  3. 画面に従い標準でインストール

2. 「(1)パッケージの作成(ビルド)」で作成したpublishフォルダーをサーバーにコピー(デスクトップ等)

3. 設定を変更する

  1. publish\web.SetParameters.xmlを編集する
    IIS Web Application Name Webアプリケーションの名称を編集する。 Default Web Site/SampleWeb_deploySampleWeb_deployの部分
    ~Connection String 接続先文字列を編集可能

4. コマンドラインからpublishの内容をIISに配置する

  1. コマンドプロンプトを開く

  2. web.deploy.cmdがあるフォルダーに移動

    cd <web.deploy.cmdのフォルダー>
    
  3. テストで更新されるファイル、削除されるファイルを確認

    web.deploy.cmd  /T /M: /U: /P:
    
  4. 問題なければ実行するとIISに配置される

    webdeploy.cmd  /Y /M: /U: /P:
    

参考

https://atmarkit.itmedia.co.jp/ait/articles/1311/15/news096_3.html
https://learn.microsoft.com/en-us/aspnet/web-forms/overview/deployment/web-deployment-in-the-enterprise/building-and-packaging-web-application-projects
https://www.iis.net/downloads/microsoft/web-deploy

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2