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

使用GitHub Action执行Publish-Module时触发401未授权错误求助

问题:Publish-Module发布PowerShell模块到GitHub Packages时触发401未授权错误

在安装了GitHub Action运行器的全新Windows Server 2022系统上,通过GitHub Action调用Publish-Module cmdlet将pswslmanage模块发布到GitHub Packages仓库时,始终触发401未授权错误。

工作流配置

jobs:
  pwslmanage:
    name: pswslmanage
    runs-on: [self-hosted]
    permissions:
      actions: read
      packages: write
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          path: ./pswslmanage

      - name: Publish
        run: |
          $_repo_name = "SAPSGallery"
          $_repo_owner = "myorg"
          $_feed = "https://nuget.pkg.github.com/$_repo_owner/index.json"
          $_user = "user-myorg"
          $_token = "$($env:GH_TOKEN)" | ConvertTo-SecureString -AsPlainText -Force
          $_creds = New-Object System.Management.Automation.PSCredential -ArgumentList @($_user, $_token)
          $_module_name = "pswslmanage"
          $_repo_dir = "{0}\myorg-pswslmanage\pswslmanage" -f $($env:RUNNER_WORKSPACE)

          Register-PSRepository -Name $_repo_name -SourceLocation $_feed -PublishLocation $_feed -InstallationPolicy 'Trusted' -Credential $_creds -Verbose
          Publish-Module -Path "$_repo_dir" -Repository "$_repo_name" -NuGetApiKey "$($env:GH_TOKEN)" -Verbose
        env:
          GH_TOKEN: ${{secrets.GITHUB_TOKEN}}

错误信息

无论在PowerShell 5.1还是PowerShell Core 7.3.6环境下测试,均返回以下错误:

Unable to load the service index for source https://nuget.pkg.github.com/myorg/index.json. 401 (Unauthorized)

详细VERBOSE输出

