Publishing nuget libraries to Github Packages using paket
If you’re reading this you are probably aware that you can host your packages on Github. There is plenty of documentation on how to publish to Github packages using Nuget but I have migrated all my libraries to Paket and I wanted to document it so that you can see how simple it is.
Personal Access Token
I’m going to assume that you know how to package a library and have one ready to publish. In order for this process to work you will need a Github Personal Access Token. When you are logged in to Github you can get this from your profile — Settings | Developer Settings | Personal Access Tokens. Create a new token ensuring it has the read:packages and write:packages scope and name it so that you will know what it’s for, you will be using this later.
Packages Url
The next step is to work out the url you need to push to. This is done using the following template
https://nuget.pkg.github.com/[OWNER]/index.json
where OWNER is your user name, or your organisation name. You can check the url by browsing to it, you should be asked for a username and password, the username will be your username and the password will be the personal access token, you should get a valid json response
The big push
The final step is to push using paket. One of the things that’s easy to overlook here, especially for new projects is that the library project file should have the RepositoryUrl specified —
<RepositoryUrl>https://github.com/[OWNER]/MyLibrary.git</RepositoryUrl>
When I first tried this it still wasn’t happy, but changing the PackageProjectUrl to the same url fixed that, meaning that all I need to do is -
paket push MyPackage.nupkg --url https://nuget.pkg.github.com/[OWNER] --api-key {myPersonalAccessToken]
and my package is now published.
Github have added a nice touch in that the package has the same visibility as your repo, so private repos have private packages, public repos have public packages.
Update Feb 2021
One of my libraries was randomly throwing errors when I tried to push it
Could not push MyPackage.0.5.0.nupkg: Request to 'https://nuget.pkg.github.com/owner' failed with: 400 BadRequest - {"error":"No destination repository detected. Ensure the source project has a 'RepositoryUrl' property defined. If you're using a nuspec file, ensure that it has a repository element with the required 'type' and 'url' attributes."}
I double checked the RepositoryUrl and it was definitely correct. After comparing with a library that was working it became clear that the clue was the type attribute mentioned above and the missing line was
<RepositoryType>git</RepositoryType>
I do find it very odd that this is needed in Github.