Skip to content

Commit 26cb9df

Browse files
authored
Add GitHub Actions workflow for NuGet publishing (#3)
Introduces a workflow to build, test, pack, and publish NuGet packages on pushes to main or via manual dispatch. Supports version override and handles API key securely for publishing to nuget.org. Adds symbol package creation and conditional push to the publish workflow. Ensures .snupkg files are only pushed if present, and refines version extraction logic for robustness.
1 parent f276907 commit 26cb9df

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

.github/workflows/publish.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: publish-nuget
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
workflow_dispatch:
7+
inputs:
8+
version:
9+
description: 'Override version (optional, e.g. 0.1.2)'
10+
required: false
11+
type: string
12+
13+
jobs:
14+
pack-and-push:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- uses: actions/setup-dotnet@v4
20+
with:
21+
dotnet-version: '9.0.x'
22+
23+
- name: Restore
24+
run: dotnet restore
25+
26+
- name: Build
27+
run: dotnet build -c Release
28+
29+
- name: Test
30+
run: dotnet test -c Release --logger trx
31+
32+
- name: Resolve version
33+
id: ver
34+
shell: bash
35+
run: |
36+
if [ -n "${{ github.event.inputs.version }}" ]; then
37+
PKGVER="${{ github.event.inputs.version }}"
38+
else
39+
FILE=$(git ls-files '*.csproj' | head -n1)
40+
PKGVER=$(grep -oPm1 '(?<=<Version>)[^<]+' "$FILE" || true)
41+
if [ -z "$PKGVER" ]; then echo "Version not found in $FILE"; exit 1; fi
42+
fi
43+
echo "PKGVER=$PKGVER" >> $GITHUB_ENV
44+
echo "Using version: $PKGVER"
45+
46+
47+
- name: Pack
48+
run: dotnet pack -c Release -p:Version=${PKGVER} -o ./artifacts -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg
49+
50+
- name: Push to nuget.org
51+
env:
52+
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
53+
run: |
54+
if [ -z "$NUGET_API_KEY" ]; then
55+
echo "NUGET_API_KEY secret is not set."; exit 1
56+
fi
57+
dotnet nuget push "./artifacts/*.nupkg" --skip-duplicate --api-key "$NUGET_API_KEY" --source https://api.nuget.org/v3/index.json
58+
if ls ./artifacts/*.snupkg 1> /dev/null 2>&1; then
59+
dotnet nuget push "./artifacts/*.snupkg" --skip-duplicate --api-key "$NUGET_API_KEY" --source https://api.nuget.org/v3/index.json
60+
else
61+
echo "No symbol packages (.snupkg) found to push."
62+
fi

0 commit comments

Comments
 (0)