Skip to content

Commit 4237d07

Browse files
committed
build(release): improve version release strategy
1 parent 806fa3a commit 4237d07

File tree

2 files changed

+73
-72
lines changed

2 files changed

+73
-72
lines changed

.github/workflows/cd.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
name: CD
22
on:
33
push:
4-
branches: [production, docs]
4+
tags:
5+
- "v[0-9]+.[0-9]+.[0-9]+"
6+
branches:
7+
- docs
58

69
jobs:
710
launch:

tools/release

Lines changed: 69 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,16 @@
1414

1515
set -eu
1616

17-
opt_pre=false # preview mode option
18-
opt_skip_ver=false # option for skip versioning
17+
opt_pre=false # preview mode option
1918

2019
working_branch="$(git branch --show-current)"
2120

21+
# AKA the default branch, main/master branch
2222
STAGING_BRANCH="$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')"
2323

2424
PROD_BRANCH="production"
2525

2626
GEM_SPEC="jekyll-theme-chirpy.gemspec"
27-
2827
NODE_CONFIG="package.json"
2928

3029
JS_DIST="assets/js/dist"
@@ -50,21 +49,30 @@ help() {
5049
echo " bash ./tools/release [options]"
5150
echo
5251
echo "Options:"
53-
echo " -k, --skip-versioning Skip the step of generating the version number."
5452
echo " -p, --preview Enable preview mode, only package, and will not modify the branches"
5553
echo " -h, --help Print this information."
5654
}
5755

