使用 GitHub 包注册表作为 PowerShell 包存储库

Evr*_*glu 6 powershell nuget powershell-module github-package-registry

据我所知,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."
        }
      ]
    }
Run Code Online (Sandbox Code Playgroud)

我一直在尝试使用它在我的本地机器上将其设置为存储库(在我理想地将模块推送到我的 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
Run Code Online (Sandbox Code Playgroud)

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

小智 5

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

使用 PowerShellGet 的预发行版本

Install-Module -Name PowerShellGet -AllowPrerelease -Force
Run Code Online (Sandbox Code Playgroud)

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

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

使用 .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}}
Run Code Online (Sandbox Code Playgroud)

此时,您已将 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
Run Code Online (Sandbox Code Playgroud)

我正在使用 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


ala*_*irs 1

我自己已经研究了几天了。这些文档中步骤 3 中需要注意的一点是,PowerShell 不支持 NuGet feed/API v3。据我所知,GitHub Package 注册表支持 NuGet feed/API v3,因此我担心目前这是不可能实现的。