我有一个python应用程序,我想实现语义版本控制:MAJOR.MINOR.PATCH.BUILD。我想通过bitbucket管道尽可能地自动化它。
我将在下面回答我自己的问题,与其他人分享我是如何做到这一点的,因为博客帖子/资源很少。
答案 0 :(得分:0)
下面列出了所有相关文件,但这里有它的要点。我有一个master分支,在合并到master之前我会从中创建各种功能分支。我已将每个合并标记为主分支作为次要更新。这通过以下代码完全自动化。构建号也是自动化的,并且每次新提交都会更新,无论我在哪个分支。最后,补丁号码和主要号码更新是通过BB管道中的触发器半自动的。
我将版本号存储在version.txt文件中,每个构建版本我都会更新该编号并在该版本中运行提交和推送,但是使用[跳过CI]跳过该次要提交的构建过程,否则它将循环无限地。如果需要,很乐意进一步解释。
version.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
很高兴就如何改进我的方法提出任何建议。