只有累積,沒有奇蹟

2019年2月7日 星期四

[Nuget] 架設公司內部 Nuget Server

前言 
在開發上常常會遇到某些代碼是跨專案共用的,在 .NET 中會將重複使用的代碼打包成「套件」,其中包含了編譯後的 dll 以及相關的檔案資訊,接著將套件內容上傳到 Nuget Server 上提供開發者下載使用,Nuget server 有分共用與私人主機兩種,共用的 nuget server 上有相當多好用的 libary 供開發者下載,例如常用的 NLog、newtonsoft.json、NUnit...等各種好用的 package,從官方得知截至目前為止共用的 nuget server 有 14 萬 package 供開發者使用,其全部 package 總下載量達到 150 億次,對細節有興趣可以參考 An introduction to NuGet 官方介紹這裡不在多加介紹。有些團隊常用的 Library 是公司內使用不方便上傳到共用的 nuget server 上面,今天就來介紹如何架設私有的 Nuget Server。

安裝步驟
要架設私有的 Nuget Server 可以透過共用的 nuget server 下載 nuget.server 套件,安裝後進行上傳資訊的設定,例如 上傳的api key、目錄位置..等資訊,詳細步驟說明如下
下載 nuget.server package
Step 1 : 在 Visual Studio 開啟新專案 > 選擇 Web > ASP.Net Web Application > 輸入 Project Name > OK
Step 2 : 選擇空白專案 > OK
Step 3 : 下載 Nuget.Server > 到 Visual Studio 右上角 輸入 nuget > 選擇 Manage Nuget Package
Step 4 : 輸入 nuget.server > 點擊 nuget.server 套件 > 下載  
Step 5 : 會提醒要安裝的資訊與相關套件 > 沒問題就按下 OK
 
透過以上步驟,完成下載 nuget.server的動作 

設定 nuget server 資訊
在安裝 nuget.server 過程中會在專案的 web.config 加上 nuget server 相關設定資訊,內容如下
  1. <staticContent>
  2. <mimeMap fileExtension=".nupkg" mimeType="application/zip" />
  3. </staticContent><modules runAllManagedModulesForAllRequests="true">
  4. <remove name="WebDAVModule" />
  5. </modules><security>
  6. <requestFiltering>
  7. <!-- maxAllowedContentLength is specified in Bytes -->
  8. <requestLimits maxAllowedContentLength="31457280" />
  9. </requestFiltering>
  10. </security></system.webServer>
  11. <appSettings>
  12. <!--
  13. Determines if an Api Key is required to push\delete packages from the server.
  14. -->
  15. <add key="requireApiKey" value="true" />
  16.  
  17. <!--
  18. Set the value here to allow people to push/delete packages from the server.
  19. NOTE: This is a shared key (password) for all users.
  20. -->
  21. <add key="apiKey" value="123456" />
  22.  
  23. <!--
  24. Change the path to the packages folder. Default is ~/Packages.
  25. This can be a virtual or physical path.
  26. -->
  27. <add key="packagesPath" value="" />
  28.  
  29. <!--
  30. Change the name of the internal cache file. Default is machine name (System.Environment.MachineName).
  31. This is the name of the cache file in the packages folder. No paths allowed.
  32. -->
  33. <add key="cacheFileName" value="" />
  34.  
  35. <!--
  36. Set allowOverrideExistingPackageOnPush to false to mimic NuGet.org's behaviour (do not allow overwriting packages with same id + version).
  37. -->
  38. <add key="allowOverrideExistingPackageOnPush" value="false" />
  39.  
  40. <!--
  41. Set ignoreSymbolsPackages to true to filter out symbols packages. Since NuGet.Server does not come with a symbol server,
  42. it makes sense to ignore this type of packages. When enabled, files named `.symbols.nupkg` or packages containing a `/src` folder will be ignored.
  43. If you only push .symbols.nupkg packages, set this to false so that packages can be uploaded.
  44. -->
  45. <add key="ignoreSymbolsPackages" value="true" />
  46.  
  47. <!--
  48. Set enableDelisting to true to enable delist instead of delete as a result of a "nuget delete" command.
  49. - delete: package is deleted from the repository's local filesystem.
  50. - delist:
  51. - "nuget delete": the "hidden" file attribute of the corresponding nupkg on the repository local filesystem is turned on instead of deleting the file.
  52. - "nuget list" skips delisted packages, i.e. those that have the hidden attribute set on their nupkg.
  53. - "nuget install packageid -version version" command will succeed for both listed and delisted packages.
  54. e.g. delisted packages can still be downloaded by clients that explicitly specify their version.
  55. -->
  56. <add key="enableDelisting" value="false" />
  57.  
  58. <!--
  59. Set enableFrameworkFiltering to true to enable filtering packages by their supported frameworks during search.
  60. -->
  61. <add key="enableFrameworkFiltering" value="false" />
  62.  
  63. <!--
  64. When running NuGet.Server in a NAT network, ASP.NET may embed the server's internal IP address in the V2 feed.
  65. Uncomment the following configuration entry to enable NAT support.
  66. -->
  67. <!-- <add key="aspnet:UseHostHeaderForRequestUrl" value="true" /> -->
  68. <!--
  69. Set enableFileSystemMonitoring to true (default) to enable file system monitoring (which will update the package cache appropriately on file system changes).
  70. Set it to false to disable file system monitoring.
  71. NOTE: Disabling file system monitoring may result in increased storage capacity requirements as package cache may only be purged by a background job running
  72. on a fixed 1-hour interval.
  73. -->
  74. <add key="enableFileSystemMonitoring" value="true" />
  75. </appSettings>
