Skip to content

Commit d4ddcf6

Browse files
authored
build: add release script (#338)
1 parent df240f3 commit d4ddcf6

1 file changed

Lines changed: 131 additions & 0 deletions

File tree

tools/create-release.sh

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#!/bin/bash
2+
3+
# @license
4+
# Copyright 2026 Google Inc.
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
set -e
8+
9+
# Change directory to the git repository root so the script works from any directory
10+
cd "$(git rev-parse --show-toplevel)"
11+
12+
# Parse arguments for dry-run option
13+
DRY_RUN=false
14+
for arg in "$@"; do
15+
if [ "$arg" = "--dry-run" ]; then
16+
DRY_RUN=true
17+
fi
18+
done
19+
20+
# 1. Check requirements
21+
if ! command -v gh &> /dev/null; then
22+
echo "Error: 'gh' CLI is required but not installed." >&2
23+
echo "Please install it first (e.g. 'brew install gh')." >&2
24+
exit 1
25+
fi
26+
27+
if ! gh auth status &> /dev/null; then
28+
echo "Error: You are not authenticated with 'gh' CLI." >&2
29+
echo "Please run 'gh auth login' first." >&2
30+
exit 1
31+
fi
32+
33+
# 2. Find the latest version from .release-please-manifest.json
34+
if [ ! -f .release-please-manifest.json ]; then
35+
echo "Error: .release-please-manifest.json not found." >&2
36+
exit 1
37+
fi
38+
39+
VERSION=$(node -p "require('./.release-please-manifest.json')['.']" 2>/dev/null)
40+
if [ -z "$VERSION" ]; then
41+
echo "Error: Failed to extract version from .release-please-manifest.json." >&2
42+
exit 1
43+
fi
44+
45+
TAG_NAME="webdriver-bidi-protocol-v$VERSION"
46+
RELEASE_TITLE="webdriver-bidi-protocol: v$VERSION"
47+
48+
echo "Latest release-please version: $VERSION"
49+
echo "Target Tag: $TAG_NAME"
50+
echo "Target Release Title: $RELEASE_TITLE"
51+
52+
if [ "$DRY_RUN" = true ]; then
53+
echo "--- RUNNING IN DRY RUN MODE ---"
54+
fi
55+
56+
# 3. Check if the release already exists on GitHub
57+
if gh release view "$TAG_NAME" &>/dev/null; then
58+
echo "GitHub release '$TAG_NAME' already exists. Nothing to do."
59+
exit 0
60+
fi
61+
62+
echo "GitHub release '$TAG_NAME' does not exist. Proceeding..."
63+
64+
# 4. Extract the changelog for this version from CHANGELOG.md
65+
export VERSION
66+
CHANGELOG=$(node << 'EOF' 2>/dev/null
67+
const fs = require('fs');
68+
if (!fs.existsSync('CHANGELOG.md')) {
69+
process.exit(1);
70+
}
71+
const content = fs.readFileSync('CHANGELOG.md', 'utf8');
72+
const version = process.env.VERSION;
73+
const escapedVersion = version.replace(/\./g, '\\.');
74+
const regex = new RegExp(`## \\[\\[?${escapedVersion}\\]?(?:\\([^)]+\\))?\\s*\\([^)]+\\)[\\s\\S]*?(?=\\n## |$)`);
75+
const match = content.match(regex);
76+
if (match) {
77+
console.log(match[0].split('\n').slice(1).join('\n').trim());
78+
} else {
79+
process.exit(1);
80+
}
81+
EOF
82+
)
83+
84+
if [ -z "$CHANGELOG" ]; then
85+
echo "Warning: Changelog for version $VERSION not found in CHANGELOG.md." >&2
86+
CHANGELOG="Changelog details for version $VERSION."
87+
fi
88+
89+
# 5. Tagging and releasing
90+
if [ "$DRY_RUN" = true ]; then
91+
echo "[DRY RUN] Would check if git tag '$TAG_NAME' exists locally..."
92+
if git rev-parse "$TAG_NAME" &>/dev/null; then
93+
echo "[DRY RUN] Git tag '$TAG_NAME' already exists locally."
94+
else
95+
echo "[DRY RUN] Would run: git tag \"$TAG_NAME\""
96+
echo "[DRY RUN] Would run: git push origin \"$TAG_NAME\""
97+
fi
98+
99+
echo "[DRY RUN] Would run: gh release create \"$TAG_NAME\" --title \"$RELEASE_TITLE\" --notes-file <temp_notes_file>"
100+
echo "[DRY RUN] Extracted changelog notes:"
101+
echo "--------------------------------------------"
102+
printf "%s\n" "$CHANGELOG"
103+
echo "--------------------------------------------"
104+
echo "[DRY RUN] Dry run complete!"
105+
else
106+
# Check and create local git tag
107+
if git rev-parse "$TAG_NAME" &>/dev/null; then
108+
echo "Git tag '$TAG_NAME' already exists locally."
109+
else
110+
echo "Creating git tag '$TAG_NAME'..."
111+
git tag "$TAG_NAME"
112+
113+
echo "Pushing tag '$TAG_NAME' to origin..."
114+
if ! git push origin "$TAG_NAME"; then
115+
echo "Warning: Failed to push tag to origin. It may already exist on remote." >&2
116+
fi
117+
fi
118+
119+
# Create a temporary file for notes to handle multi-line input and any double quotes perfectly
120+
NOTES_FILE=$(mktemp)
121+
trap 'rm -f "$NOTES_FILE"' EXIT
122+
printf "%s\n" "$CHANGELOG" > "$NOTES_FILE"
123+
124+
# Create GitHub release
125+
echo "Creating GitHub release..."
126+
gh release create "$TAG_NAME" \
127+
--title "$RELEASE_TITLE" \
128+
--notes-file "$NOTES_FILE"
129+
130+
echo "Successfully created GitHub release and tag for version $VERSION!"
131+
fi

0 commit comments

Comments
 (0)