基礎環境配置
netcore安裝文檔:https://www.microsoft.com/net/learn/get-started/linuxcentos
jenkins相關配置:http://blog.csdn.net/xqj198404/article/details/78093590
jenkins項目配置
- service jenkins start 啓動服務
- 域名:8080(默認,可配置修改)登錄jenkins
- 配置BUILD過程的執行腳本需要注意通過jenkins的宏指定dotnet執行的正確路徑,並且不同操作系統的宏是不一樣的。windows是 %宏名%,linux則是 ${宏名}
- 項目編譯的路徑在linux下爲:
其中WORKSPACE是jenkins規定的工作空間的宏名 - 編譯以及發佈腳本 :【見修正後的腳本】,雖然會隱式調用restore,但是有多個源時最好顯式調用,並且採用publish而不是單純依賴build輸出。如果源指定缺少的話,可能會存在一個問題,即使編譯發佈正常,但是會缺少隱式引用的dll
dotnet restore ${WORKSPACE}/git項目的項目名 -s 源的地址(如果是公用源和私有源都存在需要多個-s參數) dotnet build ${WORKSPACE}/git項目的項目名 -r centos.7-x64(centos7運行時環境) dotnet publish ${WORKSPACE}/git項目的目錄名 -o ${WORKSPACE}/publish
- 上面的-o產生的publish目錄有一個問題,就是產生在git項目的目錄下,不知道是dotnet的問題還是對於宏的理解有錯誤
- dotnet publish 默認發佈爲debug,所以需要按照自己需求配置publish相關參數 相關文檔:https://docs.microsoft.com/zh-cn/dotnet/core/tools/dotnet-publish?tabs=netcore2x
nuget發佈
- 使用yum isntall nuget安裝nuget
- 參考文檔:https://docs.microsoft.com/en-us/nuget/create-packages/creating-a-package
- 配置文件參考:https://docs.microsoft.com/en-us/nuget/schema/nuget-config-file
- nexus相關文檔:https://help.sonatype.com/display/NXRM2/.NET+Package+Repositories+with+NuGet
- 在~/.nuget/NuGet/NuGet.Config文件中添加自定義的源,如果沒有安裝nuget則需要在dotnet nuget命令中指定源的配置文件
- 需要將nuspec文件中的模板替換,可以採用sed 也可以寫一個替換程序使用shell執行。如果沒有完全替換會有警告信息。
- 使用dll生成nuspec文件需要注意,id會根據執行路徑生成,所以最好使用cd命令切換執行路徑,防止ID名稱非法的情況,如果含有\符號會被認爲不合法。
- 執行nuget命令注意執行的路徑問題。
- 執行腳本【不再需要】
cd ${WORKSPACE}/publish/ nuget spec xxxx.dll sed -i 's!http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE!http://url/hayaaai.txt!' ${WORKSPACE}/publish/xxxxx.dll.nuspec sed -i 's!http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE!http://url/nexus/service/local/nuget/xxxxx/!' ${WORKSPACE}/publish/xxxxx.dll.nuspec sed -i 's!http://ICON_URL_HERE_OR_DELETE_THIS_LINE!http://url/ico/xxxx.ico!' ${WORKSPACE}/publish/xxxxx.dll.nuspec sed -i '20i<files><file src="Hayaa.BaseModel.dll" target="lib/\" /></files>' ${WORKSPACE}/publish/xxxxx.dll.nuspec nuget pack ${WORKSPACE}/publish/xxxxx.dll.nuspec nuget push ${WORKSPACE}/publish/xxxxx.dll.1.0.0.nupkg -Source http://url/repository/nuget-hosted -ApiKey nexus用戶的key
問題
理論是可行的,但是使用nuget發佈總是失敗,本以爲是nexus配置錯誤,但是使用nuget Package Explorer在windows上測試卻是可以的。證明nexus服務器配置沒有問題,有問題的是nuget在centos上存在bug,爆出對象錯誤,無法傳輸文件,但是這是由於打包文件錯誤造成還是卻是nuget確實存在問題,還需要實驗。
後進行了實驗,將nuget以及nuget依賴的mono進行了重裝,首先完全重裝了mono,而非採用依賴方式安裝,然後又安裝了nuget,然後就可以了。所以爆出的錯誤是mono應該缺少庫造成的。
後續
仔細閱讀相關工具文檔
https://docs.microsoft.com/zh-cn/dotnet/core/tools/?tabs=netcore2x
其實donet和nuget有一體化的命令,無需分開處理。
修正後的發佈腳本
dotnet restore ${WORKSPACE}/Hayaa.BaseModel
dotnet build ${WORKSPACE}/Hayaa.BaseModel
dotnet pack ${WORKSPACE}/Hayaa.BaseModel -o ${WORKSPACE}/publish
echo 'find |grep "nupkg" >var'
dotnet nuget push ${WORKSPACE}/publish/$var -k nexus的key -s http://url/repository/nuget-hosted/