app-starlock/tag_generator.sh
2024-10-22 09:58:30 +08:00

63 lines
2.3 KiB
Bash
Executable File

#!/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)
IFS='.' read -r major minor patch <<< "$newest_tag"
major="${major#v}"
compare_json=""
if [[ "$1" == "generate_tag" ]];then
echo "generate_tag:$newest_tag-to-master\n"
compare_json=$(curl -s --header "PRIVATE-TOKEN: $TOKEN" "$URL/projects/$PROJECT_ID/repository/compare?from=$newest_tag&to=master")
elif [[ "$1" == "generate_version" ]]; then
echo "generate_version:master-to-$CI_COMMIT_BRANCH\n"
compare_json=$(curl -s --header "PRIVATE-TOKEN: $TOKEN" "$URL/projects/$PROJECT_ID/repository/compare?from=master&to=$CI_COMMIT_BRANCH")
fi
echo "compare_json:$compare_json\n"
new_patch=$patch
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:"*) ]]; then
((minor++))
new_patch=0
break
elif [[ "$commit_message" =~ ("fix:"*) ]] && [[ $new_patch == $patch ]]; then
((new_patch++))
elif [[ ! "$commit_message" =~ ("Merge"* | "Revert"*) ]] && [[ $new_patch == $patch ]]; then
((new_patch++))
fi
done < <(echo "$compare_json" | jq -c '.commits[] | {id: .id, message: .message}')
next_tag="v$major.$minor.$new_patch"
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