56+
_check_cli() {
57+
for i in "${!TOOLS[@]}"; do
58+
cli="${TOOLS[$i]}"
59+
if ! command -v "$cli" &>/dev/null; then
60+
echo "> Command '$cli' not found!"
61+
exit 1
62+
fi
63+
done
64+
}
65+
5866
_check_git() {
5967
# ensure nothing is uncommitted
6068
if [[ -n $(git status . -s) ]]; then
61-
echo "Abort: Commit the staged files first, and then run this tool again."
69+
echo "> Abort: Commit the staged files first, and then run this tool again."
6270
exit 1
6371
fi
6472

65-
# ensure the working branch is the main/patch branch
73+
# ensure the working branch is the default/patch branch
6674
if [[ $working_branch != "$STAGING_BRANCH" && $working_branch != hotfix/* ]]; then
67-
echo "Abort: Please run on the main branch or patch branches."
75+
echo "> Abort: Please run on the $STAGING_BRANCH branch or a patch branche."
6876
exit 1
6977
fi
7078
}
@@ -73,18 +81,7 @@ _check_src() {
7381
for i in "${!FILES[@]}"; do
7482
_src="${FILES[$i]}"
7583
if [[ ! -f $_src && ! -d $_src ]]; then
76-
echo -e "Error: Missing file \"$_src\"!\n"
77-
exit 1
78-
fi
79-
done
80-
81-
}
82-
83-
_check_command() {
84-
for i in "${!TOOLS[@]}"; do
85-
cli="${TOOLS[$i]}"
86-
if ! command -v "$cli" &>/dev/null; then
87-
echo "Command '$cli' not found!"
84+
echo -e "> Error: Missing file \"$_src\"!\n"
8885
exit 1
8986
fi
9087
done
@@ -97,26 +94,55 @@ _check_node_packages() {
9794
}
9895

9996
check() {
100-
_check_command
97+
_check_cli
10198
_check_git
10299
_check_src
103100
_check_node_packages
104101
}
105102

106-
## Bump latest version number and create a commit to save the changes
107-
bump() {
103+
# auto-generate a new version number to the file 'package.json' and
104+
bump_node() {
105+
if $opt_pre; then
106+
standard-version --prerelease rc
107+
else
108+
standard-version
109+
fi
110+
111+
# Change heading of Patch version to heading level 2 (a bug from `standard-version`)
112+
sed -i "s/^### \[/## \[/g" CHANGELOG.md
113+
# Replace multiple empty lines with a single empty line
114+
sed -i "/^$/N;/^\n$/D" CHANGELOG.md
115+
}
116+
117+
## Bump new version to gem config file
118+
bump_gem() {
108119
sed -i "s/[[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+/$1/" "$GEM_SPEC"
120+
}
109121

110-
if [[ $opt_pre = false && -n $(git status . -s) ]]; then
111-
git add .
112-
git commit -m "chore(release): $1"
122+
# Update the git branches, create a new tag, and then build the gem package.
123+
release() {
124+
_version="$1" # X.Y.Z
125+
_latest_commit="$(git rev-parse HEAD)"
126+
127+
# Create a new tag on production branch
128+
echo -e "> Create tag v$_version\n"
129+
git tag "v$_version"
130+
131+
git checkout "$STAGING_BRANCH"
132+
133+
if [[ $working_branch == hotfix/* ]]; then
134+
git merge --no-ff --no-edit "$working_branch"
135+
# delete the patch branch
136+
git branch -D "$working_branch"
113137
fi
138+
139+
# cherry-pick the latest commit from production branch to default branch
140+
git cherry-pick "$_latest_commit"
141+
114142
}
115143

116144
## Build a gem package
117145
build_gem() {
118-
echo -e "Build the gem package for v$_version\n"
119-
120146
# Remove unnecessary theme settings
121147
sed -i "s/^img_cdn:.*/img_cdn:/;s/^avatar:.*/avatar:/" _config.yml
122148
rm -f ./*.gem
@@ -129,60 +155,36 @@ build_gem() {
129155
# Resume the settings
130156
git reset
131157
git checkout .
132-
}
133158

134-
# Update the git branch graph, tag, and then build the gem package.
135-
release() {
136-
_version="$1" # X.Y.Z
137-
138-
git checkout "$PROD_BRANCH"
139-
git merge --no-ff --no-edit "$working_branch"
140-
141-
# Create a new tag on working branch
142-
echo -e "Create tag v$_version\n"
143-
git tag "v$_version"
144-
145-
# Merge from patch branch to the staging branch
146-
if [[ $working_branch == hotfix/* ]]; then
147-
git checkout "$STAGING_BRANCH"
148-
git merge --no-ff --no-edit "$working_branch"
149-
git branch -D "$working_branch"
150-
fi
159+
# restore the dist files for future development
160+
mkdir -p "$JS_DIST" && cp "$BACKUP_PATH"/* "$JS_DIST"
151161
}
152162

153163
main() {
154-
if [[ $opt_skip_ver = false ]]; then
155-
check
156-
157-
# auto-generate a new version number to the file 'package.json'
158-
if $opt_pre; then
159-
standard-version --prerelease rc
160-
else
161-
standard-version
162-
fi
164+
check
165+
166+
if [[ $opt_pre = false ]]; then
167+
git checkout "$PROD_BRANCH"
168+
git merge --no-ff --no-edit "$working_branch"
163169
fi
164170

165-
# Change heading of Patch version to level 2 (a bug from `standard-version`)
166-
sed -i "s/^### \[/## \[/g" CHANGELOG.md
167-
# Replace multiple empty lines with a single empty line
168-
sed -i "/^$/N;/^\n$/D" CHANGELOG.md
171+
bump_node
169172

170173
_version="$(grep '"version":' "$NODE_CONFIG" | sed 's/.*: "//;s/".*//')"
171174

172-
echo -e "Bump version number to $_version\n"
173-
bump "$_version"
175+
bump_gem "$_version"
174176

175-
build_gem
177+
echo -e "> Build the gem package for v$_version\n"
178+
179+
if [[ $opt_pre = false ]]; then
180+
echo -e "> Bumped version number to $_version\n"
181+
git add .
182+
git commit -m "chore(release): $_version"
176183

177-
if [[ $opt_pre = true ]]; then
178-
# Undo all changes on Git
179-
git reset --hard && git clean -fd
180-
else
181184
release "$_version"
182185
fi
183186

184-
# restore the dist files for future development
185-
mkdir -p "$JS_DIST" && cp "$BACKUP_PATH"/* "$JS_DIST"
187+
build_gem
186188
}
187189

188190
while (($#)); do
@@ -192,10 +194,6 @@ while (($#)); do
192194
opt_pre=true
193195
shift
194196
;;
195-
-k | --skip-versioning)
196-
opt_skip_ver=true
197-
shift
198-
;;
199197
-h | --help)
200198
help
201199
exit 0

0 commit comments

Comments
 (0)