How to Implement Automatic Versioning for NuGet Package Using GitHub Actions?
Hello everyone,
I’m currently working on automating the release process for my NuGet package and would like to implement automatic versioning using GitHub Actions. I've managed to set up a workflow that builds and publishes the package to both GitHub Packages and NuGet.org, but I'm struggling to add the automatic version increment functionality.
Here's my current workflow:
`
name: Publish to GitHub Packages
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup .NET Core SDK
uses: actions/setup-dotnet@v1
with:
dotnet-version: '8.0.x'
- name: Build solution
run: dotnet build Kayki.StringUtils.sln --configuration Release
- name: Run tests
run: dotnet test Kayki.StringUtilsTests/Kayki.StringUtilsTests.csproj --configuration Release
- name: Build package
run: |
cd Kayki.StringUtils
dotnet build -c Release -o out
- name: Publish to GitHub Packages
run: |
cd Kayki.StringUtils/out
dotnet nuget push *.nupkg --api-key ${{ secrets.PKG_TOKEN }} --source https://nuget.pkg.github.com/kaykiletieri/index.json --skip-duplicate
- name: Publish to NuGet.org
run: |
cd Kayki.StringUtils/out
dotnet nuget push *.nupkg --api-key ${{ secrets.PKG_NUGET_TOKEN }} --source https://api.nuget.org/v3/index.json --skip-duplicate
name: Publish to GitHub Packages
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup .NET Core SDK
uses: actions/setup-dotnet@v1
with:
dotnet-version: '8.0.x'
- name: Build solution
run: dotnet build Kayki.StringUtils.sln --configuration Release
- name: Run tests
run: dotnet test Kayki.StringUtilsTests/Kayki.StringUtilsTests.csproj --configuration Release
- name: Build package
run: |
cd Kayki.StringUtils
dotnet build -c Release -o out
- name: Publish to GitHub Packages
run: |
cd Kayki.StringUtils/out
dotnet nuget push *.nupkg --api-key ${{ secrets.PKG_TOKEN }} --source https://nuget.pkg.github.com/kaykiletieri/index.json --skip-duplicate
- name: Publish to NuGet.org
run: |
cd Kayki.StringUtils/out
dotnet nuget push *.nupkg --api-key ${{ secrets.PKG_NUGET_TOKEN }} --source https://api.nuget.org/v3/index.json --skip-duplicate
1 Reply
i did something like this a while ago where it appended a dev version to the current version, maybe it can help with your case
name: Publish NuGet Package
on:
push:
branches:
- dev
jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: '7.x'
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release
- name: Determine version
id: version
run: echo "::set-output name=version::0.$((${GITHUB_RUN_NUMBER} / 10)).$((${GITHUB_RUN_NUMBER} % 10))"
- name: Get Project Version
id: base_version
run: |
version=$(grep '<Version>' < path/to/.csproj | sed 's/.*<Version>\(.*\)<\/Version>/\1/')
echo "::set-output name=version::$version"
- name: Pack
run: dotnet pack --configuration Release /p:Version="${{ steps.base_version.outputs.version }}-dev${{ steps.version.outputs.version }}" --output nupkgs
- name: Publish to NuGet
run: dotnet nuget push nupkgs/*.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json
name: Publish NuGet Package
on:
push:
branches:
- dev
jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: '7.x'
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release
- name: Determine version
id: version
run: echo "::set-output name=version::0.$((${GITHUB_RUN_NUMBER} / 10)).$((${GITHUB_RUN_NUMBER} % 10))"
- name: Get Project Version
id: base_version
run: |
version=$(grep '<Version>' < path/to/.csproj | sed 's/.*<Version>\(.*\)<\/Version>/\1/')
echo "::set-output name=version::$version"
- name: Pack
run: dotnet pack --configuration Release /p:Version="${{ steps.base_version.outputs.version }}-dev${{ steps.version.outputs.version }}" --output nupkgs
- name: Publish to NuGet
run: dotnet nuget push nupkgs/*.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json