6

据我所知,PowerShell 存储库是 NuGet 存储库......

GitHub 刚刚发布了他们的包注册表,我的公司目前用于 npm,但也有一个用于 NuGet 的端点。

我可以使用我的 GitHub 凭据访问 NuGet 端点 ( https://nuget.pkg.github.com/mycompany/index.json ),它返回一个有效的 json:

{
      "version": "3.0.0-beta.1",
      "resources": [
        {
          "@id": "https://nuget.pkg.github.com/mycompany/download",
          "@type": "PackageBaseAddress/3.0.0",
          "comment": "Get package content (.nupkg)."
        },
        {
          "@id": "https://nuget.pkg.github.com/mycompany/query",
          "@type": "SearchQueryService",
          "comment": "Filter and search for packages by keyword."
        },
        {
          "@id": "https://nuget.pkg.github.com/mycompany/query",
          "@type": "SearchQueryService/3.0.0-beta",
          "comment": "Filter and search for packages by keyword."
        },
        {
          "@id": "https://nuget.pkg.github.com/mycompany/query",
          "@type": "SearchQueryService/3.0.0-rc",
          "comment": "Filter and search for packages by keyword."
        },
        {
          "@id": "https://nuget.pkg.github.com/mycompany",
          "@type": "PackagePublish/2.0.0",
          "comment": "Push and delete (or unlist) packages."
        },
        {
          "@id": "https://nuget.pkg.github.com/mycompany",
          "@type": "RegistrationsBaseUrl",
          "comment": "Get package metadata."
        },
        {
          "@id": "https://nuget.pkg.github.com/mycompany",
          "@type": "RegistrationsBaseUrl/3.0.0-beta",
          "comment": "Get package metadata."
        },
        {
          "@id": "https://nuget.pkg.github.com/mycompany",
          "@type": "RegistrationsBaseUrl/3.0.0-rc",
          "comment": "Get package metadata."
        }
      ]
    }

我一直在尝试使用它在我的本地计算机上将其设置为存储库(在理想情况下,我会在我的 CI/CD 上推送模块并使它们可供人们Install-Module使用 GitHub 作为存储库):

PS C:> $gitHubCredential = Get-Credential
PS C:> (iwr https://nuget.pkg.github.com/mycompany/index.json -Credential $gitHubCredential).StatusCode
200
PS C:> Register-PSRepository -Name GitHub -SourceLocation https://nuget.pkg.github.com/mycompany -PublishLocation https://nuget.pkg.github.com/mycompany -Credential $gitHubCredential
Register-PSRepository : The specified Uri 'https://nuget.pkg.github.com/mycompany' for parameter 'SourceLocation' is
an invalid Web Uri. Please ensure that it meets the Web Uri requirements.
At line:1 char:1
+ Register-PSRepository -Name GitHub -SourceLocation https://nuget.pkg. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (https://nuget.pkg.github.com/mycompany:String) [Register-PSRepository
   ], ArgumentException
    + FullyQualifiedErrorId : InvalidWebUri,Register-PSRepository

我在尝试一些不可能的事情吗?

4

3 回答 3

0

我自己已经研究了几天了。这些文档中的第 3 步需要注意的是 PowerShell 不支持 NuGet 提要/API v3。据我所知,GitHub 包注册表支持 NuGet 提要/API 的 v3,因此我担心目前无法实现。

于 2020-03-27T13:28:56.830 回答
0

我拼凑了来自多个来源的解决方案。关键是:

使用 PowerShellGet 的预发布版本

Install-Module -Name PowerShellGet -AllowPrerelease -Force

将您的模块部署到本地文件系统存储库

Register-PSResourceRepository -Name nuget-local -URL c:\Output\Publish\ # Needs to be an absolute path.
Publish-PSResource -Path ".\Output\$($ModuleVersion)\$($ModuleName)" -Repository "nuget-local"

使用 .Net 实用程序 gpr,将包发布到 GitHub NuGet 注册表。

dotnet tool install --global gpr --version 0.1.281
gpr push -k ${{ secrets.GITHUB_TOKEN }} .\publish\*.nupkg -r https://github.com/${{github.repository}}

此时,您已将 PowerShell 模块发布到 GitHub 注册表。

您可以再次使用预发布的 PowerShellGet 来安装包。

Register-PSResourceRepository -Name github -URL https://nuget.pkg.github.com/${{ env.ORGANIZATION }}/index.json -Trusted
$securePwd = ConvertTo-SecureString "${{ secrets.GITHUB_TOKEN }}" -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential ("${{ github.actor }}", $securePwd)
Install-PSResource -Name ${{env.MODULE_NAME}} -Repository github -Credential $credentials

我正在使用 GitHub Actions 来执行构建。应该可以将其适应本地设置。您需要用您的个人访问令牌替换对“secrets.GITHUB_TOKEN”的引用。

参考:

NuGet 注册表上的 Github 文档 https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-nuget-registry

在 GitHub 问题中,用户 cdhunt 建议使用本地注册表/gpr 解决 https://github.com/PowerShell/PowerShellGet/issues/163

于 2022-02-24T00:44:06.447 回答
-1

如果我理解正确

Powershell Find-Package 命令不适用于 nuget v3 包源

这将适用于 powershell 7。

于 2020-10-09T05:38:50.480 回答