app-starlock/tag_generator.sh

47 lines
1.7 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=""
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}"
echo "newest_tag:$newest_tag-second_newest_tag$second_newest_tag\n"
# 比较两个tag之间的commits
compare_json=$(curl -s --header "PRIVATE-TOKEN: $TOKEN" "$URL/projects/$PROJECT_ID/repository/compare?from=$newest_tag&to=master")
echo "compare_json:$compare_json\n"
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++))
patch=0
break
elif [[ "$commit_message" =~ ("fix:"*) ]]; then
((patch++))
elif [[ ! "$commit_message" =~ ("Merge"* | "Revert"*) ]]; then
((patch++))
fi
done < <(echo "$compare_json" | jq -c '.commits[] | {id: .id, message: .message}')
next_tag="v$major.$minor.$patch"
if [ "$next_tag" == "$newest_tag" ]; then
echo "no change from master,skip to generate tag"
exit 0
fi
fi
echo "New version: $next_tag"
git config user.name
git tag $next_tag
git push -u origin $next_tag