You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PowerShell批量推送nupkg至GitHub Packages(GitHub Actions场景)

实现PowerShell批量推送NuGet包到GitHub Packages

我来帮你搞定这个批量推送NuGet包的需求,刚好在GitHub Actions环境下配置过类似的流程,给你几个实用的方案:

基础实现方案

先用你找到的Get-ChildItem获取所有nupkg文件,然后循环遍历每个文件,先回显要执行的命令(模拟sh -v的效果),再执行推送操作:

# 递归查找当前目录下所有.nupkg文件(仅获取文件,排除目录)
$nupkgFiles = Get-ChildItem -Path . -Recurse -Include *.nupkg -File

foreach ($file in $nupkgFiles) {
    # 构建带引号的命令字符串,避免路径含空格时出错
    $pushCommand = "dotnet nuget push `"$($file.FullName)`" --source github"
    
    # 回显要执行的命令,方便调试查看
    Write-Host "Executing: $pushCommand"
    
    # 执行推送命令,用&调用外部命令更安全
    & dotnet nuget push $file.FullName --source github
}

这里的-File参数确保我们只拿到文件(不会误获取目录),$file.FullName用完整路径避免相对路径带来的问题,&是PowerShell中调用外部可执行文件的标准操作符,能妥善处理路径含空格的场景。

更简洁的管道版实现

如果你喜欢用PowerShell的管道风格,可以把查找和遍历合并成一行,代码更紧凑:

Get-ChildItem -Path . -Recurse -Include *.nupkg -File | ForEach-Object {
    $cmd = "dotnet nuget push `"$($_.FullName)`" --source github"
    Write-Host "Executing: $cmd"
    & dotnet nuget push $_.FullName --source github
}

这里的$_代表管道中当前传递的文件对象,和Shell里的{}作用类似。

带错误处理的健壮版本

如果需要在某个包推送失败时给出明确提示,甚至决定是否终止流程,可以添加try/catch块:

Get-ChildItem -Path . -Recurse -Include *.nupkg -File | ForEach-Object {
    $filePath = $_.FullName
    $pushCommand = "dotnet nuget push `"$filePath`" --source github"
    Write-Host "Executing: $pushCommand"
    
    try {
        # 启用-ErrorAction Stop让错误触发catch块
        & dotnet nuget push $filePath --source github -ErrorAction Stop
        Write-Host "✅ Successfully pushed $filePath" -ForegroundColor Green
    }
    catch {
        Write-Host "❌ Failed to push $filePath : $_" -ForegroundColor Red
        # 如果需要失败就终止整个脚本,取消下面的注释
        # exit 1
    }
}

GitHub Actions环境下的注意事项

在GitHub Actions里推送时,别忘了配置认证信息:

  1. 需要给job添加packages: write权限
  2. 使用GITHUB_TOKEN作为NuGet的API密钥
  3. 明确指定GitHub Packages的源地址(格式为https://nuget.pkg.github.com/你的用户名或组织名/index.json)

示例Workflow片段:

jobs:
  build-and-push:
    runs-on: ubuntu-latest
    permissions:
      packages: write
      contents: read
    steps:
      - uses: actions/checkout@v4
      - name: Setup .NET
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: '8.x' # 替换为你的项目使用的.NET版本
      - name: Build and pack
        run: dotnet pack --configuration Release --output ./nupkgs
      - name: Push NuGet packages
        env:
          NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          Get-ChildItem -Path ./nupkgs -Recurse -Include *.nupkg -File | ForEach-Object {
              $sourceUrl = "https://nuget.pkg.github.com/你的用户名或组织名/index.json"
              $cmd = "dotnet nuget push `"$($_.FullName)`" --source $sourceUrl --api-key `$env:NUGET_AUTH_TOKEN"
              Write-Host "Executing: $cmd"
              & dotnet nuget push $_.FullName --source $sourceUrl --api-key $env:NUGET_AUTH_TOKEN
          }

内容的提问来源于stack exchange,提问作者Thorbjørn Ravn Andersen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:25:14