#!/bin/bash # 读取环境变量 URL=$CI_API_V4_URL TOKEN=$GITLAB_ACCESS_TOKEN PROJECT_ID=$CI_PROJECT_ID 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 2 ]; then newest_tag="master" second_newest_tag=$(echo "$tags" | head -n 1) else newest_tag=$(echo "$tags" | head -n 1) second_newest_tag=$(echo "$tags" | head -n 2 | tail -n 1) fi 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=$second_newest_tag&to=$newest_tag") #echo "compare_json:$compare_json\n" feat_log="" fix_log="" other_log="" 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') if [[ "$commit_message" =~ ("feat:"*) ]]; then feat_log+="* ${commit_message#feat:}\n" elif [[ "$commit_message" =~ ("fix:"*) ]]; then fix_log+="* ${commit_message#fix:}\n" elif [[ ! "$commit_message" =~ ("Merge"* | "Revert"*) ]]; then other_log+="* ${commit_message#("Merge" | "Revert")}\n" fi done < <(echo "$compare_json" | jq -c '.commits[] | {id: .id, message: .message}') echo -e "feat_log:$feat_log,fix_log:$fix_log,other_log:$other_log" # 如果没有feat或fix的commits,则设置默认日志 if [[ -z "$feat_log" ]]; then feat_log="- No 'feat' commits since last tagged commit." fi if [[ -z "$fix_log" ]]; then fix_log="- No 'fix' commits since last tagged commit." fi # 生成changelog文本 changelog="# Features\n$feat_log\n# Fixes\n$fix_log\n# Others\n$other_log" # 打印结果(或可以重定向到文件等) echo "$changelog" # (可选)将结果保存到文件中 echo -e "$changelog" > changelog.md