Skip to content

Commit 9f79d2b

Browse files
committed
add release script
1 parent 0154bec commit 9f79d2b

1 file changed

Lines changed: 178 additions & 0 deletions

File tree

scripts/release.sh

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Colors for output
5+
RED='\033[0;31m'
6+
GREEN='\033[0;32m'
7+
YELLOW='\033[1;33m'
8+
NC='\033[0m' # No Color
9+
10+
# Function to print colored output
11+
print_info() {
12+
echo -e "${GREEN}[INFO]${NC} $1"
13+
}
14+
15+
print_error() {
16+
echo -e "${RED}[ERROR]${NC} $1"
17+
}
18+
19+
print_warning() {
20+
echo -e "${YELLOW}[WARNING]${NC} $1"
21+
}
22+
23+
# Check if gh CLI is installed
24+
if ! command -v gh &> /dev/null; then
25+
print_error "GitHub CLI (gh) is not installed. Please install it first."
26+
exit 1
27+
fi
28+
29+
# Check if user is authenticated with gh
30+
if ! gh auth status &> /dev/null; then
31+
print_error "Not authenticated with GitHub CLI. Please run 'gh auth login' first."
32+
exit 1
33+
fi
34+
35+
# Parse command line arguments
36+
VERSION=""
37+
BUMP_TYPE=""
38+
PRE_RELEASE=false
39+
40+
case "${1:-}" in
41+
--major|--minor|--patch)
42+
BUMP_TYPE="${1#--}"
43+
shift
44+
;;
45+
--pre)
46+
PRE_RELEASE=true
47+
shift
48+
;;
49+
-h|--help|"")
50+
echo "Usage: $0 [VERSION|--major|--minor|--patch] [--pre]"
51+
echo ""
52+
echo "Examples:"
53+
echo " $0 1.2.3 # Release version 1.2.3"
54+
echo " $0 --patch # Bump patch version (0.1.0 -> 0.1.1)"
55+
echo " $0 --minor # Bump minor version (0.1.0 -> 0.2.0)"
56+
echo " $0 --major # Bump major version (0.1.0 -> 1.0.0)"
57+
echo " $0 --patch --pre # Bump and mark as pre-release"
58+
exit 0
59+
;;
60+
*)
61+
VERSION="$1"
62+
shift
63+
;;
64+
esac
65+
66+
# Check for --pre flag after version/bump
67+
if [ "${1:-}" = "--pre" ]; then
68+
PRE_RELEASE=true
69+
fi
70+
71+
# Get current version from Cargo.toml
72+
CURRENT_VERSION=$(grep '^version = ' Cargo.toml | head -1 | sed 's/version = "\(.*\)"/\1/')
73+
print_info "Current version: $CURRENT_VERSION"
74+
75+
# Determine new version
76+
if [ -n "$VERSION" ]; then
77+
NEW_VERSION="$VERSION"
78+
elif [ -n "$BUMP_TYPE" ]; then
79+
# Parse current version
80+
IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION"
81+
82+
case $BUMP_TYPE in
83+
major)
84+
NEW_VERSION="$((MAJOR + 1)).0.0"
85+
;;
86+
minor)
87+
NEW_VERSION="${MAJOR}.$((MINOR + 1)).0"
88+
;;
89+
patch)
90+
NEW_VERSION="${MAJOR}.${MINOR}.$((PATCH + 1))"
91+
;;
92+
*)
93+
print_error "Invalid bump type: $BUMP_TYPE. Use 'major', 'minor', or 'patch'"
94+
exit 1
95+
;;
96+
esac
97+
else
98+
print_error "No version specified. Use: $0 <version|--major|--minor|--patch>"
99+
exit 1
100+
fi
101+
102+
print_info "New version: $NEW_VERSION"
103+
104+
# Confirm with user
105+
echo ""
106+
read -p "Continue with release $NEW_VERSION? (y/N) " -n 1 -r
107+
echo
108+
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
109+
print_warning "Release cancelled"
110+
exit 0
111+
fi
112+
113+
# Check if working directory is clean
114+
if [ -n "$(git status --porcelain)" ]; then
115+
print_error "Working directory is not clean. Please commit or stash changes first."
116+
exit 1
117+
fi
118+
119+
# Update version in Cargo.toml
120+
print_info "Updating version in Cargo.toml..."
121+
sed -i.bak "s/^version = \"$CURRENT_VERSION\"/version = \"$NEW_VERSION\"/" Cargo.toml
122+
rm Cargo.toml.bak
123+
124+
# Verify the change
125+
if ! grep -q "^version = \"$NEW_VERSION\"" Cargo.toml; then
126+
print_error "Failed to update version in Cargo.toml"
127+
exit 1
128+
fi
129+
130+
# Commit the version bump
131+
print_info "Committing version bump..."
132+
git add Cargo.toml
133+
git commit -m "Bump version to $NEW_VERSION"
134+
135+
# Create git tag
136+
TAG_NAME="v$NEW_VERSION"
137+
print_info "Creating git tag: $TAG_NAME"
138+
git tag -a "$TAG_NAME" -m "Release $NEW_VERSION"
139+
140+
# Push to remote
141+
print_info "Pushing to remote..."
142+
git push
143+
git push origin "$TAG_NAME"
144+
145+
# Create GitHub release
146+
print_info "Creating GitHub release..."
147+
148+
# Prepare release notes with auto-generated content
149+
RELEASE_NOTES="## $NEW_VERSION
150+
151+
This is the $NEW_VERSION release of subway.
152+
153+
### Installation
154+
155+
\`\`\`bash
156+
cargo install subway --version $NEW_VERSION
157+
\`\`\`
158+
159+
Or download the prebuilt binary from the assets below.
160+
161+
### What's Changed
162+
163+
Full changelog: https://github.com/AcalaNetwork/subway/compare/v${CURRENT_VERSION}...v${NEW_VERSION}"
164+
165+
# Create the release
166+
if [ "$PRE_RELEASE" = true ]; then
167+
gh release create "$TAG_NAME" \
168+
--title "$TAG_NAME" \
169+
--notes "$RELEASE_NOTES" \
170+
--prerelease
171+
else
172+
gh release create "$TAG_NAME" \
173+
--title "$TAG_NAME" \
174+
--notes "$RELEASE_NOTES"
175+
fi
176+
177+
print_info "Release $NEW_VERSION created successfully!"
178+
print_info "Release page: https://github.com/AcalaNetwork/subway/releases/tag/$TAG_NAME"

0 commit comments

Comments
 (0)