Lud*_*udo 5 bash bitbucket bitbucket-pipelines
我有一个 python 应用程序,我想实现语义版本控制:MAJOR.MINOR.PATCH.BUILD。我想通过 bitbucket 管道尽可能地自动化它。
我将在下面回答我自己的问题,与其他人分享由于博客文章/资源的稀缺性,我是如何做到这一点的。
Lud*_*udo 10
所有相关文件都包含在下面,但这里是它的要点。我有一个主分支,在合并到主分支之前,我从中创建了各种功能分支。我将每次合并到 master 分支都标记为次要更新。这是通过下面的代码完全自动化的。无论我在哪个分支上,内部版本号也会随着每次新提交而自动更新和更新。最后,补丁编号和主编号更新通过 BB 管道中的触发器是半自动的。
我将版本号存储在 version.txt 文件中,每次构建我都会更新该编号并从该构建中运行提交和推送,但使用 [skip CI] 跳过该二次提交的构建过程,否则它将无限循环。如果需要,很乐意进一步解释任何事情。
version.sh 脚本的一部分来自linux上的一个答案- 简化语义版本控制脚本
这是我的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
Run Code Online (Sandbox Code Playgroud)
和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
Run Code Online (Sandbox Code Playgroud)
最后version.txt
是一个简单的文本文件,四个数字用一个点隔开,如 4.3.2.1
很高兴就如何改进我的方法提出任何建议。