使用 dotnet-outdated 维护项目 nuget 包版本
使用 dotnet-outdated 維護項目 nuget 包版本
Intro
我們項目中或多或少都會有一些 NuGet 包,使用到 NuGet 包時,如何保證我們的 NuGet 包不會太舊呢?我們可以借助 dotnet-outdated 來檢查項目中的 NuGet 包是否有更新
Sample
首先我們需要執行 dotnet tool install --global dotnet-outdated-tool 命令安裝 dotnet-outdated 工具,命令安裝好之后就會執行 dotnet-outdated 就可以分析當前解決方案/項目下的 NuGet 依賴是否是最新的,如果不是最新的會打印當前版本的信息和最新版本的信息
這只會打印版本更新信息,并不會直接更新,如果要更新包版本,只需要添加一個 -u 的選項即可,直接 -u 會更新所有不是最新的 package
CI Service
利用 dotnet-outdated 我們可以檢測項目中引用的 NuGet 包是否有更新,也可以將其改造為一個 CI 服務,在 push 代碼或者定期檢查項目中引用的 NuGet 包是否有新版本,下面是我自定義的一個 Github Actions 示例:
name:?dotnet-outdatedon:schedule:-?cron:??'0?1?*?*?*'push:branches:?-?"master" jobs:build:runs-on:?ubuntu-lateststeps:-?uses:?actions/checkout@v1-?name:?Setup?.NET?6uses:?actions/setup-dotnet@v1with:dotnet-version:?'6.0.x'include-prerelease:?true-?name:?buildrun:?dotnet?build-?name:?install?dotnet-outdatedrun:?dotnet?tool?install?--global?dotnet-outdated-tool-?name:?Run?dotnet-outdatedrun:?dotnet-outdated?-u-?name:?check?for?changesrun:?|if?git?diff?--exit-code;?thenecho?"has_changes=false"?>>?$GITHUB_ENVelseecho?"has_changes=true"?>>?$GITHUB_ENVfi-?name:?Build?againif:?${{?env.has_changes?==?'true'?}}run:?dotnet?build-?name:?Create?Pull?Requestif:?${{?env.has_changes?==?'true'?}}uses:?peter-evans/create-pull-request@v3with:token:?${{?secrets.GITHUB_TOKEN?}}commit-message:?"Update?NuGet?Packages"title:?'Update?NuGet?Packages'body:?>This?PR?updates?the?outdated?NuGet?packages.labels:?automated?prbranch:?update-nuget-dependenciesbase:?${{?github.ref?}}基本執行過程是這樣的,先 build 項目,然后安裝 dotnet-outdated 工具并嘗試更新項目中的 NuGet 包,然后通過 git diff 檢測是否有文件變更,如果有變更則重新 build 項目看是否會失敗,如果成功了,就提交變更并自動發起一個 PR
這種方式如果更新最新版本失敗的話就會導致 CI 失敗,如果不希望 CI 失敗也可以改造更新版本后的 dotnet build,改造成下面的方式
-?name:?Build?againif:?${{?env.has_changes?==?'true'?}}run:?|if?dotnet?build;?thenecho?"has_breaking_changes=false"?>>?$GITHUB_ENVelseecho?"has_breaking_changes=true"?>>?$GITHUB_ENVfi-?name:?Create?Pull?Requestif:?${{?env.has_breaking_changes?==?'false'?}}uses:?peter-evans/create-pull-request@v3with:token:?${{?secrets.GITHUB_TOKEN?}}commit-message:?"Update?NuGet?Packages"title:?'Update?NuGet?Packages'body:?>This?PR?updates?the?outdated?NuGet?packages.labels:?automated?prbranch:?update-nuget-dependenciesbase:?${{?github.ref?}}這樣 dotnet build 失敗的話就不會直接導致 CI 失敗,并且也不會自動創建 PR,但是這樣的話可能就不知道有更新了,還是建議使用第一種方式,讓 CI 失敗及時更新并修復失敗
More
dotnet-outdated 除了上面的簡單用法之外,還有更多小技巧
比如我們可以使用 --version-lock 來只更新小版本,比如使用 --version-lock=Major 只更新 Minor 版本的更新,舉個栗子,4.0.0 只會更新到 4.x.x ?版本,不會更新到 5.x.x 版本
另外可以指定 --pre-release 來指定使用預覽版本的更新,默認會自動根據當前 NuGet 包的版本去決定是否使用預覽版本,如果是預覽版本是會使用預覽版本,如果是穩定版則不會使用預覽版,可以顯式指定 -pre=Always/--pre-release=Always 來指定始終使用預覽版本
我們也可以指定 -u:prompt 來一個一個提示更新
更多用法可以參考文檔
References
https://github.com/dotnet-outdated/dotnet-outdated
https://github.com/OpenReservation/ReservationServer
https://github.com/OpenReservation/ReservationServer/blob/dev/.github/workflows/dotnet-outdated.yml
https://github.com/OpenReservation/ReservationServer/pull/56
https://github.com/WeihanLi/SparkTodo/blob/master/.github/workflows/dotnet-outdated.yml
總結
以上是生活随笔為你收集整理的使用 dotnet-outdated 维护项目 nuget 包版本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Dapr项目应用探索
- 下一篇: Dotnet 6.0 深度探索(一)