Skip to content

Commit d1de0e8

Browse files
committed
Nuget publishing improvements.
1 parent df41fc9 commit d1de0e8

File tree

3 files changed

+86
-46
lines changed

3 files changed

+86
-46
lines changed

.github/workflows/ci.yml

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ jobs:
4747
needs: build
4848
runs-on: ubuntu-latest
4949
if: github.event_name == 'push' && (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/heads/release/') || startsWith(github.ref, 'refs/heads/hotfix/') || startsWith(github.ref, 'refs/heads/develop') || startsWith(github.ref, 'refs/tags/v'))
50+
permissions:
51+
contents: read
52+
id-token: write
5053

5154
steps:
5255
- name: Checkout
@@ -59,39 +62,16 @@ jobs:
5962
with:
6063
dotnet-version: "10.0.x"
6164

65+
- name: Install GitVersion
66+
uses: gittools/actions/gitversion/setup@v4
67+
with:
68+
versionSpec: '6.x'
69+
6270
- name: Determine version
6371
id: version
64-
run: |
65-
# Base version from tag or default
66-
if [[ "$GITHUB_REF" == refs/tags/v* ]]; then
67-
VERSION="${GITHUB_REF#refs/tags/v}"
68-
else
69-
# Auto-increment: use date-based pre-release for non-tag builds
70-
BASE_VERSION="1.3.0"
71-
RUN_NUMBER="${GITHUB_RUN_NUMBER}"
72-
BRANCH="${GITHUB_REF#refs/heads/}"
73-
74-
# Clean branch name for semver compatibility
75-
BRANCH_CLEAN=$(echo "$BRANCH" | sed 's|[/_]|.|g' | sed 's|\.\.\.||g')
76-
77-
if [[ "$GITHUB_REF" == "refs/heads/master" || "$GITHUB_REF" == "refs/heads/main" ]]; then
78-
# Stable release from master/main (only via tags ideally, but allow CI builds)
79-
VERSION="${BASE_VERSION}"
80-
elif [[ "$BRANCH" == release/* ]]; then
81-
VERSION="${BASE_VERSION}-rc.${RUN_NUMBER}"
82-
elif [[ "$BRANCH" == hotfix/* ]]; then
83-
VERSION="${BASE_VERSION}-hf.${RUN_NUMBER}"
84-
elif [[ "$BRANCH" == "develop" ]]; then
85-
VERSION="${BASE_VERSION}-preview.${RUN_NUMBER}"
86-
elif [[ "$BRANCH" == feature/* ]]; then
87-
VERSION="${BASE_VERSION}-${BRANCH_CLEAN}.${RUN_NUMBER}"
88-
else
89-
VERSION="${BASE_VERSION}-ci.${RUN_NUMBER}"
90-
fi
91-
fi
92-
93-
echo "version=${VERSION}" >> "$GITHUB_OUTPUT"
94-
echo "Resolved version: ${VERSION}"
72+
uses: gittools/actions/gitversion/execute@v4
73+
with:
74+
useConfigFile: true
9575

9676
- name: Restore
9777
run: dotnet restore EventHorizon.sln
@@ -101,7 +81,7 @@ jobs:
10181

10282
- name: Pack NuGet packages
10383
run: |
104-
VERSION="${{ steps.version.outputs.version }}"
84+
VERSION="${{ steps.version.outputs.semVer }}"
10585
for proj in src/EventHorizon.*/EventHorizon.*.csproj; do
10686
ASSEMBLY_NAME=$(basename "$(dirname "$proj")")
10787
PACKAGE_ID="Cts.${ASSEMBLY_NAME}"
@@ -115,19 +95,25 @@ jobs:
11595
-p:Version="${VERSION}"
11696
done
11797
98+
- name: NuGet login (trusted publishing)
99+
uses: NuGet/login@v1
100+
id: nuget-login
101+
with:
102+
user: potatman
103+
118104
- name: Push to NuGet.org
119105
if: github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')
120106
run: |
121107
dotnet nuget push "./artifacts/*.nupkg" \
122-
--api-key ${{ secrets.NUGET_API_KEY }} \
108+
--api-key ${{ steps.nuget-login.outputs.NUGET_API_KEY }} \
123109
--source https://api.nuget.org/v3/index.json \
124110
--skip-duplicate
125111
126112
- name: Push pre-release to NuGet.org
127113
if: startsWith(github.ref, 'refs/heads/release/') || startsWith(github.ref, 'refs/heads/hotfix/') || github.ref == 'refs/heads/develop'
128114
run: |
129115
dotnet nuget push "./artifacts/*.nupkg" \
130-
--api-key ${{ secrets.NUGET_API_KEY }} \
116+
--api-key ${{ steps.nuget-login.outputs.NUGET_API_KEY }} \
131117
--source https://api.nuget.org/v3/index.json \
132118
--skip-duplicate
133119

GitVersion.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
workflow: GitFlow/v1
2+
mode: ContinuousDeployment
3+
next-version: 1.3.0
4+
5+
branches:
6+
main:
7+
label: ''
8+
increment: Patch
9+
regex: ^master$|^main$
10+
is-main-branch: true
11+
12+
develop:
13+
label: preview
14+
increment: Minor
15+
regex: ^dev(elop)?(ment)?$
16+
source-branches: [main]
17+
tracks-release-branches: true
18+
19+
release:
20+
mode: ContinuousDeployment
21+
label: rc
22+
increment: Minor
23+
regex: ^releases?[/-](?<BranchName>.+)
24+
source-branches: [main, develop, support]
25+
is-release-branch: true
26+
27+
hotfix:
28+
mode: ContinuousDeployment
29+
label: hf
30+
increment: Inherit
31+
regex: ^hotfix(es)?[/-](?<BranchName>.+)
32+
source-branches: [main, support]
33+
is-release-branch: true
34+
35+
feature:
36+
mode: ContinuousDeployment
37+
label: '{BranchName}'
38+
increment: Inherit
39+
regex: ^features?[/-](?<BranchName>.+)
40+
source-branches: [develop, main, release, support, hotfix]
41+
42+
pull-request:
43+
mode: ContinuousDelivery
44+
label: PullRequest{Number}
45+
increment: Inherit
46+
regex: ^(pull-requests|pull|pr)[/-](?<Number>\d*)
47+
source-branches: [develop, main, release, feature, support, hotfix]

README.md

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -369,24 +369,31 @@ Integration tests use `[Collection("Integration")]` and require running Docker C
369369

370370
## CI/CD
371371

372-
This project uses **GitHub Actions** (`.github/workflows/ci.yml`):
372+
This project uses **GitHub Actions** (`.github/workflows/ci.yml`) with **[GitVersion](https://gitversion.net/)** for automatic semantic versioning based on the GitFlow branching model.
373373

374-
| Branch/Tag | Version Format | NuGet Feed |
374+
Versions are derived from git history and tags — no manual version bumping required after initial setup.
375+
376+
| Branch/Tag | Pre-release Label | Example Version |
375377
|---|---|---|
376-
| `v*` tag | `{tag}` (e.g., `1.3.0`) | nuget.org (stable) |
377-
| `master` / `main` | `{BASE_VERSION}` | nuget.org (stable) |
378-
| `release/*` | `{BASE_VERSION}-rc.{run}` | nuget.org (pre-release) |
379-
| `hotfix/*` | `{BASE_VERSION}-hf.{run}` | nuget.org (pre-release) |
380-
| `develop` | `{BASE_VERSION}-preview.{run}` | nuget.org (pre-release) |
381-
| `feature/*` | `{BASE_VERSION}-{branch}.{run}` | nuget.org (pre-release) |
378+
| `v*` tag | _(stable)_ | `1.3.0` |
379+
| `master` / `main` | _(stable)_ | `1.3.0` |
380+
| `release/*` | `rc` | `1.3.0-rc.3` |
381+
| `hotfix/*` | `hf` | `1.3.1-hf.1` |
382+
| `develop` | `preview` | `1.4.0-preview.12` |
383+
| `feature/*` | `{branch}` | `1.4.0-my-feature.1` |
384+
385+
### How versioning works
386+
387+
- **Tag a release** on `main`/`master` (e.g., `v1.3.0`) to set the version baseline
388+
- All subsequent commits on branches derive their version from git tags and merge history
389+
- Commit messages with `+semver: major`, `+semver: minor`, or `+semver: fix` control version increments
390+
- Configuration lives in `GitVersion.yml` at the repo root
382391

383392
All packages are published with the `Cts.*` prefix (e.g., `Cts.EventHorizon.Abstractions`).
384393

385-
### Secrets
394+
### Trusted Publishing
386395

387-
| Secret | Purpose |
388-
|---|---|
389-
| `NUGET_API_KEY` | API key for publishing to nuget.org |
396+
NuGet packages are published using [trusted publishing](https://devblogs.microsoft.com/nuget/introducing-trusted-publishers/) via GitHub's OIDC tokens — no API keys or secrets required. The trusted publisher is configured on nuget.org to trust the `ci.yml` workflow in this repository.
390397

391398
## Samples
392399

0 commit comments

Comments
 (0)