set -e # Create tags in the bitbucket pipline, env from bitbucket pipeline source_branch=$(git log --format=%B -n 1 $BITBUCKET_COMMIT | awk '{print $3}')
if [[ -n $source_branch ]]; then # PART 1: Get the next tag echo"Triggered by pull request ${source_branch}"
if [[ ${source_branch} == feature/* ]]; then level=minor elif [[ ${source_branch} == bugfix/* ]]; then level=patch elif [[ ${source_branch} == hotfix/* ]]; then level=prerelease elif [[ ${source_branch} == major/* ]]; then level=major else echo"Nothing happen on branch: ${BITBUCKET_BRANCH}" echo"Source branch: ${source_branch}" fi
if [[ ! -z ${level} ]]; then # PART 2: Create src-tag and push back current_version=$(node -e "console.log(require('./package.json').version)") && echo"Current version - ${current_version}" echo"Crated version on branch: ${BITBUCKET_BRANCH}. Tag: ${current_version}"
echo"Build source tag: ${source_tag} on branch: ${BITBUCKET_BRANCH}" # Create the source tag and push back npm version ${next_version} --no-git-tag-version # This will not create a git tag just update the version in the package.json
git add --all git commit -m "[skip ci] ${current_version} --> ${next_version}"# [skip ci] Will not trigger this pipeline again git tag -am "[skip ci] source tag: ${source_tag}"${source_tag} git push origin ${BITBUCKET_BRANCH}${source_tag}
# PART 3: Add the dist folder and create release tag # Add the dist folder sed -i 's/dist//g' .gitignore
# build ts npm run build
# push to tag git add --all git commit -m "[skip ci] Build release tag: ${next_version} on branch: ${BITBUCKET_BRANCH}" git tag -am "[skip ci] release tag: ${next_version}"${next_version}# only push the tag git push origin refs/tags/${next_version} fi fi
Shell 脚本最好放在 Deployment 中执行。因为 Deployment 是阻塞队列,可以保证同一时间只有一个脚本在运行,从而避免 tag 重复冲突的问题。
同时对于开发分支的命名也需要做一定的限制。比如可以使用 husky 等工具。
拓展
这一套 Git flow 可以在任何的代码托管工具使用,即便没有类似 Pipeline/GitAction 的触发方式同样也可以通过跑脚本的方式实现。同时可以实现自动打 tag 的功能。