app-starlock/tag_generator.sh

68 lines
2.4 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')
# 只保留不带 _sky 后缀的 tag 作为最新 tag
plain_tags=$(echo "$tags" | grep -v '_sky$')
latest_tag=$(echo "$plain_tags" | head -n 1)
tags_length=$(echo "$tags_json" | jq -r 'length')
if [ "$tags_length" -lt 1 ]; then
next_tag="v1.0.0"
else
IFS='.' read -r major minor patch <<< "$latest_tag"
major="${major#v}"
compare_json=""
if [[ "$1" == "generate_tag" ]];then
echo "generate_tag:$latest_tag-to-master\n"
compare_json=$(curl -s --header "PRIVATE-TOKEN: $TOKEN" "$URL/projects/$PROJECT_ID/repository/compare?from=$latest_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
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
echo "New Tag:$latest_tag;New version: $next_tag;command: $1"
if [[ "$1" == "generate_tag" ]];then
if [ "$next_tag" == "$latest_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