-
Notifications
You must be signed in to change notification settings - Fork 0
186 lines (159 loc) · 6.26 KB
/
release.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
name: Create Release and Publish to NuGet
on:
workflow_dispatch:
inputs:
release_type:
description: 'Release type'
required: true
default: 'patch'
type: choice
options:
- patch
- minor
- major
jobs:
build-and-release:
runs-on: ubuntu-latest
steps:
# Checkout repository with full history
- uses: actions/checkout@v3
with:
fetch-depth: 0
# Setup .NET SDK
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: '9.0.x'
# Check which projects changed and calculate their new versions
- name: Check Changes and Calculate Versions
id: version_check
run: |
SHOULD_RELEASE="false"
CHANGED_PROJECTS=""
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
NEW_VERSION=""
# Function to compare version numbers
version_gt() {
test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"
}
# Check each project for changes and update versions
for proj in $(find . -name "*.csproj"); do
PROJECT_NAME=$(basename "$proj")
PROJECT_DIR=$(dirname "$proj")
# Get current version from csproj
CURRENT_VERSION=$(grep -oP '(?<=<Version>).*(?=</Version>)' "$proj" || echo "0.0.0")
if [ -z "$CURRENT_VERSION" ]; then
echo "Warning: Could not find version in $proj"
continue
fi
# Split version into parts
IFS='.' read -r -a VERSION_PARTS <<< "$CURRENT_VERSION"
MAJOR="${VERSION_PARTS[0]}"
MINOR="${VERSION_PARTS[1]}"
PATCH="${VERSION_PARTS[2]}"
# Check if project has changes since last tag
if [ -z "$LATEST_TAG" ] || ! git diff --quiet $LATEST_TAG HEAD -- "$PROJECT_DIR"; then
# Calculate new version based on release type
case "${{ github.event.inputs.release_type }}" in
"major")
PROJECT_NEW_VERSION="$((MAJOR + 1)).0.0"
;;
"minor")
PROJECT_NEW_VERSION="${MAJOR}.$((MINOR + 1)).0"
;;
"patch")
PROJECT_NEW_VERSION="${MAJOR}.${MINOR}.$((PATCH + 1))"
;;
esac
# Update version in csproj
sed -i "s/<Version>.*<\/Version>/<Version>${PROJECT_NEW_VERSION}<\/Version>/g" "$proj"
CHANGED_PROJECTS="$CHANGED_PROJECTS$PROJECT_NAME -> $PROJECT_NEW_VERSION\n"
SHOULD_RELEASE="true"
# Update NEW_VERSION if this project's version is higher
if [ -z "$NEW_VERSION" ] || version_gt "$PROJECT_NEW_VERSION" "$NEW_VERSION"; then
NEW_VERSION="$PROJECT_NEW_VERSION"
echo "New highest version: $NEW_VERSION from $PROJECT_NAME"
fi
fi
done
if [ -z "$NEW_VERSION" ]; then
echo "No version changes detected"
exit 0
fi
echo "Final highest version will be: v$NEW_VERSION"
# Set environment variables for next steps
echo "CHANGED_PROJECTS<<EOF" >> $GITHUB_ENV
echo -e "$CHANGED_PROJECTS" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
echo "SHOULD_RELEASE=$SHOULD_RELEASE" >> $GITHUB_ENV
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV
# Generate release notes with commits and changed projects
- name: Generate Release Notes
if: env.SHOULD_RELEASE == 'true'
id: release_notes
run: |
PREVIOUS_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
if [ -z "$PREVIOUS_TAG" ]; then
COMMITS=$(git log --pretty=format:"- %s" --no-merges)
else
COMMITS=$(git log ${PREVIOUS_TAG}..HEAD --pretty=format:"- %s" --no-merges)
fi
echo "RELEASE_NOTES<<EOF" >> $GITHUB_ENV
echo "## What's Changed" >> $GITHUB_ENV
echo "$COMMITS" >> $GITHUB_ENV
echo -e "\n## Updated Projects" >> $GITHUB_ENV
echo "${{ env.CHANGED_PROJECTS }}" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
# Restore project dependencies
- name: Restore dependencies
if: env.SHOULD_RELEASE == 'true'
run: dotnet restore
# Build solution
- name: Build
if: env.SHOULD_RELEASE == 'true'
run: dotnet build --configuration Release --no-restore
# Create NuGet packages
- name: Pack
if: env.SHOULD_RELEASE == 'true'
run: dotnet pack --configuration Release --no-build --output nupkgs
# Create GitHub release
- name: Create Release
if: env.SHOULD_RELEASE == 'true'
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: v${{ env.NEW_VERSION }}
release_name: Release v${{ env.NEW_VERSION }}
body: ${{ env.RELEASE_NOTES }}
draft: false
prerelease: false
# Upload NuGet packages as release assets
- name: Upload Release Assets
if: env.SHOULD_RELEASE == 'true'
uses: softprops/action-gh-release@v1
with:
files: ./nupkgs/*.nupkg
tag_name: v${{ env.NEW_VERSION }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Publish packages to NuGet.org
- name: Push to NuGet
if: env.SHOULD_RELEASE == 'true'
run: dotnet nuget push "./nupkgs/*.nupkg" --source "https://api.nuget.org/v3/index.json" --api-key ${{secrets.NUGET_API_KEY}} --skip-duplicate
# Commit version updates and create tag
- name: Commit version updates
if: env.SHOULD_RELEASE == 'true' && env.NEW_VERSION != ''
run: |
if [ -z "${{ env.NEW_VERSION }}" ]; then
echo "Error: NEW_VERSION is not set"
exit 1
fi
echo "Creating release for version v${{ env.NEW_VERSION }}"
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git add .
git commit -m "Update project versions to v${{ env.NEW_VERSION }}"
git tag -a "v${{ env.NEW_VERSION }}" -m "Release v${{ env.NEW_VERSION }}"
git push --follow-tags