JandaBox NuGet Class Library - Jandini/JandaBox GitHub Wiki

Create NuGet package and push to GitHub/NuGet.org package registry.

  • Create new GitHub repository.

  • Create GitHub access tokens and/or API key in NuGet.org and save it as secrets in the new repository.

  • Create new Project from the template and push it to GitHub.

    dotnet new nugetbox -n MyNuGet --actions --user Jandini --gitVersion
    cd MyNuGet
    git init -b main
    git add .
    git commit -m "First commit"
    git remote add origin https://github.com/Jandini/MyNuGet.git
    git push -u origin main
    

At this point GitHub actions will create NuGet package and push it to GitHub private package registry. The package is created and pushed only for main branch.

  • A new tag will trigger NuGet pipeline which will create and push release package to GitHub or NuGet.org. Use --tagNugetOrg parameter to push release package to nuget.org. Nuget.org API key is required in repos' secrets.

Read more on how to use GitHub NuGet registry in GitHub actions here https://docs.github.com/en/actions/security-guides/automatic-token-authentication

Step by step

Following step can be skipped if you have safely stored your PAT which can be re-used.

  • Create Personal Access Token (PAT) with write:packages permissions. This will allow to push NuGet packages into GitHub package registry.
    • Go to https://github.com/settings/tokens and from drop down "Generate new token" select "Generate new token (classic)" or go directly to https://github.com/settings/tokens/new
    • Set "Note" to anything you want. Usually it should reflect purpose of the token.
    • Select checkbox "write:packages" to allow upload packages to GitHub Package Registry.
    • Click "Generate token"
    • Copy the new token to clipboard. Note: you will see the token only once.
    • Add token to repository secrets as PACKAGE_REGISTRY_TOKEN.

Following steps explains how create API key in https://www.nuget.org/. If you are planning to push your release packages to nuget.org then you must use --tagNugetOrg parameter when creating project from template and perform following steps.

  • Go to https://www.nuget.org/ and login to your account.

  • Under "API Keys" create new token or go directly to https://www.nuget.org/account/apikeys

    • Specify "Key Name"
    • You can specify pattern name for your NuGet package or use * to allow push for any package
    • Click "Create"
    • Copy the Key into clipboard
  • Add NuGet API Key to Secrets in GitHub repository.

    • Go To "Actions secrets and variables"

      • Open repository Setting
      • In the left hand side, under Security select Secrets and variables | Actions
    • Click "New repository secret"

    • Paste your NuGet API Key into "Secret*" field

    • Secret's "Name" must be set to NUGET_ORG_API_KEY. This name is used in nuget.yml file.

    • Click "Add secret"

  • Create new project from nugetbox template.

    • Give it a name -n MyNuGet
    • Add GitHub Actions --actions
    • Provide your GitHub user name --user Jandini
    • Add versioning with GitVersoin --gitVersion
    dotnet new nugetbox -n MyNuGet --actions --user Jandini --gitVersion
    
  • Initialize local git repository and push it to GitHub

    cd MyNuGet
    git init -b main
    git add .
    git commit -m "First commit"
    git remote add origin https://github.com/Jandini/MyNuGet.git
    git push -u origin main
    

That's all. Your NuGet package will be waiting in GitHub registry !

Template options
  • --nuget Add properties to project file required to build and push NuGet package. Default value is true. Use false to create simple class library.
  • --user Specify GitHub user name to update links in project file properties and GitHub action badge links in README.md file.
  • --actions Add GitHub Actions pipeline files for building and pushing NuGet packages. Build pipeline creates NuGet package and push it to private GitHub packages only form main branch. NuGet pipeline creates NuGet package and push it to NuGet.org package registry. Default value is false.
  • --tagNugetOrg Add GitHub action file to push tagged NuGet packages to NuGet.org registry. By default tagged packages are pushed into of GitHub package registry.
  • --gitVersion Add semantic versioning with GitVersion. The code created with this parameter can be only build from initialized git repository.
  • --license Add MIT license file to the NuGet package with authors provided in --authors parameter.
  • --authors Add NuGet package authors. The authors will be written to package properties and license file.
Template features
  • Repository Layout
    • The src and bin folders
    • Default README.md file
    • Default .gitignore file
  • NuGet configuration
    • Build NuGet package with only dotnet pack
  • GitHub Actions
    • Restore packages from private GitHub package registry.
    • Pack library into NuGet package.
    • Push NuGet package created from main branch into NuGet package registry.
    • Push Tagged package into NuGet.org or GitHub package registry.
  • Update links in project properties RepositoryUrl PackageProjectUrl and badge links in README.md files.
  • Add files into the NuGet package
    • README.md
    • MIT LICENSE with given authors
    • Icon file