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
|
|
|
#
|
|
|
|
#
|
2022-03-01 18:24:51 +03:00
|
|
|
# Usage: run on main branch or the patch branch
|
|
|
|
#
|
2022-03-13 11:53:13 +03:00
|
|
|
# Requires: Git, NPM and RubyGems
|
2021-09-28 18:17:39 +03:00
|
|
|
|
|
|
|
set -eu
|
|
|
|
|
2022-03-01 18:24:51 +03:00
|
|
|
opt_pre=false # preview mode option
|
|
|
|
|
|
|
|
working_branch="$(git branch --show-current)"
|
|
|
|
|
|
|
|
STAGING_BRANCH="$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')"
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
FILES=(
|
|
|
|
"_sass/jekyll-theme-chirpy.scss"
|
|
|
|
"_javascript/copyright"
|
|
|
|
"$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
|
|
|
|
echo " bash ./tools/release.sh [options]"
|
|
|
|
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."
|
|
|
|
}
|
|
|
|
|
2022-03-01 18:24:51 +03:00
|
|
|
_check_git() {
|
|
|
|
# ensure nothing is uncommitted
|
2021-09-28 18:17:39 +03:00
|
|
|
if [[ -n $(git status . -s) ]]; then
|
2022-03-01 18:24:51 +03:00
|
|
|
echo "Abort: Commit the staged files first, and then run this tool again."
|
|
|
|
exit 1
|
2021-09-28 18:17:39 +03:00
|
|
|
fi
|
|
|
|
|
2022-03-01 18:24:51 +03:00
|
|
|
# ensure the working branch is the main/patch branch
|
|
|
|
if [[ $working_branch != "$STAGING_BRANCH" && $working_branch != hotfix/* ]]; then
|
|
|
|
echo "Abort: Please run on the main branch or patch branches."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
_check_src() {
|
|
|
|
if [[ ! -f $1 && ! -d $1 ]]; then
|
|
|
|
echo -e "Error: Missing file \"$1\"!\n"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
_check_command() {
|
|
|
|
if ! command -v "$1" &>/dev/null; then
|
|
|
|
echo "Command '$1' not found"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
_check_node_packages() {
|
|
|
|
if [[ ! -d node_modules || "$(du node_modules | awk '{print $1}')" == "0" ]]; then
|
|
|
|
npm i
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
check() {
|
|
|
|
_check_git
|
|
|
|
|
|
|
|
for i in "${!FILES[@]}"; do
|
|
|
|
_check_src "${FILES[$i]}"
|
|
|
|
done
|
|
|
|
|
|
|
|
for i in "${!TOOLS[@]}"; do
|
|
|
|
_check_command "${TOOLS[$i]}"
|
|
|
|
done
|
|
|
|
|
|
|
|
_check_node_packages
|
|
|
|
}
|
|
|
|
|
|
|
|
_bump_file() {
|
|
|
|
for i in "${!FILES[@]}"; do
|
|
|
|
sed -i "s/v[[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+/v$1/" "${FILES[$i]}"
|
|
|
|
done
|
|
|
|
|
|
|
|
npx gulp
|
|
|
|
}
|
|
|
|
|
|
|
|
_bump_gemspec() {
|
|
|
|
sed -i "s/[[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+/$1/" "$GEM_SPEC"
|
|
|
|
}
|
|
|
|
|
|
|
|
# 1. Bump latest version number to the following files:
|
|
|
|
#
|
|
|
|
# - _sass/jekyll-theme-chirpy.scss
|
|
|
|
# - _javascript/copyright
|
|
|
|
# - assets/js/dist/*.js (will be built by gulp later)
|
|
|
|
# - jekyll-theme-chirpy.gemspec
|
|
|
|
#
|
|
|
|
# 2. Create a commit to save the changes.
|
|
|
|
bump() {
|
|
|
|
_bump_file "$1"
|
|
|
|
_bump_gemspec "$1"
|
|
|
|
|
|
|
|
if [[ $opt_pre = false && -n $(git status . -s) ]]; then
|
|
|
|
git add .
|
|
|
|
git commit -m "chore(release): $1"
|
2021-09-28 18:17:39 +03:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-10-03 13:39:00 +03:00
|
|
|
## Remove unnecessary theme settings
|
|
|
|
cleanup_config() {
|
|
|
|
cp _config.yml _config.yml.bak
|
|
|
|
sed -i "s/^img_cdn:.*/img_cdn:/;s/^avatar:.*/avatar:/" _config.yml
|
|
|
|
}
|
|
|
|
|
|
|
|
resume_config() {
|
|
|
|
mv _config.yml.bak _config.yml
|
|
|
|
}
|
|
|
|
|
2022-03-01 18:24:51 +03:00
|
|
|
# auto-generate a new version number to the file 'package.json'
|
|
|
|
standard_version() {
|
|
|
|
if $opt_pre; then
|
2022-03-13 11:53:13 +03:00
|
|
|
standard-version --prerelease rc
|
2022-03-01 18:24:51 +03:00
|
|
|
else
|
2022-03-13 11:53:13 +03:00
|
|
|
standard-version
|
2022-02-14 18:59:17 +03:00
|
|
|
fi
|
2022-03-01 18:24:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# Prevent changelogs generated on master branch from having duplicate content
|
|
|
|
# (the another bug of `standard-version`)
|
|
|
|
standard_version_plus() {
|
|
|
|
temp_branch="prod-mirror"
|
|
|
|
temp_dir="$(mktemp -d)"
|
|
|
|
|
|
|
|
git checkout -b "$temp_branch" "$PROD_BRANCH"
|
|
|
|
git merge --no-ff --no-edit "$STAGING_BRANCH"
|
2021-09-28 18:17:39 +03:00
|
|
|
|
2022-03-01 18:24:51 +03:00
|
|
|
standard_version
|
|
|
|
|
|
|
|
cp package.json CHANGELOG.md "$temp_dir"
|
|
|
|
|
|
|
|
git checkout "$STAGING_BRANCH"
|
|
|
|
git reset --hard HEAD # undo the changes from $temp_branch
|
|
|
|
mv "$temp_dir"/* . # rewrite the changelog
|
|
|
|
|
|
|
|
# clean up the temp stuff
|
|
|
|
rm -rf "$temp_dir"
|
|
|
|
git branch -D "$temp_branch"
|
|
|
|
}
|
|
|
|
|
|
|
|
# build a gem package
|
|
|
|
build_gem() {
|
|
|
|
echo -e "Build the gem package for v$_version\n"
|
2021-10-03 13:39:00 +03:00
|
|
|
cleanup_config
|
2021-09-28 18:17:39 +03:00
|
|
|
rm -f ./*.gem
|
|
|
|
gem build "$GEM_SPEC"
|
2021-10-03 13:39:00 +03:00
|
|
|
resume_config
|
2021-09-28 18:17:39 +03:00
|
|
|
}
|
|
|
|
|
2022-03-01 18:24:51 +03:00
|
|
|
# Update the git branch graph, tag, and then build the gem package.
|
|
|
|
release() {
|
|
|
|
_version="$1" # X.Y.Z
|
|
|
|
|
|
|
|
if $opt_pre; then
|
|
|
|
$opt_pre
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
git checkout "$PROD_BRANCH"
|
|
|
|
git merge --no-ff --no-edit "$working_branch"
|
|
|
|
|
|
|
|
# Create a new tag on production branch
|
|
|
|
echo -e "Create tag v$_version\n"
|
|
|
|
git tag "v$_version"
|
|
|
|
|
|
|
|
build_gem
|
|
|
|
|
|
|
|
# merge from patch branch to the staging branch
|
|
|
|
# NOTE: This may break due to merge conflicts, so it may need to be resolved manually.
|
|
|
|
if [[ $working_branch == hotfix/* ]]; then
|
|
|
|
git checkout "$STAGING_BRANCH"
|
|
|
|
git merge --no-ff --no-edit "$working_branch"
|
|
|
|
git branch -D "$working_branch"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-09-28 18:17:39 +03:00
|
|
|
main() {
|
|
|
|
check
|
2022-03-01 18:24:51 +03:00
|
|
|
|
|
|
|
if [[ "$working_branch" == "$STAGING_BRANCH" ]]; then
|
|
|
|
standard_version_plus
|
|
|
|
else
|
|
|
|
standard_version
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Change heading of Patch version to level 2 (a bug from `standard-version`)
|
|
|
|
sed -i "s/^### \[/## \[/g" CHANGELOG.md
|
|
|
|
|
|
|
|
_version="$(grep '"version":' package.json | sed 's/.*: "//;s/".*//')"
|
|
|
|
|
|
|
|
echo -e "Bump version number to $_version\n"
|
|
|
|
bump "$_version"
|
|
|
|
|
|
|
|
release "$_version"
|
|
|
|
|
|
|
|
# Undo all changes on Git
|
|
|
|
$opt_pre && git reset --hard && git clean -fd
|
|
|
|
|
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
|