Skip to content

Commit 66ea1e0

Browse files
committed
Update CI Publish Workflow
1 parent dffad3d commit 66ea1e0

File tree

2 files changed

+91
-42
lines changed

2 files changed

+91
-42
lines changed

.github/workflows/Publish.yml

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,40 @@
1-
name: Publish
1+
name: Publish NuGet Package
22
# This job builds and publishes the package to NuGet.
33
# It depends on the included tests job to complete successfully.
4-
# The version number is determined by the existing tag provided
5-
# when manually triggering the workflow.
6-
# The tag is not limited to a certain branch.
4+
# This workflow finds the most recent tag that starts with
5+
# the selected major version (sorted by creatordate)
6+
# and runs the test, build and publish tasks for that tag.
77
on:
88
workflow_dispatch:
9-
inputs:
10-
version_tag:
11-
description: 'Enter an existing version tag to publish (e.g., "v5.1.0" or "v6.0.0-pre.1")'
12-
required: true
13-
type: string
14-
9+
inputs:
10+
major_version:
11+
description: 'Select the major version to publish (tag prefix). The workflow will pick the most recent tag that starts with this value (e.g. v3 -> v3.2.1).'
12+
required: true
13+
type: choice
14+
options:
15+
- v3
16+
# - v4
1517
jobs:
1618
tests:
17-
runs-on: ubuntu-22.04
18-
# ubuntu-latest = ubuntu-24.04 does not include mono needed for tests (2025-08-01)
19+
runs-on: ubuntu-22.04 # ubuntu-24.04 does not include mono needed for net4x tests
1920
outputs:
20-
VERSION: ${{ steps.set_version.outputs.VERSION }}
21+
VERSION: ${{ steps.resolve.outputs.VERSION }}
22+
TAG_NAME: ${{ steps.resolve.outputs.TAG_NAME }}
2123
steps:
2224
- name: Checkout repository
2325
uses: actions/checkout@v5
2426
with:
2527
fetch-depth: 0 # Fetch all history for all tags and branches
2628

27-
- name: Verify tag exists
28-
run: |
29-
git fetch --tags --quiet
30-
if ! git rev-parse "${{ inputs.version_tag }}" >/dev/null 2>&1; then
31-
echo "::error::Tag '${{ inputs.version_tag }}' does not exist."
32-
echo "Available tags:"
33-
git tag -l
34-
exit 1
35-
fi
36-
37-
- name: Checkout tag
38-
uses: actions/checkout@v5
39-
with:
40-
fetch-depth: 0 # Fetch all history for all tags and branches
41-
ref: ${{ inputs.version_tag }}
42-
43-
- name: Set VERSION output
44-
id: set_version
29+
- name: Show matching major version tags and select latest by creatordate
30+
id: resolve
31+
shell: bash
4532
run: |
46-
Version="${{ inputs.version_tag }}"
47-
# Strip 'v' prefix for VERSION variable
48-
Version="${Version#v}"
49-
echo "VERSION=$Version" >> $GITHUB_ENV
50-
echo "VERSION=$Version" >> $GITHUB_OUTPUT
51-
echo "Version: $Version"
33+
set -euo pipefail
34+
# Call the script to resolve the latest tag for the requested major version.
35+
# Script writes TAG_NAME and VERSION to $GITHUB_OUTPUT and $GITHUB_ENV when run on Actions.
36+
chmod +x .github/workflows/PublishResolveVersion.sh
37+
.github/workflows/PublishResolveVersion.sh "${{ github.event.inputs.major_version }}"
5238
5339
- name: Set Git config for line endings
5440
run: git config --global core.autocrlf true
@@ -57,12 +43,20 @@ jobs:
5743
uses: actions/setup-dotnet@v5
5844
with:
5945
dotnet-version: |
46+
10.0.x
6047
8.0.x
6148
6.0.x
6249
3.1.x
6350
51+
- name: Checkout tag
52+
uses: actions/checkout@v5
53+
with:
54+
fetch-depth: 0 # Fetch all history for all tags and branches
55+
ref: ${{ steps.resolve.outputs.TAG_NAME }}
56+
6457
- name: Remove Demo
6558
run: dotnet sln ./src/SmartFormat.sln remove ./src/Demo/Demo.csproj
59+
6660
- name: Restore dependencies
6761
run: dotnet restore ./src/SmartFormat.sln
6862

@@ -73,15 +67,16 @@ jobs:
7367
run: dotnet test ./src/SmartFormat.sln --no-build --configuration Release --verbosity quiet
7468

