-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbuild.sh
104 lines (85 loc) · 3.24 KB
/
build.sh
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
#!/bin/bash
# 🚀 Usage: ./build.sh <build_type>
# <build_type> should be one of: patch, minor, major
BUILD_TYPE=$1
# ❓ Ensure build type is provided
if [[ -z "$BUILD_TYPE" ]]; then
echo "❌ Error: Build type (patch, minor, major) must be specified."
exit 1
fi
# ✅ Validate build type
if [[ "$BUILD_TYPE" != "patch" && "$BUILD_TYPE" != "minor" && "$BUILD_TYPE" != "major" ]]; then
echo "❌ Error: Invalid build type. Allowed types are: patch, minor, major."
exit 1
fi
# 📦 Step 1: Update composer.version in composer.json
echo "🔄 Updating version in composer.json..."
CURRENT_VERSION=$(jq -r '.version' composer.json)
IFS='.' read -r -a VERSION_PARTS <<< "$CURRENT_VERSION"
if [[ "$BUILD_TYPE" == "patch" ]]; then
VERSION_PARTS[2]=$((VERSION_PARTS[2] + 1))
elif [[ "$BUILD_TYPE" == "minor" ]]; then
VERSION_PARTS[1]=$((VERSION_PARTS[1] + 1))
VERSION_PARTS[2]=0
elif [[ "$BUILD_TYPE" == "major" ]]; then
VERSION_PARTS[0]=$((VERSION_PARTS[0] + 1))
VERSION_PARTS[1]=0
VERSION_PARTS[2]=0
fi
NEW_VERSION="${VERSION_PARTS[0]}.${VERSION_PARTS[1]}.${VERSION_PARTS[2]}"
# Update the composer.json file
jq --arg new_version "$NEW_VERSION" '.version = $new_version' composer.json > composer_temp.json && mv composer_temp.json composer.json
echo "✅ Updated version to $NEW_VERSION in composer.json"
# 📝 Step 2: Git commit and tag
echo "🔨 Committing changes..."
git add .
git commit -m "release: $BUILD_TYPE version $NEW_VERSION"
if [[ $? -ne 0 ]]; then
echo "❌ Error: Git commit failed."
exit 1
fi
# 🔖 Step 3: Create a new tag
echo "🏷️ Creating new git tag..."
git tag "$NEW_VERSION" -m "Release $BUILD_TYPE version $NEW_VERSION"
if [[ $? -ne 0 ]]; then
echo "❌ Error: Git tag creation failed."
exit 1
fi
# 📜 Step 4: Update CHANGELOG.md
echo "📜 Updating CHANGELOG.md..."
PREVIOUS_TAG=$(git tag --sort=-version:refname | sed -n 2p) # Get the second latest tag
if [[ -z "$PREVIOUS_TAG" ]]; then
PREVIOUS_TAG="HEAD" # Use HEAD if no previous tag exists
fi
echo "📝 Generating changelog from $PREVIOUS_TAG to $NEW_VERSION..."
CHANGELOG_ENTRIES=$(git log "$PREVIOUS_TAG".."$NEW_VERSION" --pretty=format:"- %s [%an]")
echo -e "## [$NEW_VERSION] - $(date +"%Y-%m-%d")\n\n$CHANGELOG_ENTRIES\n\n$(cat CHANGELOG.md)" > CHANGELOG.md
echo "✅ Updated CHANGELOG.md"
# Add CHANGELOG.md to git
git add CHANGELOG.md
git commit -m "chore: update CHANGELOG.md for $BUILD_TYPE version $NEW_VERSION"
if [[ $? -ne 0 ]]; then
echo "❌ Error: Git commit for CHANGELOG.md failed."
exit 1
fi
# 🔄 Step 5: Push changes to remote
echo "⬆️ Pushing changes to remote repository..."
git push origin main
if [[ $? -ne 0 ]]; then
echo "❌ Error: Git push to main branch failed."
exit 1
fi
git push --tags
if [[ $? -ne 0 ]]; then
echo "❌ Error: Git push tags failed."
exit 1
fi
echo "🎉 Release $BUILD_TYPE version $NEW_VERSION completed successfully!"
# 🛠️ Run composer update hook
sleep 5
echo "🔄 Running composer update hook..."
curl -X POST \
-H "Content-Type: application/json" \
-d "{\"repository\":{\"url\":\"${TURSO_DRIVER_LARAVEL_PACKAGE_URL}\",\"type\":\"github\"}}" \
"https://packagist.org/api/update-package?username=darkterminal&apiToken=${COMPOSER_UPDATE_TOKEN}"
echo "✅ Done running composer update hook."