将PowerShell模块发布到GitHub私有包仓库后无法安装的问题
解决GitHub Packages私有PowerShell模块无法安装的问题
已将PowerShell模块发布到GitHub Packages,但执行Install-Module时提示无法解析源、找不到包。调试发现NuGet Provider未识别已注册的GitHub源,且访问私有源的index.json时无目标包信息,以下是具体解决步骤:
一、修复NuGet源与PSRepository的同步问题
PowerShellGet依赖NuGet Provider管理包源,仅注册PSRepository不足以让NuGet识别该源,需确保两者配置完全一致:
- 清理旧的无效源:
# 移除已有的NuGet源和PSRepository nuget sources Remove -Name 'GitHub' Unregister-PSRepository -Name 'GitHub'
- 重新注册源与仓库:
$username = '<你的GitHub用户名>' $token = '<你的GitHub个人访问令牌(需包含packages:read和packages:write权限)>' $sourceName = 'GitHub' $source = "https://nuget.pkg.github.com/$username/index.json" # 注册NuGet源(StorePasswordInClearText仅本地测试用,生产环境建议加密存储) nuget sources Add -Name $sourceName -Source $source -UserName $username -Password $token -StorePasswordInClearText # 创建PSCredential对象 $creds = New-Object System.Management.Automation.PSCredential -ArgumentList $username, (ConvertTo-SecureString -AsPlainText $token -Force) # 注册PSRepository并设为可信 Register-PSRepository -Name $sourceName -SourceLocation $source -PublishLocation $source -Credential $creds -InstallationPolicy Trusted
- 验证注册结果:
# 检查NuGet源列表 nuget sources List # 检查PSRepository列表 Get-PSRepository
确保两者的源地址、名称完全匹配。
二、修复模块清单文件(psd1)的警告
将FunctionsToExport通配符'*'改为具体函数名,避免发布警告同时保证元数据准确:
打开模块的psd1文件,修改:
FunctionsToExport = @('Confirm-Command') # 替换为你的实际函数名
保存后重新发布模块。
三、重新发布模块并验证包索引
- 重新发布模块:
Publish-Module -Path .\Confirm-Command\ -Repository GitHub -NuGetApiKey $token
发布成功后,登录GitHub Packages页面确认包已存在且状态正常。
- 验证
index.json的包信息:
使用带凭证的请求确认包已被收录到索引:
$response = Invoke-WebRequest -Uri $source -Credential $creds $packages = $response.Content | ConvertFrom-Json # 检查是否包含目标模块 $packages.items | Where-Object { $_.id -eq 'Confirm-Command' }
若返回为空,等待几分钟让GitHub同步索引,或检查包ID与模块名称是否完全一致(大小写敏感)。
四、正确安装模块
确保使用已注册的PSRepository,若本地未存储凭证需显式指定:
Install-Module -Name Confirm-Command -Repository GitHub -Credential $creds -Scope CurrentUser
若已在NuGet源中存储密码,可省略-Credential参数。
优化后的自动化流程
实现你需要的认证和发布函数:
1. 认证函数:Authenticate-ToGitHub
function Authenticate-ToGitHub { param( [Parameter(Mandatory=$true)] [string]$Username, [Parameter(Mandatory=$true)] [string]$Token, [string]$SourceName = 'GitHub' ) $source = "https://nuget.pkg.github.com/$Username/index.json" # 清理旧源 nuget sources Remove -Name $SourceName -ErrorAction SilentlyContinue Unregister-PSRepository -Name $SourceName -ErrorAction SilentlyContinue # 注册NuGet源 nuget sources Add -Name $SourceName -Source $source -UserName $Username -Password $Token -StorePasswordInClearText # 注册PSRepository $creds = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, (ConvertTo-SecureString -AsPlainText $Token -Force) Register-PSRepository -Name $SourceName -SourceLocation $source -PublishLocation $source -Credential $creds -InstallationPolicy Trusted Write-Host "已成功认证并注册GitHub Packages源:$SourceName" }
2. 发布函数:Publish-MyModuleToGitHub
function Publish-MyModuleToGitHub { param( [Parameter(Mandatory=$true)] [string]$ModuleName, [Parameter(Mandatory=$true)] [string]$ModulePath, [Parameter(Mandatory=$true)] [string]$GitHubUser, [string]$SourceName = 'GitHub', [Parameter(Mandatory=$true)] [string]$Token ) # 验证模块清单存在 $psdPath = Join-Path $ModulePath "$ModuleName.psd1" if (-not (Test-Path $psdPath)) { Write-Error "未找到模块清单文件:$psdPath" return } # 发布模块 Publish-Module -Path $ModulePath -Repository $SourceName -NuGetApiKey $Token Write-Host "模块 $ModuleName 已成功发布到GitHub Packages" }
使用示例
# 认证到GitHub Authenticate-ToGitHub -Username '<你的GitHub用户名>' -Token '<你的令牌>' # 发布模块 Publish-MyModuleToGitHub -ModuleName 'Confirm-Command' -ModulePath '.\Confirm-Command\' -GitHubUser '<你的GitHub用户名>' -Token '<你的令牌>' # 安装模块 Install-Module -Name 'Confirm-Command' -Repository 'GitHub'
内容的提问来源于stack exchange,提问作者Marl
相关产品推荐
相关产品推荐

