Gitlab as PSRepository

Ricardo Martin | May 31, 2024 min read

This guide assumes you already have access to a gitlab project

Project Settings

If you have not activated the “Package registry” feature for your project in Settings > General > Visibility,project features, permissions, you’ll receive a 403 Forbidden response. Accessing package registry via deploy token is not available when external authorization is enabled.

In order to allow anyone to pull from the Package Registry, toggle Allow anyone to pull from Package Registry as shown below.

Gitlab Settings

Create Token

To be able to push items to the registry, an API token must be created Go to Settings > Access Tokens -> Add a new token with api permissions

Access Tokens

Save the token in a password manager or gitlab secrets

Setup Powershell Reository

Get Project ID

Go to Settings > General and get the project ID

Project ID

The repository URL will follow the following format

"https://gitlab.example.com/api/v4/projects/<Project ID>/packages/nuget/index.json"

In this case our project ID is 54 so the url will be as follows:

"https://gitlab.example.com/api/v4/projects/54/packages/nuget/index.json"

Register the PS Repository

To make this step work, make sure to allow all to retrieve from the registry as metioned in the Project Settings section.

Open powershell and register the gitlab repository:

Register-PSRepository -Name "gitlab" -SourceLocation "https://gitlab.example.com/api/v4/projects/54/packages/nuget/index.json" -PublishLocation "https://gitlab.example.com/api/v4/projects/54/packages/nuget/index.json" -InstallationPolicy "Trusted"

Pusblish a Module

Now that we have registered the repository, we can publish modules or nuget packages.

To publish the module, point the command Publish-Module to gitlab and use the Access Token as the Nuget API Key.

Publish-Module -Name "MyModule" -Repository "gitlab" -NuGetApiKey "<Your Token>"

List all Modules

To list all modules, just point the command Find-Module to the gilab repository

Find-Module -Repository "gitlab"

Install Module

To install a module from the repository, point the command Install-Moduleto the gilab repository

Install-Module "MyModule" -Repository "gitlab"

Final Notes

That’s it!

Now you should be able to store powershell modules on your private repository.