iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 12
0
DevOps

.NET Core 專案持續整合與部署系列 第 12

Integrate:Publish Mode

  • 分享至 

  • xImage
  •  

.NET Core 在發佈應用程式主要有三種模式:

  • 相依 Framework 的部署:framework-dependent deployment, FDD
  • 自封式部署:self-contained deployment, SCD
  • 架構相依可執行檔:Framework-dependent executables, FDE

其中常見的以前兩者為主,今天也就先簡單介紹這兩種模式以及如何透過 dotnet publish 進行發布。

為了方便示例,先建立一個 console 專案,並且透過 dotnet run 去執行看預期結果:

$ dotnet new console -o CLIProj
The template "Console Application" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on CLIProj/CLIProj.csproj...
  Restore completed in 176.45 ms for /Users/fntsr/Projects/Ironman/dotnet-sln/CLIProj/CLIProj.csproj.

Restore succeeded.

$ cd CLIProj/
$ dotnet run
Hello World!

接著我們嘗試使用 dotnet publish 並且不搭配任何參數,通常這樣的發佈模式預設會是 FDD:

$ dotnet publish
Microsoft (R) Build Engine version 16.2.32702+c4012a063 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 29.46 ms for /Users/fntsr/Projects/Ironman/dotnet-sln/CLIProj/CLIProj.csproj.
  CLIProj -> /Users/fntsr/Projects/Ironman/dotnet-sln/CLIProj/bin/Debug/netcoreapp2.2/CLIProj.dll
  CLIProj -> /Users/fntsr/Projects/Ironman/dotnet-sln/CLIProj/bin/Debug/netcoreapp2.2/publish/

此時我們可以透過 dotnet 指令去執行發佈出來的 dll 檔案:

$ dotnet ./bin/Debug/netcoreapp2.2/publish/CLIProj.dll
Hello World!

但若我們直接想嘗試直接執行話,就會出錯:

$ ./bin/release/netcoreapp2.2/publish/CLIProj.dll
fish: The file './bin/release/netcoreapp2.2/publish/CLIProj.dll' is not executable by this user

這就是 FDD 模式,如其名,是相依 Framework。

若是我們要發布一個可以執行的程式又該如何呢?這時就可使用 SCD 了!若我們在發布參數中透過 --runtime 指定了執行階段,就會預設開啟 SCD 模式:

$ dotnet publish -c release -r osx-x64
Microsoft (R) Build Engine version 16.2.32702+c4012a063 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 242.13 ms for /Users/fntsr/Projects/Ironman/dotnet-sln/CLIProj/CLIProj.csproj.
  CLIProj -> /Users/fntsr/Projects/Ironman/dotnet-sln/CLIProj/bin/release/netcoreapp2.2/osx-x64/CLIProj.dll
  CLIProj -> /Users/fntsr/Projects/Ironman/dotnet-sln/CLIProj/bin/release/netcoreapp2.2/osx-x64/publish/

這時就可以直接執行了!

$ ./bin/release/netcoreapp2.2/osx-x64/publish/CLIProj
Hello World!

上一篇
Integrate:Environments
下一篇
Integrate:Version Number
系列文
.NET Core 專案持續整合與部署31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言