Skip to content

Publish PowerShell Module

Publish to GitHub Packages as a NuGet Package

Pre-Requisites:

  1. Install the latest BETA version of PowerShellGet from the PowerShell Gallery:
# Ensuring PowerShellGet stable is latest version
Install-Module -Name PowerShellGet -Force -AllowClobber
# Installing PowerShellGet 3 Prerelease
Install-Module -Name PowerShellGet -RequiredVersion 3.0.16-beta16 -AllowPrerelease -Force -Repository PSGallery -SkipPublisherCheck
  1. Install the NuGet CLI
winget install Microsoft.NuGet
  1. Install the gpr tool:
dotnet tool install --global gpr
  1. Create a Personal Access Token with the read:packages and write:packages scopes.
gh secret set --user GHPKG_TOKEN -b $ENV:GHPKG_TOKEN

Workflow

  1. Create a "local" Nuget Repository:
New-Item -Path $LocalRepoPath -Type Directory | Out-Null
Register-PSResourceRepository -Name "LocalRepo" -Uri $LocalRepoPath
  1. Publish the module to the local repository as a PSResource:
Publish-PSResource -Path "./*.psd1" -Repository "LocalRepo"
  1. Using gpr, publish to GitHub Packages for your repository:
$NupkgPath = Get-ChildItem -Path $LocalRepoPath -Include "*.nupkg" -Recurse
gpr push -k $Env:GHPKG_TOKEN $NupkgPath -r "https://github.com/<user>/<repo>"
Publish-PSResource -Path <path/to/module> -Repository "PSGallery" -ApiKey $Env:NUGET_API_TOKEN

or using the build.ps1 script:

# setup API key
$apiKey = $Env:NUGET_API_TOKEN | ConvertTo-SecureString -AsPlainText -Force
$cred = [pscredential]::new('apikey', $apiKey)
# run build "Publish" task
./build.ps1 -Publish -PSGalleryApiKey $cred -Bootstrap