app-starlock/tag_generator.sh

73 lines
2.6 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 读取环境变量
URL=$CI_API_V4_URL
TOKEN=$GITLAB_ACCESS_TOKEN
PROJECT_ID=$CI_PROJECT_ID
next_tag=""
newest_tag=""
echo "PRIVATE-TOKEN: $TOKEN $URL/projects/$PROJECT_ID/repository/tags"
tags_json=$(curl -H "Content-Type: application/json" -H "PRIVATE-TOKEN: $TOKEN" "$URL/projects/$PROJECT_ID/repository/tags")
#echo "tags_json:$tags_json\n"
tags=$(echo "$tags_json" | jq -r '.[].name')
tags_length=$(echo "$tags_json" | jq -r 'length')
if [ "$tags_length" -lt 1 ]; then
next_tag="v1.0.0"
else
newest_tag=$(echo "$tags" | head -n 1)
# 去除已有的sky_前缀防止重复
base_tag=${newest_tag#sky_}
IFS='.' read -r major minor patch <<< "$base_tag"
major="${major#v}"
compare_json=""
if [[ "$1" == "generate_tag" ]];then
echo "generate_tag:$newest_tag-to-master_sky\n"
compare_json=$(curl -s --header "PRIVATE-TOKEN: $TOKEN" "$URL/projects/$PROJECT_ID/repository/compare?from=$newest_tag&to=master_sky")
elif [[ "$1" == "generate_version" ]]; then
echo "generate_version:master_sky-to-$CI_COMMIT_BRANCH\n"
compare_json=$(curl -s --header "PRIVATE-TOKEN: $TOKEN" "$URL/projects/$PROJECT_ID/repository/compare?from=master_sky&to=$CI_COMMIT_BRANCH")
fi
echo "compare_json:$compare_json\n"
new_patch=$patch
new_minor=$minor
while IFS= read -r commit_json; do
# 使用 jq 解析每一行的 JSON 对象
commit_id=$(echo "$commit_json" | jq -r '.id')
commit_message=$(echo "$commit_json" | jq -r '.message')
echo "----$commit_message"
if [[ "$commit_message" =~ ("feat:"*) ]] && [[ $new_minor == $minor ]]; then
((new_minor++))
# new_patch=0
# break
elif [[ "$commit_message" =~ ("fix:"*) ]]; then
((new_patch++))
elif [[ ! "$commit_message" =~ ("Merge"* | "Revert"*) ]]; then
((new_patch++))
fi
done < <(echo "$compare_json" | jq -c '.commits[] | {id: .id, message: .message}')
next_tag="v$major.$new_minor.$new_patch"
fi
# 生成新tag名时若在master_sky分支加前缀sky_
branch_name="$CI_COMMIT_BRANCH"
if [[ "$branch_name" == "master_sky" ]]; then
next_tag="sky_${next_tag}"
fi
echo "New Tag:$newest_tag;New version: $next_tag;command: $1"
if [[ "$1" == "generate_tag" ]];then
if [ "$next_tag" == "$newest_tag" ]; then
echo "no change from master,skip to generate tag"
exit 0
fi
git config user.name
git tag $next_tag
git push -u origin $next_tag
echo "generate tag: $next_tag"
elif [[ "$1" == "generate_version" ]]; then
export NEXT_VERSION="$next_tag"
echo "generate version: $NEXT_VERSION"
fi
echo "$next_tag" > app_new.version