1+ name : Build and Publish Packages
2+
3+ on :
4+ push :
5+ branches : [ master, main ]
6+ paths :
7+ - ' BlazorTextDiff/**'
8+ pull_request :
9+ branches : [ master, main ]
10+ paths :
11+ - ' BlazorTextDiff/**'
12+ workflow_dispatch :
13+
14+ env :
15+ DOTNET_VERSION : ' 9.0.x'
16+ PROJECT_PATH : ' BlazorTextDiff/BlazorTextDiff.csproj'
17+ ARTIFACT_NAME : ' BlazorTextDiff-Package'
18+
19+ jobs :
20+ build-and-test :
21+ runs-on : ubuntu-latest
22+ outputs :
23+ version : ${{ steps.version.outputs.version }}
24+ should-publish : ${{ steps.check-publish.outputs.should-publish }}
25+
26+ steps :
27+ - name : Checkout repository
28+ uses : actions/checkout@v4
29+ with :
30+ fetch-depth : 0
31+
32+ - name : Setup .NET
33+ uses : actions/setup-dotnet@v4
34+ with :
35+ dotnet-version : ${{ env.DOTNET_VERSION }}
36+
37+ - name : Cache NuGet packages
38+ uses : actions/cache@v4
39+ with :
40+ path : ~/.nuget/packages
41+ key : ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj') }}
42+ restore-keys : |
43+ ${{ runner.os }}-nuget-
44+
45+ - name : Generate version number
46+ id : version
47+ run : |
48+ # Generate date-based version similar to 2021.4.22.43642
49+ UTC_DATE=$(date -u '+%Y.%m.%d')
50+ START_OF_DAY=$(date -u -d "today 00:00:00" '+%s')
51+ CURRENT_TIME=$(date -u '+%s')
52+ SECONDS_SINCE_MIDNIGHT=$(( (CURRENT_TIME - START_OF_DAY) / 2 ))
53+ VERSION="$UTC_DATE.$SECONDS_SINCE_MIDNIGHT"
54+ echo "version=$VERSION" >> $GITHUB_OUTPUT
55+ echo "Generated version: $VERSION"
56+
57+ - name : Update project version
58+ run : |
59+ sed -i "s|<Version>.*</Version>|<Version>${{ steps.version.outputs.version }}</Version>|" ${{ env.PROJECT_PATH }}
60+ echo "Updated version in project file to: ${{ steps.version.outputs.version }}"
61+ cat ${{ env.PROJECT_PATH }} | grep -A1 -B1 "<Version>"
62+
63+ - name : Restore dependencies
64+ run : dotnet restore
65+
66+ - name : Build solution
67+ run : dotnet build --no-restore --configuration Release
68+
69+ - name : Run tests
70+ run : dotnet test --no-build --configuration Release --verbosity normal
71+
72+ - name : Pack NuGet package
73+ run : dotnet pack ${{ env.PROJECT_PATH }} --no-build --configuration Release --output ./artifacts
74+
75+ - name : Check if should publish
76+ id : check-publish
77+ run : |
78+ if [[ "${{ github.event_name }}" == "push" && ("${{ github.ref }}" == "refs/heads/master" || "${{ github.ref }}" == "refs/heads/main") ]]; then
79+ echo "should-publish=true" >> $GITHUB_OUTPUT
80+ echo "Will publish packages"
81+ else
82+ echo "should-publish=false" >> $GITHUB_OUTPUT
83+ echo "Will not publish packages (not a push to main/master branch)"
84+ fi
85+
86+ - name : Upload build artifacts
87+ uses : actions/upload-artifact@v4
88+ with :
89+ name : ${{ env.ARTIFACT_NAME }}
90+ path : ./artifacts/*.nupkg
91+ if-no-files-found : error
92+
93+ publish-github :
94+ name : Publish to GitHub Packages
95+ runs-on : ubuntu-latest
96+ needs : build-and-test
97+ if : needs.build-and-test.outputs.should-publish == 'true'
98+ permissions :
99+ contents : read
100+ packages : write
101+
102+ steps :
103+ - name : Setup .NET
104+ uses : actions/setup-dotnet@v4
105+ with :
106+ dotnet-version : ${{ env.DOTNET_VERSION }}
107+
108+ - name : Download artifacts
109+ uses : actions/download-artifact@v4
110+ with :
111+ name : ${{ env.ARTIFACT_NAME }}
112+ path : ./artifacts
113+
114+ - name : Publish to GitHub Packages
115+ run : |
116+ dotnet nuget push "./artifacts/*.nupkg" \
117+ --source "https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json" \
118+ --api-key ${{ secrets.GITHUB_TOKEN }} \
119+ --skip-duplicate
120+
121+ publish-nuget :
122+ name : Publish to NuGet.org
123+ runs-on : ubuntu-latest
124+ needs : build-and-test
125+ if : needs.build-and-test.outputs.should-publish == 'true'
126+
127+ steps :
128+ - name : Setup .NET
129+ uses : actions/setup-dotnet@v4
130+ with :
131+ dotnet-version : ${{ env.DOTNET_VERSION }}
132+
133+ - name : Download artifacts
134+ uses : actions/download-artifact@v4
135+ with :
136+ name : ${{ env.ARTIFACT_NAME }}
137+ path : ./artifacts
138+
139+ - name : Publish to NuGet.org
140+ run : |
141+ dotnet nuget push "./artifacts/*.nupkg" \
142+ --api-key ${{ secrets.NUGET_API_KEY }} \
143+ --source https://api.nuget.org/v3/index.json \
144+ --skip-duplicate
145+
146+ create-release :
147+ name : Create GitHub Release
148+ runs-on : ubuntu-latest
149+ needs : [build-and-test]
150+ if : needs.build-and-test.outputs.should-publish == 'true'
151+ permissions :
152+ contents : write
153+
154+ steps :
155+ - name : Checkout repository
156+ uses : actions/checkout@v4
157+
158+ - name : Download artifacts
159+ uses : actions/download-artifact@v4
160+ with :
161+ name : ${{ env.ARTIFACT_NAME }}
162+ path : ./artifacts
163+
164+ - name : Create Release
165+ uses : softprops/action-gh-release@v2
166+ with :
167+ tag_name : v${{ needs.build-and-test.outputs.version }}
168+ name : Release v${{ needs.build-and-test.outputs.version }}
169+ body : |
170+ ## BlazorTextDiff v${{ needs.build-and-test.outputs.version }}
171+
172+ ### 📦 Package Information
173+ - **Version**: ${{ needs.build-and-test.outputs.version }}
174+ - **Built**: ${{ github.run_id }}
175+ - **Commit**: ${{ github.sha }}
176+
177+ ### 📥 Installation
178+ ```bash
179+ dotnet add package BlazorTextDiff --version ${{ needs.build-and-test.outputs.version }}
180+ ```
181+
182+ ### 🔗 Links
183+ - [NuGet Package](https://www.nuget.org/packages/BlazorTextDiff/${{ needs.build-and-test.outputs.version }})
184+ - [GitHub Package](https://github.com/${{ github.repository }}/packages)
185+ files : ./artifacts/*.nupkg
186+ draft : false
187+ prerelease : false
0 commit comments