使用 powershell 将多个 nupkg 文件推送到 Github 包?

问题描述

我们正在 Github 上进行整合,我正在研究如何使用 Github Actions 构建我们的 C# 项目之一,并将生成的 nuget 包推送到 Github 包。我的测试项目生成了大约 15 个包。

看来“dotnet nuget push”命令一次只支持推送一个文件,所以我正在尝试编写一个与此shell命令大致相同的powershell脚本

find . -name '*.nupkg' -exec '/usr/bin/echo' 'dotnet nuget push "{}" --source github' ';' | sh -v -

换句话说,为找到的每个 nupkg 文件生成一个“dotnet nuget push "..." --source github”命令。

到目前为止,我已经找到了 powershell 命令 Get-Childitem -Path . -Recurse -Include *.nupkg,它可以获取尽可能多的文件,但是我需要“在 这个 文件上执行命令”位。我还希望在执行之前也回显执行的命令,类似于“-v”标志的作用。

另外,如果有更简单的方法来做到这一点(其他人之前一定遇到过这个问题并且更优雅地解决了它),我会考虑这样做。

谢谢。

如何

解决方法

要推送到 nuget.org,您应该能够使用以下命令:

dotnet nuget push "**/*.nupkg" --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.PUBLISH_TO_NUGET_ORG }} --skip-duplicate

要推送到您的 github.com 提要,您应该能够使用以下命令,您应该会发现该命令始终有效(也适用于更大的 nuget 包,例如多目标包):

nuget.exe push "**/*.nupkg" -NoSymbols -SkipDuplicate

如果您使用的是 GitHub Actions,

name: .NET Core

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:

    runs-on: windows-latest

    steps:
    - name: Checkout
      uses: actions/checkout@v2
    
    - name: Setup .NET Core
      uses: actions/[email protected]
      with:
        dotnet-version: 3.1.301
        # Authenticates packages to push to GPR
        source-url: https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json
      env:
        NUGET_AUTH_TOKEN: '%NUGET_AUTH_TOKEN%'
    
    - name: Setup MSBuild
      uses: microsoft/[email protected]
    
    - name: Install dependencies
      run: msbuild /t:Restore
      env:
        NUGET_AUTH_TOKEN: ${{ github.token }}
    
    - name: Build
      run: |
        msbuild /t:Pack /p:Configuration=Debug Library/MintPlayer.MVVM/MintPlayer.MVVM.csproj
        dotnet pack Library\MintPlayer.MVVM.BaseModel\MintPlayer.MVVM.BaseModel.csproj --configuration=Release
    
    - name: PushNuget
      run: dotnet nuget push "**/*.nupkg" --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.PUBLISH_TO_NUGET_ORG }} --skip-duplicate
    
    - name: PushGithub
      run: nuget.exe push "**/*.nupkg" -NoSymbols -SkipDuplicate
      env:
        NUGET_AUTH_TOKEN: ${{ github.token }}

目前 github.com 没有符号服务器,因此使用了 -NoSymbols 标志。

,

这里惯用的解决方案是将生成的文件信息对象通过管道传输到 ForEach-Object

Get-ChildItem -Path . -Recurse -Filter *.nupkg |ForEach-Object {
  Write-Host "About to push a package named '$($_.Name)'..." 
  dotnet nuget push $_.FullName --source github
}

ForEach-Object 块内,$_ 指的是当前正在处理/迭代的单个项目。

请注意,大多数 PowerShell cmdlet 返回 复杂对象(与基于字符串的输出相反) - 因此 $_.FullName 表达式 - FullName 属性包含指向有问题的文件(即C:\path\to\package.nupkg