Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How to Create and Publish a NuGet Package to GitLab Package Registry

If you"re working with PowerShell modules and need an efficient way to distribute them, using the GitLab Package Registry is a great option. In this guide, we"ll walk through the steps to create, package, publish, and install a PowerShell module using NuGet and GitLab.

The PowerShell module in this example provides a simple function to display the hostname of the current machine.

Context: This guide assumes you are using a self-managed GitLab Community Edition instance (v17.1.1), PowerShell Core Edition 7.5.0, and nuget.exe CLI version 6.13.2.


Creating the NuGet Package

Step 1: Create the Manifest

First, we need to create a module manifest that defines the properties of our PowerShell module. Run the following command:

New-ModuleManifest -Path Show-Hostname.psd1 -RootModule Show-Hostname.psm1 -ModuleVersion "1.0.0" -Author "Simon Bouvet" -Description "A simple PowerShell module to display the hostname"

Step 2: Create the Package

Once the module files are ready, use nuget.exe to package them:

nuget.exe pack Show-Hostname.nuspec

This command generates a .nupkg file, which we can now publish to GitLab.


Publishing the NuGet Package to GitLab

Step 1: Add GitLab as a NuGet Source

Before publishing, we need to register GitLab as a NuGet source:

nuget.exe source Add -Name "GitLab" -Source "https://<your_gitlab_instance_fqdn>/api/v4/projects/<your_project_id>/packages/nuget/index.json"

Documentation

Step 2: Publish the Package

To push the package to GitLab, run:

nuget.exe push Show-Hostname.1.0.0.nupkg -Source "GitLab" -ApiKey "<GITLAB_TOKEN>"

Replace <GITLAB_TOKEN> with your personal access token from GitLab.

Documentation


Configuring PowerShell to Use the GitLab NuGet Package Registry

Step 1: Allow anyone to pull GitLab NuGet Package Registry

documentation

Step 2: Register GitLab as a PowerShell Repository

To make PowerShell recognize our GitLab NuGet repository, register it with:

Register-PSResourceRepository -Name "GitLabNuget" -Uri "https://<your_gitlab_instance_fqdn>/api/v4/projects/<your_project_id>/packages/nuget/index.json" -Trusted

Verify the registration:

Get-PSResourceRepository

You should see an entry for GitLabNuget.

Step 3: Find the Published Module

To confirm the module is available, run:

Find-PSResource -Name Show-Hostname -Repository "GitLabNuget"

Installing and Using the Module

Step 1: Install the Module

To install the module from GitLab"s NuGet registry, use:

install-PSResource -Name Show-Hostname -Repository "GitLabNuget"

Step 2: Import the Module (if needed)

After installation, check where the module is stored:

(get-module -Name Show-Hostname).Path

If the module"s path is not in $env:PSModulePath, manually import it:

import-Module -Name Show-Hostname

Step 3: Run the Module Function

Test the module by running:

Show-Hostname

Expected output:

Hostname: mypc

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages