只有累積,沒有奇蹟

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 相關設定資訊,內容如下
     <staticContent>
      <mimeMap fileExtension=".nupkg" mimeType="application/zip" />
    </staticContent><modules runAllManagedModulesForAllRequests="true">
      <remove name="WebDAVModule" />
    </modules><security>
      <requestFiltering>
        <!-- maxAllowedContentLength is specified in Bytes -->
        <requestLimits maxAllowedContentLength="31457280" />
      </requestFiltering>
    </security></system.webServer>
    <appSettings>
    <!--
    Determines if an Api Key is required to push\delete packages from the server. 
    -->
    <add key="requireApiKey" value="true" />

    <!-- 
    Set the value here to allow people to push/delete packages from the server.
    NOTE: This is a shared key (password) for all users.
    -->
    <add key="apiKey" value="123456" />

    <!--
    Change the path to the packages folder. Default is ~/Packages.
    This can be a virtual or physical path.
    -->
    <add key="packagesPath" value="" />

    <!--
    Change the name of the internal cache file. Default is machine name (System.Environment.MachineName).
    This is the name of the cache file in the packages folder. No paths allowed.
    -->
    <add key="cacheFileName" value="" />

    <!--
    Set allowOverrideExistingPackageOnPush to false to mimic NuGet.org's behaviour (do not allow overwriting packages with same id + version).
    -->
    <add key="allowOverrideExistingPackageOnPush" value="false" />

    <!--
    Set ignoreSymbolsPackages to true to filter out symbols packages. Since NuGet.Server does not come with a symbol server,
    it makes sense to ignore this type of packages. When enabled, files named `.symbols.nupkg` or packages containing a `/src` folder will be ignored.
    
    If you only push .symbols.nupkg packages, set this to false so that packages can be uploaded.
    -->
    <add key="ignoreSymbolsPackages" value="true" />

    <!--
    Set enableDelisting to true to enable delist instead of delete as a result of a "nuget delete" command.
    - delete: package is deleted from the repository's local filesystem.
    - delist: 
      - "nuget delete": the "hidden" file attribute of the corresponding nupkg on the repository local filesystem is turned on instead of deleting the file.
      - "nuget list" skips delisted packages, i.e. those that have the hidden attribute set on their nupkg.
      - "nuget install packageid -version version" command will succeed for both listed and delisted packages.
        e.g. delisted packages can still be downloaded by clients that explicitly specify their version.
    -->
    <add key="enableDelisting" value="false" />

    <!--
    Set enableFrameworkFiltering to true to enable filtering packages by their supported frameworks during search.
    -->
    <add key="enableFrameworkFiltering" value="false" />

    <!--
    When running NuGet.Server in a NAT network, ASP.NET may embed the server's internal IP address in the V2 feed.
    Uncomment the following configuration entry to enable NAT support.
    -->
    <!-- <add key="aspnet:UseHostHeaderForRequestUrl" value="true" /> -->
    <!--
    Set enableFileSystemMonitoring to true (default) to enable file system monitoring (which will update the package cache appropriately on file system changes).
    Set it to false to disable file system monitoring.
    NOTE: Disabling file system monitoring may result in increased storage capacity requirements as package cache may only be purged by a background job running 
    on a fixed 1-hour interval.
    -->
    <add key="enableFileSystemMonitoring" value="true" />
  </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

0 意見:

張貼留言

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

Design by Anders Noren | Blogger Theme by NewBloggerThemes.com