2021-09-28 18:17:39 +03:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
2022-03-01 18:24:51 +03:00
|
|
|
# Release a new version to the GitLab flow production branch.
|
2021-09-28 18:17:39 +03:00
|
|
|
#
|
2022-03-01 18:24:51 +03:00
|
|
|
# For a new major/minor version, bump version on the main branch, and then merge into the production branch.
|
2021-09-28 18:17:39 +03:00
|
|
|
#
|
2022-03-01 18:24:51 +03:00
|
|
|
# For a patch version, bump the version number on the patch branch, then merge that branch into the main branch
|
|
|
|
# and production branch.
|
2021-09-28 18:17:39 +03:00
|
|
|
#
|
|
|
|
#
|
2023-11-06 22:38:02 +03:00
|
|
|
# Usage: run on the default, release or the patch branch
|
2022-03-01 18:24:51 +03:00
|
|
|
#
|
2022-03-13 11:53:13 +03:00
|
|
|
# Requires: Git, NPM and RubyGems
|
2021-09-28 18:17:39 +03:00
|
|
|
|
|
|
|
set -eu
|
|
|
|
|
2023-09-10 12:25:41 +03:00
|
|
|
opt_pre=false # preview mode option
|
2022-03-01 18:24:51 +03:00
|
|
|
|
|
|
|
working_branch="$(git branch --show-current)"
|
|
|
|
|
2023-11-06 22:38:02 +03:00
|
|
|
DEFAULT_BRANCH="$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')"
|
2022-03-01 18:24:51 +03:00
|
|
|
|
|
|
|
PROD_BRANCH="production"
|
|
|
|
|
2021-09-28 18:17:39 +03:00
|
|
|
GEM_SPEC="jekyll-theme-chirpy.gemspec"
|
2022-03-01 18:24:51 +03:00
|
|
|
NODE_CONFIG="package.json"
|
2023-09-30 20:49:20 +03:00
|
|
|
CHANGE_LOG="docs/CHANGELOG.md"
|
2022-03-01 18:24:51 +03:00
|
|
|
|
2023-03-17 20:33:44 +03:00
|
|
|
JS_DIST="assets/js/dist"
|
|
|
|
BACKUP_PATH="$(mktemp -d)"
|
|
|
|
|
2022-03-01 18:24:51 +03:00
|
|
|
FILES=(
|
|
|
|
"$GEM_SPEC"
|
|
|
|
"$NODE_CONFIG"
|
|
|
|
)
|
|
|
|
|
|
|
|
TOOLS=(
|
|
|
|
"git"
|
|
|
|
"npm"
|
2022-03-13 11:53:13 +03:00
|
|
|
"standard-version"
|
2022-03-01 18:24:51 +03:00
|
|
|
"gem"
|
|
|
|
)
|
2022-02-14 18:59:17 +03:00
|
|
|
|
|
|
|
help() {
|
|
|
|
echo "A tool to release new version Chirpy gem"
|
|
|
|
echo
|
|
|
|
echo "Usage:"
|
|
|
|
echo
|
2023-06-28 08:49:13 +03:00
|
|
|
echo " bash ./tools/release [options]"
|
2022-02-14 18:59:17 +03:00
|
|
|
echo
|
|
|
|
echo "Options:"
|
2022-03-01 18:24:51 +03:00
|
|
|
echo " -p, --preview Enable preview mode, only package, and will not modify the branches"
|
2022-02-14 18:59:17 +03:00
|
|
|
echo " -h, --help Print this information."
|
|
|
|
}
|
|
|
|
|
2023-09-10 12:25:41 +03:00
|
|
|
_check_cli() {
|
|
|
|
for i in "${!TOOLS[@]}"; do
|
|
|
|
cli="${TOOLS[$i]}"
|
|
|
|
if ! command -v "$cli" &>/dev/null; then
|
|
|
|
echo "> Command '$cli' not found!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2022-03-01 18:24:51 +03:00
|
|
|
_check_git() {
|
2023-11-06 22:38:02 +03:00
|
|
|
# ensure that changes have been committed
|
2021-09-28 18:17:39 +03:00
|
|
|
if [[ -n $(git status . -s) ]]; then
|
2023-09-10 12:25:41 +03:00
|
|
|
echo "> Abort: Commit the staged files first, and then run this tool again."
|
2022-03-01 18:24:51 +03:00
|
|
|
exit 1
|
2021-09-28 18:17:39 +03:00
|
|
|
fi
|
|
|
|
|
2024-01-27 21:22:33 +03:00
|
|
|
$opt_pre || (
|
|
|
|
if [[ $working_branch != "$DEFAULT_BRANCH" &&
|
|
|
|
$working_branch != hotfix/* &&
|
|
|
|
$working_branch != "$PROD_BRANCH" ]]; then
|
|
|
|
echo "> Abort: Please run on the default, release or patch branch."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
)
|
2022-03-01 18:24:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_check_src() {
|
2023-03-13 20:25:42 +03:00
|
|
|
for i in "${!FILES[@]}"; do
|
|
|
|
_src="${FILES[$i]}"
|
|
|
|
if [[ ! -f $_src && ! -d $_src ]]; then
|
2023-09-10 12:25:41 +03:00
|
|
|
echo -e "> Error: Missing file \"$_src\"!\n"
|
2023-03-13 20:25:42 +03:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
2022-03-01 18:24:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_check_node_packages() {
|
|
|
|
if [[ ! -d node_modules || "$(du node_modules | awk '{print $1}')" == "0" ]]; then
|
|
|
|
npm i
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
check() {
|
2023-09-10 12:25:41 +03:00
|
|
|
_check_cli
|
2022-03-01 18:24:51 +03:00
|
|
|
_check_git
|
2023-03-13 20:25:42 +03:00
|
|
|
_check_src
|
2022-03-01 18:24:51 +03:00
|
|
|
_check_node_packages
|
|
|
|
}
|
|
|
|
|
2023-11-06 22:38:02 +03:00
|
|
|
# Auto-generate a new version number to the file 'package.json'
|
2023-09-10 12:25:41 +03:00
|
|
|
bump_node() {
|
2023-11-06 22:38:02 +03:00
|
|
|
bump="standard-version -i $CHANGE_LOG"
|
|
|
|
|
2023-09-10 12:25:41 +03:00
|
|
|
if $opt_pre; then
|
2023-11-06 22:38:02 +03:00
|
|
|
bump="$bump -p rc"
|
2023-09-10 12:25:41 +03:00
|
|
|
fi
|
|
|
|
|
2023-11-06 22:38:02 +03:00
|
|
|
eval "$bump"
|
|
|
|
|
2023-09-10 12:25:41 +03:00
|
|
|
# Change heading of Patch version to heading level 2 (a bug from `standard-version`)
|
2023-09-30 20:49:20 +03:00
|
|
|
sed -i "s/^### \[/## \[/g" "$CHANGE_LOG"
|
2023-09-10 12:25:41 +03:00
|
|
|
# Replace multiple empty lines with a single empty line
|
2023-09-30 20:49:20 +03:00
|
|
|
sed -i "/^$/N;/^\n$/D" "$CHANGE_LOG"
|
2023-09-10 12:25:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
## Bump new version to gem config file
|
|
|
|
bump_gem() {
|
2023-11-06 22:38:02 +03:00
|
|
|
_ver="$1"
|
|
|
|
|
|
|
|
if $opt_pre; then
|
|
|
|
_ver="${1/-/.}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
sed -i "s/[[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+/$_ver/" "$GEM_SPEC"
|
2023-09-10 12:25:41 +03:00
|
|
|
}
|
2022-03-01 18:24:51 +03:00
|
|
|
|
2023-11-06 22:38:02 +03:00
|
|
|
# Creates a new tag on the production branch with the given version number.
|
|
|
|
# Also commits the changes and merges the production branch into the default branch.
|
|
|
|
branch() {
|
2023-09-10 12:25:41 +03:00
|
|
|
_version="$1" # X.Y.Z
|
2023-11-06 22:38:02 +03:00
|
|
|
|
|
|
|
git add .
|
|
|
|
git commit -m "chore(release): $_version"
|
2023-09-10 12:25:41 +03:00
|
|
|
|
|
|
|
# Create a new tag on production branch
|
|
|
|
echo -e "> Create tag v$_version\n"
|
|
|
|
git tag "v$_version"
|
|
|
|
|
2023-11-06 22:38:02 +03:00
|
|
|
git checkout "$DEFAULT_BRANCH"
|
|
|
|
git merge --no-ff --no-edit "$PROD_BRANCH"
|
2023-09-10 12:25:41 +03:00
|
|
|
|
|
|
|
if [[ $working_branch == hotfix/* ]]; then
|
|
|
|
# delete the patch branch
|
|
|
|
git branch -D "$working_branch"
|
2021-09-28 18:17:39 +03:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-02-29 22:11:17 +03:00
|
|
|
## Build a Gem package
|
2022-03-01 18:24:51 +03:00
|
|
|
build_gem() {
|
2024-02-29 22:11:17 +03:00
|
|
|
git checkout "$PROD_BRANCH"
|
|
|
|
|
2023-09-10 07:39:18 +03:00
|
|
|
# Remove unnecessary theme settings
|
2024-04-12 23:34:48 +03:00
|
|
|
sed -i "s/^cdn:.*/cdn:/;s/^avatar:.*/avatar:/" _config.yml
|
2021-09-28 18:17:39 +03:00
|
|
|
rm -f ./*.gem
|
2023-09-10 07:39:18 +03:00
|
|
|
|
|
|
|
npm run build
|
2024-02-13 18:35:32 +03:00
|
|
|
git add "$JS_DIST" -f # add JS distribution files to gem
|
2021-09-28 18:17:39 +03:00
|
|
|
gem build "$GEM_SPEC"
|
2023-03-17 20:33:44 +03:00
|
|
|
cp "$JS_DIST"/* "$BACKUP_PATH"
|
2023-09-10 07:39:18 +03:00
|
|
|
|
|
|
|
# Resume the settings
|
|
|
|
git reset
|
|
|
|
git checkout .
|
2021-09-28 18:17:39 +03:00
|
|
|
|
2023-09-10 12:25:41 +03:00
|
|
|
# restore the dist files for future development
|
|
|
|
mkdir -p "$JS_DIST" && cp "$BACKUP_PATH"/* "$JS_DIST"
|
2024-02-29 22:11:17 +03:00
|
|
|
|
|
|
|
# back to the default branch
|
|
|
|
git checkout "$DEFAULT_BRANCH"
|
2022-03-01 18:24:51 +03:00
|
|
|
}
|
|
|
|
|
2021-09-28 18:17:39 +03:00
|
|
|
main() {
|
2023-09-10 12:25:41 +03:00
|
|
|
check
|
|
|
|
|
2023-11-06 22:38:02 +03:00
|
|
|
if [[ $opt_pre = false && $working_branch != "$PROD_BRANCH" ]]; then
|
2023-09-10 12:25:41 +03:00
|
|
|
git checkout "$PROD_BRANCH"
|
|
|
|
git merge --no-ff --no-edit "$working_branch"
|
2022-03-01 18:24:51 +03:00
|
|
|
fi
|
|
|
|
|
2023-09-10 12:25:41 +03:00
|
|
|
bump_node
|
2022-11-22 16:08:37 +03:00
|
|
|
|
2022-11-22 13:24:26 +03:00
|
|
|
_version="$(grep '"version":' "$NODE_CONFIG" | sed 's/.*: "//;s/".*//')"
|
2022-03-01 18:24:51 +03:00
|
|
|
|
2023-09-10 12:25:41 +03:00
|
|
|
bump_gem "$_version"
|
2022-03-01 18:24:51 +03:00
|
|
|
|
2023-09-10 12:25:41 +03:00
|
|
|
if [[ $opt_pre = false ]]; then
|
2023-11-06 22:38:02 +03:00
|
|
|
branch "$_version"
|
2022-06-09 10:01:27 +03:00
|
|
|
fi
|
2023-03-17 20:33:44 +03:00
|
|
|
|
2023-11-06 22:38:02 +03:00
|
|
|
echo -e "> Build the gem package for v$_version\n"
|
|
|
|
|
2023-09-10 12:25:41 +03:00
|
|
|
build_gem
|
2021-09-28 18:17:39 +03:00
|
|
|
}
|
|
|
|
|
2022-02-14 18:59:17 +03:00
|
|
|
while (($#)); do
|
|
|
|
opt="$1"
|
|
|
|
case $opt in
|
|
|
|
-p | --preview)
|
|
|
|
opt_pre=true
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-h | --help)
|
|
|
|
help
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
# unknown option
|
|
|
|
help
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2021-09-28 18:17:39 +03:00
|
|
|
main
|