Publishing a NuGet package to nuget.org can be automated using GitHub Actions, which is a powerful tool for continuous integration and continuous deployment (CI/CD). This guide will walk you through the steps required to publish your NuGet package automatically every time you push a code update to a specific branch in your GitHub repository.
Prerequisites
- GitHub Repository: Make sure you have a repository on GitHub containing a .NET project that you want to package and publish.
- NuGet Account: You need an account on NuGet.org.
- GitHub Actions Access: Ensure that you have permissions to configure GitHub Actions for the repository.
Step 1: Obtain a NuGet API Key
Before setting up the GitHub Actions workflow, you need to create an API key on nuget.org that will be used to authenticate and push the package.
- Go to NuGet.org and sign in.
- Navigate to your account settings.
- Under API Keys, create a new key:
- Select Scopes: Allow the API key to Push new packages and package versions.
- Expiration: Set an appropriate expiration date based on your security preferences.
- Copy the generated API key. You will use this to configure GitHub Actions.
Step 2: Add the NuGet API Key to GitHub Secrets
To securely store your NuGet API key, add it to your GitHub repository’s secrets.
- In your GitHub repository, navigate to Settings.
- Under Secrets and variables, click on Actions.
- Click on New repository secret.
- Name the secret
NUGET_API_KEY. - Paste the NuGet API key that you copied earlier.
Step 3: Create the GitHub Actions Workflow File
Now, let’s create a workflow that will build the project, create the NuGet package, and publish it to nuget.org. You will create a file named .github/workflows/publish-nuget.yml in your repository.
- Create the Workflow File
In your GitHub repository, create a new file at.github/workflows/publish-nuget.yml. - Add the Following Content to the Workflow File
The following YAML file configures the GitHub Actions workflow:
name: Publish NuGet Package
on:
push:
branches:
- main
- 'release/*' # Trigger on any branch named 'release/*'
jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: '8.x' # Specify the .NET version you need
- name: Restore dependencies
run: dotnet restore
- name: Build the project
run: dotnet build --configuration Release --no-restore
- name: Pack the NuGet package
run: dotnet pack --configuration Release --no-build --output ./nupkg
- name: Publish NuGet package
run: dotnet nuget push ./nupkg/*.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json
env:
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
Step 4: Explanation of the Workflow Steps
- Trigger on Push: This workflow is triggered on any push to the
mainbranch or any branch that matches the patternrelease/*. You can customize the branches to your workflow requirements. - Checkout Code: The workflow uses
actions/checkout@v3to clone the repository, allowing GitHub Actions to access your code. - Setup .NET SDK: The
actions/setup-dotnet@v3action sets up the .NET SDK needed for the build. The specified version here is8.x, but you can adjust this according to your project’s requirements. - Restore Dependencies: The
dotnet restorecommand restores NuGet packages for the project. - Build the Project:
dotnet buildcompiles the project inReleaseconfiguration. The--no-restoreflag ensures that dependencies are not restored again. - Pack the NuGet Package: The
dotnet packcommand creates a.nupkgfile in the./nupkgdirectory. - Publish to NuGet.org: The
dotnet nuget pushcommand pushes the package tonuget.org. The API key is securely accessed from the GitHub secrets you set up earlier.
Step 5: Commit and Push the Workflow File
- Add the new workflow file to your repository using Git commands:
git add .github/workflows/publish-nuget.yml git commit -m "Add GitHub Actions workflow for publishing to NuGet" git push origin main
- Once pushed, any future commits to the
mainbranch (or the specified release branches) will trigger the workflow and attempt to publish the NuGet package.
Step 6: Test the Workflow
To verify that the workflow is functioning correctly:
- Make a minor change to your code and push it to the
mainbranch or arelease/*branch. - Go to the Actions tab in your GitHub repository.
- Monitor the workflow run. It should go through the steps and publish the package if everything is configured correctly.
Troubleshooting Tips
- Failed Builds: If the workflow fails at the build step, make sure your project builds locally using the
dotnet buildcommand. - Publishing Errors: If the package cannot be published, ensure the API key is valid and has the correct permissions.
- Version Conflicts: Make sure your NuGet package version is incremented for each push. NuGet does not allow overwriting an existing package with the same version.
Conclusion
Automating the publishing of your NuGet package using GitHub Actions not only saves time but also ensures a consistent CI/CD process. By following these steps, you can set up a reliable workflow that manages the creation and deployment of your NuGet packages to nuget.org.
With GitHub Actions, your software release process becomes more efficient, allowing you to focus on improving your code rather than worrying about manual deployment steps.