下面列出了所有相关文件,但这里是其要点。我有一个主分支,在合并到主分支之前,我从中创建各种功能分支。我已将每个合并到主分支的操作标记为次要更新。通过下面的代码,这是完全自动化的。无论我在哪个分支上,构建编号也会随着每次新提交而自动更新。最后,通过BB管道中的触发器,补丁编号和主要编号更新是半自动的。
我将版本号存储在版本中。txt文件和每个构建我都会更新编号,并在该构建中运行提交和推送,但使用[跳过CI]跳过该次提交的构建过程,否则它将无限循环。如有需要,欢迎进一步解释。
版本的一部分。sh脚本来源于
linux - simplify semantic versioning script
这是我的
bitbucket-pipelines.yml
文件如下所示:
image: python:3.6
pipelines:
default:
- step:
script: # Modify the commands below to build your repository.
# Linting check
- pip3 --version
- pip3 install -r requirements.txt
- pylint backend
- bash version.sh build $BITBUCKET_BUILD_NUMBER $BB_AUTH_STRING
branches:
master:
-step:
name: Minor version update
script:
# Linting check
- pip3 --version
- pip3 install -r requirements.txt
- pylint backend
# Increase minor version number
- bash version.sh minor $BITBUCKET_BUILD_NUMBER $BB_AUTH_STRING
-step:
trigger: manual
name: Major version update
script:
- bash version.sh major $BITBUCKET_BUILD_NUMBER $BB_AUTH_STRING
-step:
trigger: manual
name: Patch version update
script:
- bash version.sh patch $BITBUCKET_BUILD_NUMBER $BB_AUTH_STRING
和
version.sh
:
#!/bin/bash
#CONFIG
USER=""
REPO=""
USERNAME=""
EMAIL=""
major() {
if IFS=. read -r major rest <version.txt || [ -n "$major" ]; then
echo "$((major + 1)).0.0.$1" >"version.txt"
else
echo "ERROR: Unable to read version number from version.txt" >&2
exit 1
fi
}
minor() {
if IFS=. read -r major minor patch build <version.txt || [ -n "$major" ]; then
echo "$major.$((minor + 1)).0.$1" >"version.txt"
else
echo "ERROR: Unable to read version number from version.txt" >&2
exit 1
fi
}
# Need to substract one from minor because automatically runs
patch() {
if IFS=. read -r major minor patch build <version.txt || [ -n "$major" ]; then
echo "$major.$((minor - 1)).$((patch + 1)).$1" >"version.txt"
else
echo "ERROR: Unable to read version number from version.txt" >&2
exit 1
fi
}
build() {
if IFS=. read -r major minor patch build <version.txt || [ -n "$major" ]; then
echo "$major.$minor.$patch.$1" >"version.txt"
else
echo "ERROR: Unable to read version number from version.txt" >&2
exit 1
fi
}
update() {
echo "New version = $(<version.txt)"
git config --global push.default simple
git remote set-url origin https://${1}@bitbucket.org/${USER}/${REPO}.git
git config user.name $USERNAME
git config user.email $EMAIL
git config -l
git add version.txt
git commit -m "[skip CI]"
git push
}
case "$1" in
major)
major $2
update $3
;;
minor)
minor $2
update $3
;;
patch)
patch $2
update $3
;;
build)
build $2
update $3
;;
*)
echo "Usage: bash version.sh {major|minor|patch|build} build_number bb_auth_string"
exit 1
esac
exit 0
最后
version.txt
是一个简单的文本文件,四个数字之间用点分隔,如4.3.2.1
很乐意接受关于如何改进我的方法的任何建议。