For information how to set up a private Chocolatey repository on VSTS check this post
Let me start by saying that I really like VSTS package management. Earlier I blogged about Release Views and I also like the integration with the whole build/release eco system. That said, I was quite annoyed when I noticed that I could not publish my Powershell Modules (which are also NuGet packages) to VSTS Package Management using the default Publish-Module cmdlet from Powershell.
Since the uservoice request https://visualstudio.uservoice.com/forums/330519-team-services/suggestions/17505808-add-powershellget-support-for-packagemanagement is Under Investigation I assumed it is not possible.
But, after some digging and searching I found a way to make it possible and to use VSTS Package Management as your private Powershell Gallery. This is great, because this is THE enabler for distributing powershell script amongst my team but also within my builds that build from different repositories.
Let me show you how to do it.
Create a Package Feed in VSTS
To use packages in VSTS you need a feed. Follow these steps to create a new feed
https://www.visualstudio.com/en-us/docs/package/feeds/create-feed
Install NuGet and the VSTS Credential Provider
To connect securely to your VSTS Package repository you need to authenticate. NuGet has now way of storing credentials. Therefore you need an extra tool called the CredentialProvider that can store your credentials for you.
Follow the steps described here https://www.visualstudio.com/en-us/docs/package/nuget/publish to download NuGet and the credential provider. Unzip the package, and place the location of the files in your PATH.
WAIT! with executing step 2 and 3 (registering repo + push package)
Create a Powershell Module, manifest and Nuspec file
First you need to create a Powershell Module. This is a PSM1 file. To describe the Powershell Module you need a manifest with some metadata. You can put that in a PSD1 file. For an example, please use the Git Repo that accompanies this post.
Move to the directory where you created the psm1 and psd1 file and type
nuget spec roadtoalmUtils
You will see a the file roadtoalmUtils.nuspec appear. Update the information in this file to reflect your metadata. Make sure your version is the same in the PSD1 and Nuspec file
Register the VSTS Package repository
This is an important part! When you downloaded the NuGet + CredentialProvider, the next step is to register the repository as source within NuGet. When you open the feed you can see the “Connect to feed” button.
The url that shows looks like this
https://x.pkgs.visualstudio.com/_packaging/vstsgallery/nuget/v3/index.json
When you use this URL, it is not going to work. You need to transform it to a NuGet 2.x feed. You can do this by replacing /v3/index.json with /v2 (see here)
Use this feed url to register your source in NuGet
nuget.exe sources Add -Name "vstsgallery" -Source "https://x.pkgs.visualstudio.com/_packaging/vstsgallery/nuget/v2"
Push your package to VSTS
Now you can push your package to this repo by using
nuget.exe push -Source "vstsgallery" -ApiKey VSTS roadtoalmUtils.1.3.0.nupkg
You can see it appear in your feed
Find, Install and use your package from OneGet
Now that you have published your module to the Package Repo, you can use it in Powershell. To connect securely you need your VSTS user account (email) and Personal Access Token (See here how to generate one)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$password = ConvertTo-SecureString 'PAT' –AsPlainText –Force | |
$vstsCredential = New-Object System.Management.Automation.PSCredential 'vstsuseraccount-email', $password | |
Find-Module –Name roadtoalmUtils –Repository VSTSGallery –Credential $vstsCredential | |
install-Module –Name roadtoalmUtils –Repository VSTSGallery –Credential $vstsCredential | |
Get-Module –ListAvailable –Name roadtoalmUtils |
That’s it. You can now use your VSTS as a fully functional Powershell Gallery !
Trackbacks/Pingbacks
[…] For information how to set up a private Powershell repository on VSTS check this post […]