-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
175 lines (138 loc) · 4.5 KB
/
deploy.sh
File metadata and controls
175 lines (138 loc) · 4.5 KB
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
#!/bin/bash
# ┌────────────────────────────────────────────────────────────┐
# │ EnumerablePrinter Deploy Script (Linux/macOS) │
# │ │
# │ Bumps version, updates csproj, commits, tags, builds, │
# │ tests, packs, and pushes to NuGet.org. │
# │ │
# │ Requires: │
# │ - nuget.secret (git‑ignored) OR $NUGET_API_KEY │
# │ │
# │ Usage: │
# │ ./deploy.sh │
# │ ./deploy.sh --dry-run │
# └────────────────────────────────────────────────────────────┘
PROJECT_PATH="src/EnumerablePrinter/EnumerablePrinter.csproj"
TEST_PROJECT_PATH="tests/EnumerablePrinter.Tests/EnumerablePrinter.Tests.csproj"
OUTPUT_DIR="nupkg"
DRY_RUN=false
# Parse args
for arg in "$@"; do
case $arg in
--dry-run)
DRY_RUN=true
;;
esac
done
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1"
}
# Initialize to satisfy ShellCheck (prevents “referenced but not assigned”)
NuGetApiKey=""
# Load NuGet API key from nuget.secret if present
if [ -f "nuget.secret" ]; then
log "🔐 Loading NuGet API key from nuget.secret"
# shellcheck source=/dev/null
source nuget.secret
export NUGET_API_KEY="$NuGetApiKey"
fi
# Ensure API key exists
if [ -z "$NUGET_API_KEY" ]; then
echo "❌ NuGet API key not set. Create a file named 'nuget.secret' with:"
echo "NuGetApiKey=your-key-here"
exit 1
fi
get_version() {
if [ ! -f VERSION ]; then
echo "VERSION file not found."
exit 1
fi
cat VERSION
}
set_version() {
echo -n "$1" > VERSION
}
bump_version() {
version=$(get_version)
IFS='.' read -r major minor patch <<< "$version"
patch=$((patch + 1))
if [ "$patch" -gt 9 ]; then
patch=0
minor=$((minor + 1))
fi
if [ "$minor" -gt 9 ]; then
minor=0
major=$((major + 1))
fi
new_version="$major.$minor.$patch"
set_version "$new_version"
echo "$new_version"
}
update_csproj_version() {
version="$1"
log "📝 Updating .csproj version to $version"
sed -i.bak "s|<Version>.*</Version>|<Version>$version</Version>|" "$PROJECT_PATH"
rm "$PROJECT_PATH.bak"
}
package_exists() {
version="$1"
url="https://api.nuget.org/v3-flatcontainer/enumerableprinter/$version/enumerableprinter.$version.nupkg"
curl -s --head "$url" | head -n 1 | grep "200 OK" > /dev/null
}
build_and_pack() {
version="$1"
log "🔧 Restoring project..."
dotnet restore "$PROJECT_PATH"
log "🛠️ Building project..."
dotnet build "$PROJECT_PATH" -c Release
log "🧪 Running tests..."
dotnet test "$TEST_PROJECT_PATH" --no-build
log "📦 Packing version $version..."
dotnet pack "$PROJECT_PATH" -c Release -o "$OUTPUT_DIR" /p:PackageVersion="$version"
}
push_to_nuget() {
version="$1"
package="$OUTPUT_DIR/EnumerablePrinter.$version.nupkg"
if [ ! -f "$package" ]; then
echo "❌ Package not found: $package"
exit 1
fi
log "📦 Found package: $(basename "$package")"
if [ "$DRY_RUN" = true ]; then
log "🧪 Dry run enabled. Skipping NuGet push."
return
fi
log "📤 Pushing to NuGet..."
dotnet nuget push "$package" \
--api-key "$NUGET_API_KEY" \
--source https://api.nuget.org/v3/index.json
}
# MAIN
log "🚀 Starting deploy..."
# 1. Bump version
new_version=$(bump_version)
tag="v$new_version"
log "🔢 New version: $new_version"
log "🏷️ Tag to be created: $tag"
# 2. Update .csproj
update_csproj_version "$new_version"
# 3. Commit version bump
log "📦 Staging version bump..."
git add VERSION
git add "$PROJECT_PATH"
log "📝 Committing version bump..."
git commit -m "Release $new_version"
# 4. Tag release
log "🏷️ Tagging release..."
git tag "$tag"
git push origin main
git push origin "$tag"
# 5. Skip if version already exists
if package_exists "$new_version"; then
log "⚠️ Version $new_version already exists on NuGet. Skipping push."
exit 0
fi
# 6. Build, test, pack, push
build_and_pack "$new_version"
push_to_nuget "$new_version"
log "✅ Deploy complete."