Skip to content

Commit 64d6088

Browse files
authored
feat(ci): add main build, PR validation, and release workflows (#81)
1 parent 5ad9c00 commit 64d6088

4 files changed

Lines changed: 248 additions & 24 deletions

File tree

.github/workflows/ci.yml

Lines changed: 0 additions & 24 deletions
This file was deleted.

.github/workflows/main-build.yml

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
name: Main Build
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
permissions:
9+
contents: write
10+
packages: write
11+
12+
jobs:
13+
build-and-pack:
14+
name: Build, Pack and Create Draft Release
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- name: Checkout code
19+
uses: actions/checkout@v4
20+
with:
21+
fetch-depth: 0 # Required for GitVersion
22+
23+
- name: Setup .NET
24+
uses: actions/setup-dotnet@v4
25+
with:
26+
dotnet-version: |
27+
8.0.x
28+
9.0.x
29+
10.0.x
30+
31+
- name: Restore dependencies
32+
run: dotnet restore
33+
34+
- name: Build
35+
run: dotnet build --configuration Release --no-restore
36+
37+
- name: Test
38+
run: dotnet test --configuration Release --no-build --verbosity normal
39+
40+
- name: Pack NuGet packages
41+
run: dotnet pack --configuration Release --no-build --output ./artifacts
42+
43+
- name: Get version from packages
44+
id: get-version
45+
run: |
46+
# Extract version from the first package
47+
VERSION=$(ls ./artifacts/*.nupkg | head -1 | sed -n 's/.*\.MyCSharp\.HttpUserAgentParser\.\([0-9]\+\.[0-9]\+\.[0-9]\+.*\)\.nupkg/\1/p')
48+
echo "version=$VERSION" >> $GITHUB_OUTPUT
49+
echo "Version: $VERSION"
50+
51+
- name: Check for existing draft release
52+
id: check-draft
53+
env:
54+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
55+
run: |
56+
DRAFT_RELEASE=$(gh release list --limit 100 --json isDraft,name,tagName | jq -r '.[] | select(.isDraft == true) | .tagName' | head -1)
57+
if [ -n "$DRAFT_RELEASE" ]; then
58+
echo "exists=true" >> $GITHUB_OUTPUT
59+
echo "tag=$DRAFT_RELEASE" >> $GITHUB_OUTPUT
60+
echo "Found existing draft release: $DRAFT_RELEASE"
61+
else
62+
echo "exists=false" >> $GITHUB_OUTPUT
63+
echo "No existing draft release found"
64+
fi
65+
66+
- name: Delete existing draft release
67+
if: steps.check-draft.outputs.exists == 'true'
68+
env:
69+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
70+
run: |
71+
echo "Deleting existing draft release: ${{ steps.check-draft.outputs.tag }}"
72+
gh release delete ${{ steps.check-draft.outputs.tag }} --yes --cleanup-tag || true
73+
74+
- name: Create draft release
75+
env:
76+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
77+
run: |
78+
VERSION="${{ steps.get-version.outputs.version }}"
79+
TAG="v${VERSION}"
80+
81+
# Create release notes
82+
cat > release-notes.md << 'EOF'
83+
## What's Changed
84+
85+
This is an automated draft release created from the main branch.
86+
87+
### Packages
88+
89+
The following NuGet packages are included in this release:
90+
91+
EOF
92+
93+
# List all packages
94+
for file in ./artifacts/*.nupkg; do
95+
filename=$(basename "$file")
96+
echo "- \`$filename\`" >> release-notes.md
97+
done
98+
99+
cat >> release-notes.md << 'EOF'
100+
101+
### Installation
102+
103+
```bash
104+
dotnet add package MyCSharp.HttpUserAgentParser
105+
dotnet add package MyCSharp.HttpUserAgentParser.AspNetCore
106+
dotnet add package MyCSharp.HttpUserAgentParser.MemoryCache
107+
```
108+
109+
**Full Changelog**: https://github.com/${{ github.repository }}/commits/${{ github.sha }}
110+
EOF
111+
112+
# Create draft release
113+
gh release create "$TAG" \
114+
./artifacts/*.nupkg \
115+
--draft \
116+
--title "Release $VERSION" \
117+
--notes-file release-notes.md \
118+
--target ${{ github.sha }}
119+
120+
echo "Created draft release: $TAG"
121+
122+
- name: Upload artifacts
123+
uses: actions/upload-artifact@v4
124+
with:
125+
name: nuget-packages
126+
path: ./artifacts/*.nupkg
127+
retention-days: 30
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: PR Validation
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
8+
permissions:
9+
contents: read
10+
pull-requests: read
11+
12+
jobs:
13+
validate:
14+
name: Build and Test
15+
runs-on: ubuntu-latest
16+
17+
strategy:
18+
matrix:
19+
dotnet-version: ['8.0.x', '9.0.x', '10.0.x']
20+
21+
steps:
22+
- name: Checkout code
23+
uses: actions/checkout@v4
24+
with:
25+
fetch-depth: 0 # Required for GitVersion
26+
27+
- name: Setup .NET ${{ matrix.dotnet-version }}
28+
uses: actions/setup-dotnet@v4
29+
with:
30+
dotnet-version: ${{ matrix.dotnet-version }}
31+
32+
- name: Restore dependencies
33+
run: dotnet restore
34+
35+
- name: Build
36+
run: dotnet build --configuration Release --no-restore
37+
38+
- name: Test
39+
run: dotnet test --configuration Release --no-build --verbosity normal --logger "trx;LogFileName=test-results.trx"
40+
41+
- name: Upload test results
42+
if: always()
43+
uses: actions/upload-artifact@v4
44+
with:
45+
name: test-results-${{ matrix.dotnet-version }}
46+
path: '**/TestResults/**/*.trx'
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
name: Publish Release
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
permissions:
8+
contents: read
9+
packages: write
10+
11+
jobs:
12+
publish-nuget:
13+
name: Publish to NuGet.org
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v4
19+
with:
20+
fetch-depth: 0
21+
22+
- name: Setup .NET
23+
uses: actions/setup-dotnet@v4
24+
with:
25+
dotnet-version: |
26+
8.0.x
27+
9.0.x
28+
10.0.x
29+
30+
- name: Restore dependencies
31+
run: dotnet restore
32+
33+
- name: Build
34+
run: dotnet build --configuration Release --no-restore
35+
36+
- name: Test
37+
run: dotnet test --configuration Release --no-build --verbosity normal
38+
39+
- name: Pack NuGet packages
40+
run: dotnet pack --configuration Release --no-build --output ./artifacts
41+
42+
- name: Verify packages
43+
run: |
44+
echo "Packages to be published:"
45+
ls -la ./artifacts/*.nupkg
46+
47+
# Verify package count
48+
PACKAGE_COUNT=$(ls ./artifacts/*.nupkg | wc -l)
49+
if [ "$PACKAGE_COUNT" -eq 0 ]; then
50+
echo "Error: No packages found!"
51+
exit 1
52+
fi
53+
54+
echo "Found $PACKAGE_COUNT package(s) to publish"
55+
56+
- name: Publish to NuGet.org
57+
env:
58+
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
59+
run: |
60+
for package in ./artifacts/*.nupkg; do
61+
echo "Publishing $package to NuGet.org..."
62+
dotnet nuget push "$package" \
63+
--api-key "$NUGET_API_KEY" \
64+
--source https://api.nuget.org/v3/index.json \
65+
--skip-duplicate
66+
done
67+
68+
echo "All packages published successfully!"
69+
70+
- name: Upload published packages as artifacts
71+
uses: actions/upload-artifact@v4
72+
with:
73+
name: published-nuget-packages
74+
path: ./artifacts/*.nupkg
75+
retention-days: 90

0 commit comments

Comments
 (0)