Use GitHub package registry as a PowerShell package repository
Asked Answered
C

3

10

As far as I know a PowerShell repository is a NuGet repository...

GitHub just released their package registry, which my company currently uses for npm, but also has an endpoint for NuGet.

I can access the NuGet endpoint (https://nuget.pkg.github.com/mycompany/index.json) with my GitHub credential, which returns a valid 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."
        }
      ]
    }

I've been trying to use this to set it up as a repo on my local machine (before I'd ideally push modules on my CI/CD and make them available for people to Install-Moduleusing GitHub as a repo):

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

Am I trying something impossible?

Cleanthes answered 31/1, 2020 at 17:19 Comment(0)
G
5

I've cobbled together a solution from multiple sources. The keys are:

Using the pre-release version of PowerShellGet

Install-Module -Name PowerShellGet -AllowPrerelease -Force

Deploy your module to a local filesystem repository

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

Using the .Net utility gpr, publish the package to the GitHub NuGet registry.

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

At this point you have published your PowerShell Module to a GitHub registry.

You can use the pre-release PowerShellGet again to install the package.

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

I'm using GitHub Actions to execute the build. It should be possible to adapt this to a local set up. You will need to replace references to "secrets.GITHUB_TOKEN" with your Personal Access Token.

References:

Github Documentation on the NuGet registry https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-nuget-registry

In a GitHub Issue, user cdhunt proposed the local registry/gpr work around https://github.com/PowerShell/PowerShellGet/issues/163

Gunrunning answered 24/2, 2022 at 0:44 Comment(0)
C
1

I've been looking into this myself for a couple of days now. Something to note from step 3 in these docs is that PowerShell does not support v3 of the NuGet feed/API. As far as I can tell, GitHub Package registry only supports v3 of the NuGet feed/API, and so I fear this is currently impossible to achieve.

Connect answered 27/3, 2020 at 13:28 Comment(2)
Actually, looking into this further, it seems that PowerShell 7 does support NuGet v3: the Publish-Module cmdlet delegates to the dotnet CLI. However, it still doesn't support GitHub Package Registry, as there's no way to specify the required repository data.Connect
What if you provided a nuget.conf file specifying a source? The source @WillEtson linked to above seems to indicate that this could work: docs.github.com/en/packages/…Cochrane
S
-1

If I understand this correctly

Powershell Find-Package command doesn't work with nuget v3 package source

this would work with powershell 7.

Savoury answered 9/10, 2020 at 5:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.