7569
publish:
76-
runs-on: ubuntu-22.04
70+
runs-on: ubuntu-24.04
7771
needs: tests
7872

7973
steps:
8074
- name: Checkout tag
8175
uses: actions/checkout@v5
8276
with:
8377
fetch-depth: 0 # Fetch all history for all tags and branches
84-
ref: ${{ inputs.version_tag }}
78+
# Reuse the tag name from tests job
79+
ref: ${{ needs.tests.outputs.TAG_NAME }}
8580

8681
- name: Set Git config for line endings
8782
run: git config --global core.autocrlf true
@@ -90,7 +85,7 @@ jobs:
9085
uses: actions/setup-dotnet@v5
9186
with:
9287
dotnet-version: |
93-
8.0.x
88+
10.0.x
9489
9590
- name: Use verified version
9691
run: |
@@ -112,7 +107,7 @@ jobs:
112107

113108
- name: Build and pack for publishing
114109
run: |
115-
# For version e.g. "5.1.0-pre.1", the FileVersion will be "5.1.0" and the AssemblyVersion will be "5.0.0"
110+
# For version e.g. "3.1.0-pre.1", the FileVersion will be "3.1.0" and the AssemblyVersion will be "3.0.0"
116111
# AssemblyVersion must only change for major releases
117112
dotnet build ./src/SmartFormat.Deploy.sln --configuration Release \
118113
-p:Version=${{env.VERSION}} \
@@ -141,7 +136,9 @@ jobs:
141136
- name: Push package to NuGet
142137
# Does not fail, if the package already exists
143138
# Note: Symbol packages get uploaded automatically with the main package
139+
env:
140+
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
144141
run: |
145142
for pkg in ${{ github.workspace }}/artifacts/*.nupkg; do
146-
dotnet nuget push "$pkg" --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.NUGET_API_KEY }} --skip-duplicate
143+
dotnet nuget push "$pkg" --source https://api.nuget.org/v3/index.json --api-key "$NUGET_API_KEY" --skip-duplicate
147144
done
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env bash
2+
# Enable strict mode when running inside GitHub Actions runner
3+
if [ "${GITHUB_ACTIONS:-false}" = "true" ]; then
4+
set -euo pipefail
5+
fi
6+
7+
Major="${1:-}"
8+
if [ -z "$Major" ]; then
9+
echo "::error::No major version argument supplied. Usage: $0 <major> (e.g. v3)" >&2
10+
exit 1
11+
fi
12+
echo "Looking for tags that start with: $Major"
13+
14+
# Fetch tags
15+
git fetch --tags --quiet
16+
17+
# Collect matching tags (newest first by creatordate)
18+
mapfile -t MATCHING < <(
19+
git for-each-ref --sort=-creatordate --format='%(refname:short)' refs/tags \
20+
| grep -E "^${Major}(\.|$)" || true
21+
)
22+
23+
if [ ${#MATCHING[@]} -eq 0 ]; then
24+
echo "::error::No tag found starting with '${Major}'. Showing latest tags:"
25+
git for-each-ref --sort=-creatordate --format='%(refname:short)' refs/tags | head -n 10
26+
exit 1
27+
fi
28+
29+
echo "Showing up to 10 of top ${#MATCHING[@]} matching tags (newest first):"
30+
for tag in "${MATCHING[@]:0:10}"; do
31+
echo " - $tag"
32+
done
33+
34+
Tag="${MATCHING[0]}"
35+
Version="${Tag#v}"
36+
37+
echo "Selected tag: $Tag -> version: $Version"
38+
39+
# If running in GitHub Actions, write to the runner files
40+
if [ -n "${GITHUB_OUTPUT-}" ]; then
41+
printf 'TAG_NAME=%s\n' "$Tag" >> "$GITHUB_OUTPUT"
42+
printf 'VERSION=%s\n' "$Version" >> "$GITHUB_OUTPUT"
43+
fi
44+
45+
if [ -n "${GITHUB_ENV-}" ]; then
46+
printf 'TAG_NAME=%s\n' "$Tag" >> "$GITHUB_ENV"
47+
printf 'VERSION=%s\n' "$Version" >> "$GITHUB_ENV"
48+
fi
49+
50+
# Always print results
51+
printf 'TAG_NAME=%s\n' "$Tag"
52+
printf 'VERSION=%s\n' "$Version"

0 commit comments

Comments
 (0)