Publish PowerShell Module to private GitHub package repo and download

36 Views Asked by At

I am using GitHub to store my private PowerShell repo's, and wanted to make use of the GH Packages system. My understanding is that PowerShell modules can be pushed to GitHub Packages and then downloaded on any machine via normal 'install-module' commands?

Each of my PS modules have a .psm1 file with the functions in it and a .psd1 manifest file (with version number/author/functions to export etc).

I have added the NuGet repo to powershell using the following commands :

$username = '<My-GH-User>'
$token = '<My-GH-Token>'
$sourceName = 'GitHub'
$source = "https://nuget.pkg.github.com/$username/index.json"
nuget sources Add -Name $sourceName -Source $source -UserName $username -Password $token
$creds = New-Object System.Management.Automation.PSCredential -ArgumentList $username, (ConvertTo-SecureString -AsPlainText $token -Force)
# Register-PSRepository -Name $sourceName -SourceLocation $source -PublishLocation $source -Credential $creds

The above comes back with :

WARNING: Unable to resolve package source 'https://nuget.pkg.github.com/<$username>/index.json'.

But it does show the repo when i run Get-PSrepository

Name                      InstallationPolicy   SourceLocation
----                      ------------------   --------------
PSGallery                 Trusted              https://www.powershellgallery.com/api/v2
GitHub                    Trusted              https://nuget.pkg.github.com/<$username>/index.json

I then tried to publish a module :

Publish-Module -Path .\Confirm-Command\ -Repository GitHub -NuGetApiKey '<$token>'

I do get a warning about the manifest file wanting FunctionsToExport updated, but this is due i believe to me having a '*' for the exported functions just now (there is only one function in this module)

The Confirm-Command package now shows in GitHub packages.

My main problem now appears to be trying to use the package :( If I run the following command I get an error :

Install-Module -Name Confirm-Command -Repository GitHub

WARNING: Unable to resolve package source 'https://nuget.pkg.github.com/<$username>/index.json'.
PackageManagement\Install-Package : No match was found for the specified search criteria and module name 'Confirm-Command'. Try Get-PSRepository to see all available registered module
repositories.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.5\PSModule.psm1:9711 char:34
+ ... talledPackages = PackageManagement\Install-Package @PSBoundParameters
+                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Microsoft.Power....InstallPackage:InstallPackage) [Install-Package], Exception
    + FullyQualifiedErrorId : NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage

I have also tried adding :

 -Credential $creds

With no change, running the command with -debug i can see entries like :

VERBOSE: Using the specified source names : 'GitHub'.
VERBOSE: Getting the provider object for the PackageManagement Provider 'NuGet'.
VERBOSE: The specified Location is 'https://nuget.pkg.github.com/<$username>/index.json' and PackageManagementProvider is 'NuGet'.
DEBUG: 00:00:27.9414797 PackageProvider::FindPackage with name Confirm-Command
DEBUG: 00:00:27.9478019 Calling 'NuGet'::'FindPackage' - name='Confirm-Command', requiredVersion='',minimumVersion='', maximumVersion='''.
DEBUG: 00:00:27.9478559 Iterating 'Confirm-Command'.
DEBUG: 00:00:27.9489494 There are '0' registered sources in 'NuGet' provider.
DEBUG: 00:00:27.9490166 Source 'https://nuget.pkg.github.com/<$username>/index.json' is not one of the registered sources in 'NuGet' provider.
WARNING: Unable to resolve package source 'https://nuget.pkg.github.com/<$username>/index.json'.

If i call Invoke-WebRequest with the index.json url, i get a 401 denied, if i add -credential $creds then i get json output to the console, but this doesn't have any text like 'Confirm-Command' in it.

From all of the above, I think the issue is the PS module is being pushed to GH, but then GH is not updating the index.json file. I am not sure if the install-module would work if that was fixed.

Ideally, I would like to run everything as a powershell command, something like :

Authenticate-ToGitHub
Publish-MyModuleToGitHub ModuleName ModulePath GitHubUser

And then just use

Install-Module -Name <ModuleName> -Repository GitHub

At a later date (probably MUCH later) I may try and move this into a GH Action, but that is likely to be a long time yet.

0

There are 0 best solutions below