VERBOSE: Acquiring providers for assembly: C:\program files\powershell\7\Modules\PackageManagement\coreclr\netstandard2.0\Microsoft.PackageManagement.NuGetProvider.dll 
VERBOSE: Acquiring providers for assembly: C:\program files\powershell\7\Modules\PackageManagement\coreclr\netstandard2.0\Microsoft.PackageManagement.MetaProvider.PowerShell.dll 
VERBOSE: Acquiring providers for assembly: C:\program files\powershell\7\Modules\PackageManagement\coreclr\netstandard2.0\Microsoft.PackageManagement.ArchiverProviders.dll 
VERBOSE: Acquiring providers for assembly: C:\program files\powershell\7\Modules\PackageManagement\coreclr\netstandard2.0\Microsoft.PackageManagement.CoreProviders.dll 
VERBOSE: Suppressed Verbose Repository details, Name = 'SAPSGallery', Location = 'https://nuget.pkg.github.com/myorg/index.json'; IsTrusted = 'True'; IsRegistered = 'True'. 
VERBOSE: Repository details, Name = 'SAPSGallery', Location = 'https://nuget.pkg.github.com/myorg/index.json'; IsTrusted = 'True'; IsRegistered = 'True'. 
VERBOSE: Repository details, Name = 'SAPSGallery', Location = 'https://nuget.pkg.github.com/myorg/index.json'; IsTrusted = 'True'; IsRegistered = 'True'. 
VERBOSE: Publish Location:'https://nuget.pkg.github.com/myorg/index.json'. VERBOSE: Module 'pswslmanage' was found in 'C:\ProgramData\GitHub-Actions-Runner_work\myorg-pswslmanage\myorg-pswslmanage\pswslmanage'. 
VERBOSE: Suppressed Verbose Repository details, Name = 'SAPSGallery', Location = 'https://nuget.pkg.github.com/myorg/index.json'; IsTrusted = 'True'; IsRegistered = 'True'. 
VERBOSE: Repository details, Name = 'SAPSGallery', Location = 'https://nuget.pkg.github.com/myorg/index.json'; IsTrusted = 'True'; IsRegistered = 'True'. 
VERBOSE: Using the provider 'PowerShellGet' for searching packages. 
VERBOSE: Using the specified source names : 'SAPSGallery'. 
VERBOSE: Getting the provider object for the PackageManagement Provider 'NuGet'. 
VERBOSE: The specified Location is 'https://nuget.pkg.github.com/myorg/index.json' and PackageManagementProvider is 'NuGet'. 
VERBOSE: Retry downloading 'https://nuget.pkg.github.com/myorg/query' for '2' more times 
VERBOSE: Retry downloading 'https://nuget.pkg.github.com/myorg/query' for '1' more times 
VERBOSE: Retry downloading 'https://nuget.pkg.github.com/myorg/query' for '0' more times 
VERBOSE: Retry downloading 'https://nuget.pkg.github.com/myorg/pswslmanage/index.json' for '0' more times 
VERBOSE: Retry downloading 'https://nuget.pkg.github.com/myorg/pswslmanage/index.json' for '0' more times 
VERBOSE: Retry downloading 'https://nuget.pkg.github.com/myorg/pswslmanage/index.json' for '0' more times 
VERBOSE: Total package yield:'0' for the specified package 'pswslmanage'. 
VERBOSE: Performing the operation "Publish-Module" on target "Version '1.0.0' of module 'pswslmanage'". 
VERBOSE: Calling Publish-PSArtifactUtility VERBOSE: Calling New-NuspecFile VERBOSE: Calling New-NugetPackage 
VERBOSE: Calling C:\ProgramData\Microsoft\Windows\PowerShell\PowerShellGet\NuGet.exe pack "C:\Users\Administrator\AppData\Local\Temp\1989646393\pswslmanage\pswslmanage.nuspec" -outputdirectory "C:\Users\Administrator\AppData\Local\Temp\1989646393\pswslmanage" -noninteractive 
VERBOSE: C:\ProgramData\Microsoft\Windows\PowerShell\PowerShellGet\NuGet.exe output: VERBOSE: Attempting to build package from 'pswslmanage.nuspec'. 
VERBOSE: WARNING: NU5119: File 'C:\Users\Administrator\AppData\Local\Temp\1989646393\pswslmanage.gitattributes' was not added to the package. Files and folders starting with '.' or ending with '.nupkg' are excluded by default. To include this file, use -NoDefaultExcludes from the commandline 
VERBOSE: WARNING: NU5119: File 'C:\Users\Administrator\AppData\Local\Temp\1989646393\pswslmanage.gitignore' was not added to the package. Files and folders starting with '.' or ending with '.nupkg' are excluded by default. To include this file, use -NoDefaultExcludes from the commandline 
VERBOSE: WARNING: NU5110: The script file 'pswslmanage-helper.ps1' is outside the 'tools' folder and hence will not be executed during installation of this package. Move it into the 'tools' folder. 
VERBOSE: WARNING: NU5110: The script file 'pswslmanage-roles.ps1' is outside the 'tools' folder and hence will not be executed during installation of this package. Move it into the 'tools' folder. 
VERBOSE: WARNING: NU5110: The script file 'pswslmanage-start.ps1' is outside the 'tools' folder and hence will not be executed during installation of this package. Move it into the 'tools' folder. 
VERBOSE: WARNING: NU5110: The script file 'tests\pswslmanage.tests.ps1' is outside the 'tools' folder and hence will not be executed during installation of this package. Move it into the 'tools' folder. 
VERBOSE: WARNING: NU5111: The script file 'pswslmanage-helper.ps1' is not recognized by NuGet and hence will not be executed during installation of this package. Rename it to install.ps1, uninstall.ps1 or init.ps1 and place it directly under 'tools'. 
VERBOSE: WARNING: NU5111: The script file 'pswslmanage-roles.ps1' is not recognized by NuGet and hence will not be executed during installation of this package. Rename it to install.ps1, uninstall.ps1 or init.ps1 and place it directly under 'tools'. 
VERBOSE: WARNING: NU5111: The script file 'pswslmanage-start.ps1' is not recognized by NuGet and hence will not be executed during installation of this package. Rename it to install.ps1, uninstall.ps1 or init.ps1 and place it directly under 'tools'. 
VERBOSE: WARNING: NU5111: The script file 'tests\pswslmanage.tests.ps1' is not recognized by NuGet and hence will not be executed during installation of this package. Rename it to install.ps1, uninstall.ps1 or init.ps1 and place it directly under 'tools'. 
VERBOSE: The package pswslmanage.1.0.0 is missing a readme. Go to https://aka.ms/nuget/authoring-best-practices/readme to learn why package readmes are important. 
VERBOSE: Successfully created package 'C:\Users\Administrator\AppData\Local\Temp\1989646393\pswslmanage\pswslmanage.1.0.0.nupkg'. 
VERBOSE: 
VERBOSE: finished running C:\ProgramData\Microsoft\Windows\PowerShell\PowerShellGet\NuGet.exe with exit code 0 
VERBOSE: Created Nuget Package C:\Users\Administrator\AppData\Local\Temp\1989646393\pswslmanage\pswslmanage.1.0.0.nupkg 
VERBOSE: Successfully created nuget package at C:\Users\Administrator\AppData\Local\Temp\1989646393\pswslmanage\pswslmanage.1.0.0.nupkg 
VERBOSE: Calling Publish-NugetPackage -NupkgPath C:\Users\Administrator\AppData\Local\Temp\1989646393\pswslmanage\pswslmanage.1.0.0.nupkg -Destination https://nuget.pkg.github.com/myorg/index.json -NugetExePath C:\ProgramData\Microsoft\Windows\PowerShell\PowerShellGet\NuGet.exe -UseDotnetCli:FalseWrite-Error: Failed to publish module 'pswslmanage': 'nuget.exe failed to push Unable to load the service index for source https://nuget.pkg.github.com/myorg/index.json. Response status code does not indicate success: 401 (Unauthorized). '.

已尝试的解决方法

  • 使用浏览器携带GH_TOKEN可成功下载index.json
  • 替换为全权限PAT替代内置的secrets.GITHUB_TOKEN
  • 禁用TLS1.3并将nuget.exe下载到指定路径

以上方法均未解决问题,恳请提供解决思路。

内容的提问来源于stack exchange,提问作者David Koenig

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 00:47:14