- Workflow now triggers on tag pushes in addition to branch pushes - Tags must match pattern: [0-9]+.[0-9]+.[0-9]+ (e.g., 0.43.0) - Updated documentation to explain both triggering methods - Allows publishing by pushing tags directly without requiring branch push Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
5.7 KiB
NuGet Release Workflow
This document describes the automated NuGet release workflow for SharpCompress.
Overview
The nuget-release.yml workflow automatically builds, tests, and publishes SharpCompress packages to NuGet.org when:
- Changes are pushed to the
masterorreleasebranch - A version tag (format:
MAJOR.MINOR.PATCH) is pushed
The workflow runs on both Windows and Ubuntu, but only the Windows build publishes to NuGet.
How It Works
Version Determination
The workflow automatically determines the version based on whether the commit is tagged using C# code in the build project:
-
Tagged Release (Stable):
- If the current commit has a version tag (e.g.,
0.42.1) - Uses the tag as the version number
- Published as a stable release
- If the current commit has a version tag (e.g.,
-
Untagged Release (Prerelease):
- If the current commit is NOT tagged
- Creates a prerelease version based on the next minor version
- Format:
{NEXT_MINOR_VERSION}-beta.{COMMIT_COUNT} - Example:
0.43.0-beta.123(if last tag is 0.42.x) - Published as a prerelease to NuGet.org (Windows build only)
Workflow Steps
The workflow runs on a matrix of operating systems (Windows and Ubuntu):
- Checkout: Fetches the repository with full history for version detection
- Setup .NET: Installs .NET 10.0
- Determine Version: Runs
determine-versionbuild target to check for tags and determine version - Update Version: Runs
update-versionbuild target to update the version in the project file - Build and Test: Runs the full build and test suite on both platforms
- Upload Artifacts: Uploads the generated
.nupkgfiles as workflow artifacts (separate for each OS) - Push to NuGet: (Windows only) Runs
push-to-nugetbuild target to publish the package to NuGet.org using the API key
All version detection, file updates, and publishing logic is implemented in C# in the build/Program.cs file using build targets.
Setup Requirements
1. NuGet API Key Secret
The workflow requires a NUGET_API_KEY secret to be configured in the repository settings:
- Go to https://www.nuget.org/account/apikeys
- Create a new API key with "Push" permission for the SharpCompress package
- In GitHub, go to: Settings → Secrets and variables → Actions
- Create a new secret named
NUGET_API_KEYwith the API key value
2. Branch Protection (Recommended)
Consider enabling branch protection rules for the release branch to ensure:
- Code reviews are required before merging
- Status checks pass before merging
- Only authorized users can push to the branch
Usage
Creating a Stable Release
There are two ways to trigger a stable release:
Method 1: Push tag to trigger workflow
- Ensure all changes are committed on the
masterorreleasebranch - Create and push a version tag:
git checkout master # or release git tag 0.43.0 git push origin 0.43.0 - The workflow will automatically trigger, build, test, and publish
SharpCompress 0.43.0to NuGet.org (Windows build)
Method 2: Tag after pushing to branch
- Ensure all changes are merged and pushed to the
masterorreleasebranch - Create and push a version tag on the already-pushed commit:
git checkout master # or release git tag 0.43.0 git push origin 0.43.0 - The workflow will automatically trigger, build, test, and publish
SharpCompress 0.43.0to NuGet.org (Windows build)
Creating a Prerelease
- Push changes to the
masterorreleasebranch without tagging:git checkout master # or release git push origin master # or release - The workflow will automatically:
- Build and test the project on both Windows and Ubuntu
- Publish a prerelease version like
0.43.0-beta.456to NuGet.org (Windows build)
Troubleshooting
Workflow Fails to Push to NuGet
- Check the API Key: Ensure
NUGET_API_KEYis set correctly in repository secrets - Check API Key Permissions: Verify the API key has "Push" permission for SharpCompress
- Check API Key Expiration: NuGet API keys may expire; create a new one if needed
Version Conflict
If you see "Package already exists" errors:
- The workflow uses
--skip-duplicateflag to handle this gracefully - If you need to republish the same version, delete it from NuGet.org first (if allowed)
Build or Test Failures
- The workflow will not push to NuGet if build or tests fail
- Check the workflow logs in GitHub Actions for details
- Fix the issues and push again
Manual Package Creation
If you need to create a package manually without publishing:
dotnet run --project build/build.csproj -- publish
The package will be created in the artifacts/ directory.
Build Targets
The workflow uses the following C# build targets defined in build/Program.cs:
- determine-version: Detects version from git tags and outputs VERSION and PRERELEASE variables
- update-version: Updates VersionPrefix, AssemblyVersion, and FileVersion in the project file
- push-to-nuget: Pushes the generated NuGet packages to NuGet.org (requires NUGET_API_KEY)
These targets can be run manually for testing:
# Determine the version
dotnet run --project build/build.csproj -- determine-version
# Update version in project file
VERSION=0.43.0 dotnet run --project build/build.csproj -- update-version
# Push to NuGet (requires NUGET_API_KEY environment variable)
NUGET_API_KEY=your-key dotnet run --project build/build.csproj -- push-to-nuget
Related Files
.github/workflows/nuget-release.yml- The workflow definitionbuild/Program.cs- Build script with version detection and publishing logicsrc/SharpCompress/SharpCompress.csproj- Project file with version information