這裡就小弟的了解,簡單說明各設定值資訊
  • requireApiKey : 當開發者 push/delete package 時是否需要 api key 資訊
  • apiKey : 設定 api key 數值作為讓開發者 push/delete package 使用
  • packagesPath : 更改上傳 package 檔案存放的位置,預設為 project package folder 目錄
  • cacheFileName : 更改內部 cache 的名稱,預設為 System.Environment.MachineName
  • allowOverrideExistingPackageOnPush : 是否允許覆蓋具有相同 ID + 版本號的 package。False : 不允許
  • ignoreSymbolsPackages : 是否要過濾 symbols package,設定為 true 時會忽略文件名稱含有 '.symbols.nupkg' 或是 '/src' 的package 內容.
  • enableDelisting : 是否啟用 delist 設定,當開啟 delist 設定時下 delete command line 會對應到 nupkg 的 hiddle 屬性,而不是直接刪除 package 檔案。對於 hiddle 的 package 仍可透過指定版本的 command line 進行 install 的動作
  • enableFrameworkFiltering : 搜尋時是否啟用過濾 framework 功能
  • enableFileSystemMonitoring : 是否啟用文件系統監控機制,當設定為 true 時候啟用文件更新時會適當更新 cache 資料
Publish & Test Project
接著為了簡單測試我在本機環境 IIS 架設 nuget application,並透過 Visual Studio publish 功能至 IIS 設定的 nuget server 路徑下,步驟如下
Step 1 : 使用 VS publish 功能,將 Nuget server 相關 application 發佈到指定資料夾,ex : D:\nugetserver
Step 2 : 開啟 IIS 並建立新站台為 nuget、指定 application 路徑位置、port number,按下確定
Step 3 : 在瀏覽器網址輸入稍早 IIS 設定的網址與 port number,localhost:9487 即可看到 nuget server 已架設完成
安裝成功,打完收工 ! 

後續
上面簡單介紹如何架設團隊專用的 nuget server,成功的踏出第一步,但後續還有很多細節是需要注意的,例如 push package 時是透過 command line 到 nuget server,但其實可以透過工具來協助上傳的動作;安裝完 nuget server 但如果 push 失敗要如何得知錯誤訊息資訊 log;除了透過 install nuget.server package 是否還有其他更好用的 nuget server ...等以上內容希望之後在分享,若有更多與 nuget 相關資訊也歡迎留言討論,謝謝 :) 


參考
nuget
搭建公司内部的NuGet Server

Related Posts:

  • [.NET] ASP.NET 狀態管理(State Management):ViewState Web應用程式是沒有狀態(Stateless)的,從前面介紹的文章(Application、Page Life-Cycle)可以了解到,每次用戶端發送請求(Request)到伺服器端時,都會建立Web網頁類別的新執行個體。由於每個請求都是新的個體,這也代表每次來回存取時,網頁及控制項的資訊將會遺失。例如,根據預設,當使用者在網頁textbox控制項輸入「Hello world」,按下按鈕送出後,這項資訊就會傳送到伺服器。但伺服器在回傳(Res… Read More
  • [.NET] Default 和 NativeImage 資料夾問題 收到同事反應公司 Server C:\ 硬碟空間嚴重不足,確認後發現 C:\errors 資料空間占了 25g 而且還以驚人的速度成長中,進到資料夾底下查看分 Default 與 NativeImage folder 存放各種應用程式 Log ,如下圖所示 其中竟然還有 w3wp.exe,到底這些是如何記錄的呢 ? 本篇文章簡單記錄解決問題的過程 解決方案 為了追根究柢,點開其中 w3wp.exe 資料… Read More
  • [.NET] 字串加密 MD5、SHA1 常在開發中遇到需要將特定資料加密的動作,在儲存到資料庫中(比如說網站用戶的密碼加密後存到資料庫中,用戶在登入時,在把用戶輸入的密碼進行加密,再與資料庫密碼欄位比較是否一致)在.NET Framework中,可以透過 System.Security.Cryptography 命名空間來產生加密演算法的金鑰(註一),在用雜湊值(Hash Value)的加密方式達到目的,雜湊演算法將任意長度的二進位值對應到固定長度較小的二進位值,稱為雜湊值 (Ha… Read More
  • [C#] Named Arguments 具名引數前言  同事在開發專案時詢問某 method 有多個參數,有些參數是選擇輸入有預設值,因參數太多是否有方法可以指定參數名字 ? 否則一堆有設定預設值的在閱讀代碼會造成困擾,不小心也可能傳入錯誤變數,聽到當下建議抽成一個 Model 這樣更容易維護,但他表示由於引用的方法過多會擔心會牽一髮動全身,會有不可預期和毀滅性的災害發生,因此可以使用 具名引數 named arguments 來指定需要傳入的… Read More
  • [.NET] QueryString 的那些事 概述 Querystring(查詢字串)是附加在網頁URL結尾的資訊。主要是在Url上傳遞資料,可能是一個搜尋字串、頁碼、某項特定的指標…或類似的東西 在網址結尾加上一個問號(?)開始,每一組參數都是用「&」區隔開來,是一種KEY / Value的組合。 舉例來說(參考上圖),在Yahoo首頁搜尋引擎輸入「Hello world」後,在按下搜尋按鈕,會導頁到查詢結果頁面,並將符合搜尋字串相關資料呈現出來。 運作原理是透過Querystrin… Read More

0 意見:

張貼留言

Copyright © 2025 m@rcus 學習筆記 | Powered by Blogger

Design by Anders Noren | Blogger Theme by NewBloggerThemes.com