Skip to content

Commit 55c7337

Browse files
committed
ops: add release-nuget.yml to build, test, pack and publish to NuGet.org
1 parent de00442 commit 55c7337

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
name: Release NuGet Packages
2+
3+
on:
4+
release:
5+
types: [published]
6+
workflow_dispatch:
7+
inputs:
8+
version:
9+
description: "Override package version (e.g. 0.1.0 or 0.1.0-beta.2). If empty, uses tag or csproj version"
10+
required: false
11+
type: string
12+
publish:
13+
description: "Push to NuGet.org"
14+
required: false
15+
default: true
16+
type: boolean
17+
18+
permissions:
19+
contents: read
20+
21+
jobs:
22+
build-pack-publish:
23+
name: Build, Test, Pack, Publish
24+
runs-on: ubuntu-latest
25+
env:
26+
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
27+
DOTNET_NOLOGO: true
28+
steps:
29+
- name: Checkout
30+
uses: actions/checkout@v4
31+
32+
- name: Setup .NET SDKs
33+
uses: actions/setup-dotnet@v4
34+
with:
35+
dotnet-version: |
36+
9.0.x
37+
8.0.x
38+
39+
- name: Show dotnet info
40+
run: dotnet --info
41+
42+
- name: Restore
43+
run: dotnet restore Cachula.sln
44+
45+
- name: Build (Release)
46+
run: dotnet build Cachula.sln -c Release --no-restore
47+
48+
- name: Test
49+
run: |
50+
dotnet test tests/Cachula.Tests/Cachula.Tests.csproj -c Release --no-build --logger "trx;LogFileName=Cachula.Tests.trx"
51+
dotnet test tests/Cachula.Redis.Tests/Cachula.Redis.Tests.csproj -c Release --no-build --logger "trx;LogFileName=Cachula.Redis.Tests.trx"
52+
53+
- name: Determine package version
54+
id: version
55+
shell: bash
56+
run: |
57+
INPUT_VER='${{ github.event.inputs.version || '' }}'
58+
EVENT='${{ github.event_name }}'
59+
TAG=''
60+
if [ "$EVENT" = "release" ]; then
61+
TAG='${{ github.event.release.tag_name }}'
62+
elif [ "$EVENT" = "workflow_dispatch" ] && [ -n "$INPUT_VER" ]; then
63+
# Use provided version input
64+
echo "PKG_VERSION=$INPUT_VER" >> $GITHUB_ENV
65+
echo "version=$INPUT_VER" >> $GITHUB_OUTPUT
66+
exit 0
67+
fi
68+
if [ -n "$TAG" ]; then
69+
# strip leading v or V (e.g., v1.2.3 -> 1.2.3)
70+
CLEAN_TAG=${TAG#v}
71+
CLEAN_TAG=${CLEAN_TAG#V}
72+
echo "PKG_VERSION=$CLEAN_TAG" >> $GITHUB_ENV
73+
echo "version=$CLEAN_TAG" >> $GITHUB_OUTPUT
74+
else
75+
echo "No explicit version; will use versions from csproj files"
76+
fi
77+
78+
- name: Pack Cachula
79+
run: |
80+
if [ -n "$PKG_VERSION" ]; then
81+
dotnet pack src/Cachula/Cachula.csproj -c Release -o out -p:ContinuousIntegrationBuild=true -p:Version=$PKG_VERSION
82+
else
83+
dotnet pack src/Cachula/Cachula.csproj -c Release -o out -p:ContinuousIntegrationBuild=true
84+
fi
85+
86+
- name: Pack Cachula.Redis
87+
run: |
88+
if [ -n "$PKG_VERSION" ]; then
89+
dotnet pack src/Cachula.Redis/Cachula.Redis.csproj -c Release -o out -p:ContinuousIntegrationBuild=true -p:Version=$PKG_VERSION
90+
else
91+
dotnet pack src/Cachula.Redis/Cachula.Redis.csproj -c Release -o out -p:ContinuousIntegrationBuild=true
92+
fi
93+
94+
- name: Upload packages artifact
95+
uses: actions/upload-artifact@v4
96+
with:
97+
name: nuget-packages
98+
path: |
99+
out/*.nupkg
100+
out/*.snupkg
101+
102+
- name: Publish to NuGet.org
103+
if: >-
104+
${{ (github.event_name == 'release') || (github.event_name == 'workflow_dispatch' && inputs.publish == true) }}
105+
env:
106+
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
107+
run: |
108+
if [ -z "$NUGET_API_KEY" ]; then
109+
echo "NUGET_API_KEY secret is missing" >&2
110+
exit 1
111+
fi
112+
dotnet nuget push "out/*.nupkg" --api-key "$NUGET_API_KEY" --source https://api.nuget.org/v3/index.json --skip-duplicate

0 commit comments

Comments
 (0)