Skip to content

Commit da5441a

Browse files
authored
Create build-and-nuget-publish.yml
1 parent 5591dcc commit da5441a

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
name: Build and Publish to NuGet
2+
3+
on:
4+
pull_request:
5+
types: [closed]
6+
branches:
7+
- 'v*/main'
8+
9+
jobs:
10+
build-and-publish:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v3
16+
with:
17+
fetch-depth: 0
18+
19+
- name: Set up .NET
20+
uses: actions/setup-dotnet@v3
21+
with:
22+
dotnet-version: '9.0.x'
23+
24+
- name: Check if PR was merged
25+
id: check_merge
26+
run: |
27+
if [ "${{ github.event.pull_request.merged }}" = "true" ]; then
28+
echo "merged=true" >> $GITHUB_OUTPUT
29+
else
30+
echo "merged=false" >> $GITHUB_OUTPUT
31+
fi
32+
33+
- name: Determine version increment
34+
id: versioning
35+
if: ${{ steps.check_merge.outputs.merged == 'true' }}
36+
run: |
37+
labels=$(echo '${{ toJSON(github.event.pull_request.labels) }}' | jq -r '.[].name')
38+
echo "PR labels: $labels"
39+
increment="patch"
40+
if echo "$labels" | grep -q "feature"; then
41+
increment="minor"
42+
fi
43+
echo "increment=$increment" >> $GITHUB_OUTPUT
44+
45+
- name: Get current version
46+
id: get_version
47+
if: ${{ steps.check_merge.outputs.merged == 'true' }}
48+
run: |
49+
branchName='${{ github.ref_name }}' # e.g., v1/main
50+
echo "Branch name: $branchName"
51+
if [[ $branchName =~ ^v([0-9]+)/main$ ]]; then
52+
majorVersion="${BASH_REMATCH[1]}"
53+
echo "Major version: $majorVersion"
54+
git fetch --tags
55+
latestTag=$(git tag --list "v$majorVersion.*.*" | sort -V | tail -n1)
56+
if [ -n "$latestTag" ]; then
57+
currentVersion="${latestTag#v}"
58+
else
59+
currentVersion="$majorVersion.0.0"
60+
fi
61+
else
62+
echo "Branch name does not match expected pattern"
63+
exit 1
64+
fi
65+
echo "Current version: $currentVersion"
66+
echo "major=$majorVersion" >> $GITHUB_OUTPUT
67+
echo "currentVersion=$currentVersion" >> $GITHUB_OUTPUT
68+
69+
- name: Bump version
70+
id: bump_version
71+
if: ${{ steps.check_merge.outputs.merged == 'true' }}
72+
run: |
73+
increment='${{ steps.versioning.outputs.increment }}'
74+
currentVersion='${{ steps.get_version.outputs.currentVersion }}'
75+
IFS='.' read -ra versionParts <<< "$currentVersion"
76+
major=${versionParts[0]}
77+
minor=${versionParts[1]}
78+
patch=${versionParts[2]}
79+
if [ "$increment" == 'minor' ]; then
80+
minor=$((minor + 1))
81+
patch=0
82+
elif [ "$increment" == 'patch' ]; then
83+
patch=$((patch + 1))
84+
else
85+
echo "Unknown increment type: $increment"
86+
exit 1
87+
fi
88+
newVersion="$major.$minor.$patch"
89+
echo "New version: $newVersion"
90+
echo "newVersion=$newVersion" >> $GITHUB_OUTPUT
91+
92+
- name: Install xmlstarlet
93+
if: ${{ steps.check_merge.outputs.merged == 'true' }}
94+
run: sudo apt-get install -y xmlstarlet
95+
96+
- name: Update project versions
97+
if: ${{ steps.check_merge.outputs.merged == 'true' }}
98+
run: |
99+
newVersion='${{ steps.bump_version.outputs.newVersion }}'
100+
find . -name '*.csproj' -not -path '*/Cortex.Tests/*' | while read csproj; do
101+
echo "Updating version in $csproj"
102+
xmlstarlet ed -P -L -u "//Project/PropertyGroup/Version" -v "$newVersion" "$csproj" || \
103+
xmlstarlet ed -P -L -s "//Project/PropertyGroup" -t elem -n "Version" -v "$newVersion" "$csproj"
104+
done
105+
106+
- name: Build solution
107+
if: ${{ steps.check_merge.outputs.merged == 'true' }}
108+
run: dotnet build --configuration Release
109+
110+
- name: Pack projects
111+
if: ${{ steps.check_merge.outputs.merged == 'true' }}
112+
run: |
113+
dotnet pack --configuration Release --no-build --output ./artifacts
114+
115+
- name: Publish to NuGet
116+
if: ${{ steps.check_merge.outputs.merged == 'true' }}
117+
env:
118+
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
119+
run: |
120+
dotnet nuget push ./artifacts/*.nupkg --api-key $NUGET_API_KEY --source https://api.nuget.org/v3/index.json
121+
122+
- name: Configure Git
123+
if: ${{ steps.check_merge.outputs.merged == 'true' }}
124+
run: |
125+
git config user.name "GitHub Actions"
126+
git config user.email "actions@github.com"
127+
128+
- name: Create Git tag
129+
if: ${{ steps.check_merge.outputs.merged == 'true' }}
130+
run: |
131+
newVersion='${{ steps.bump_version.outputs.newVersion }}'
132+
git tag -a "v$newVersion" -m "Release v$newVersion"
133+
git push origin "v$newVersion"
134+
135+
- name: Create GitHub Release
136+
if: ${{ steps.check_merge.outputs.merged == 'true' }}
137+
uses: actions/create-release@v1
138+
env:
139+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
140+
with:
141+
tag_name: "v${{ steps.bump_version.outputs.newVersion }}"
142+
release_name: "v${{ steps.bump_version.outputs.newVersion }}"
143+
draft: false
144+
prerelease: false
145+
body: "Release of version v${{ steps.bump_version.outputs.newVersion }}"

0 commit comments

Comments
 (0)