使用 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 依赖是否是最新的,如果不是最新的会打印当前版本的信息和最新版本的信息

e9e755ebdfd2ba0e0d8b12d8ec02daf7.png

这只会打印版本更新信息,并不会直接更新,如果要更新包版本,只需要添加一个 -u 的选项即可,直接 -u 会更新所有不是最新的 package

072ca0becdc6fe63730b9108c8d82968.png

CI Service

利用 dotnet-outdated 我们可以检测项目中引用的 NuGet 包是否有更新,也可以将其改造为一个 CI 服务,在 push 代码或者定期检查项目中引用的 NuGet 包是否有新版本,下面是我自定义的一个 Github Actions 示例:

name: dotnet-outdated

on:
  schedule:
    - cron:  '0 1 * * *'
  push:
    branches: 
      - "master"
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
    - name: Setup .NET 6
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: '6.0.x'
        include-prerelease: true
    - name: build
      run: dotnet build
    - name: install dotnet-outdated
      run: dotnet tool install --global dotnet-outdated-tool
    - name: Run dotnet-outdated
      run: dotnet-outdated -u
    - name: check for changes
      run: |
        if git diff --exit-code; then
          echo "has_changes=false" >> $GITHUB_ENV
        else
          echo "has_changes=true" >> $GITHUB_ENV
        fi
    - name: Build again
      if: ${{ env.has_changes == 'true' }}
      run: dotnet build
    - name: Create Pull Request
      if: ${{ env.has_changes == 'true' }}
      uses: peter-evans/create-pull-request@v3
      with:
        token: ${{ secrets.GITHUB_TOKEN }}
        commit-message: "Update NuGet Packages"
        title: 'Update NuGet Packages'
        body: >
          This PR updates the outdated NuGet packages.
        labels: automated pr
        branch: update-nuget-dependencies
        base: ${{ github.ref }}

基本执行过程是这样的,先 build 项目,然后安装 dotnet-outdated 工具并尝试更新项目中的 NuGet 包,然后通过 git diff 检测是否有文件变更,如果有变更则重新 build 项目看是否会失败,如果成功了,就提交变更并自动发起一个 PR

这种方式如果更新最新版本失败的话就会导致 CI 失败,如果不希望 CI 失败也可以改造更新版本后的 dotnet build,改造成下面的方式

- name: Build again
      if: ${{ env.has_changes == 'true' }}
      run: |
        if dotnet build; then
          echo "has_breaking_changes=false" >> $GITHUB_ENV
        else
          echo "has_breaking_changes=true" >> $GITHUB_ENV
        fi
    - name: Create Pull Request
      if: ${{ env.has_breaking_changes == 'false' }}
      uses: peter-evans/create-pull-request@v3
      with:
        token: ${{ secrets.GITHUB_TOKEN }}
        commit-message: "Update NuGet Packages"
        title: 'Update NuGet Packages'
        body: >
          This PR updates the outdated NuGet packages.
        labels: automated pr
        branch: update-nuget-dependencies
        base: ${{ 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 版本

652ceddc0e657b82234eafd57b655319.png

另外可以指定 --pre-release 来指定使用预览版本的更新,默认会自动根据当前 NuGet 包的版本去决定是否使用预览版本,如果是预览版本是会使用预览版本,如果是稳定版则不会使用预览版,可以显式指定 -pre=Always/--pre-release=Always 来指定始终使用预览版本

我们也可以指定 -u:prompt 来一个一个提示更新

be75b3c86de38e94e4f27ba779eccba1.png

更多用法可以参考文档

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值