Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use GitHub package registry as a PowerShell package repository

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?

like image 669
Evren Kuzucuoglu Avatar asked Jan 31 '20 17:01

Evren Kuzucuoglu


People also ask

How do I get packages from GitHub?

You can search on GitHub.com to find packages in GitHub Packages that you can install in your own project. For more information, see "Searching GitHub Packages for packages." After you find a package, you can read the package's description and installation and usage instructions on the package page.


2 Answers

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.

like image 106
alastairs Avatar answered Oct 03 '22 14:10

alastairs


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

like image 41
Will Etson Avatar answered Oct 03 '22 15:10

Will Etson