feat: 支持自动根据commitlog生成版本号以及realse changelog采集

This commit is contained in:
liuyanfeng 2024-10-14 22:51:58 +08:00
parent baeb2b5544
commit 3b414d3f66
3 changed files with 125 additions and 3 deletions

View File

@ -19,6 +19,12 @@ variables:
- if: $CI_COMMIT_BRANCH =~ /feat_[a-zA-Z]+/ - if: $CI_COMMIT_BRANCH =~ /feat_[a-zA-Z]+/
- if: $CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z]+\.[0-9]+)?$/ - if: $CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z]+\.[0-9]+)?$/
.tag_rule:
tags:
- macos
- flutter
rules:
- if: $CI_COMMIT_BRANCH == "master"
.print_env: .print_env:
stage: test stage: test
@ -51,6 +57,16 @@ test_lint_check:
- macos - macos
- flutter - flutter
git_tag:
stage: generate_tag
extends: .tag_rule
before_script:
- project_url=$(echo $CI_PROJECT_URL | sed 's/http:\/\///')
- echo "project_url:$project_url"
- git remote set-url origin http://gitlab-ci-token:${GITLAB_ACCESS_TOKEN}@$project_url.git
script:
- bash tag_generator.sh
build_android: build_android:
stage: package stage: package
extends: .setup_fastlane_android extends: .setup_fastlane_android
@ -83,6 +99,9 @@ create-release:
- if: $CI_COMMIT_TAG - if: $CI_COMMIT_TAG
before_script: before_script:
- echo "start create release" - echo "start create release"
- bash release_description_generator.sh
- export RELEASE_DESCRIPTION="$(cat changelog.md)"
- echo "${RELEASE_DESCRIPTION}"
script: script:
- export StarLock_VERSION=${CI_COMMIT_TAG#*-} - export StarLock_VERSION=${CI_COMMIT_TAG#*-}
- echo "Uploading StarLock-${StarLock_VERSION} packages to - echo "Uploading StarLock-${StarLock_VERSION} packages to
@ -94,14 +113,14 @@ create-release:
- 'curl -i --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file build/app/outputs/flutter-apk/starlock-sky-release-${CI_COMMIT_TAG}.apk - 'curl -i --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file build/app/outputs/flutter-apk/starlock-sky-release-${CI_COMMIT_TAG}.apk
"${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/StarLock/${StarLock_VERSION}/starlock-sky-release-${CI_COMMIT_TAG}.apk"' "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/StarLock/${StarLock_VERSION}/starlock-sky-release-${CI_COMMIT_TAG}.apk"'
- 'curl -i --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file build/app/outputs/flutter-apk/starlock-sky-release-${CI_COMMIT_TAG}.aab - 'curl -i --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file build/app/outputs/flutter-apk/starlock-sky-release-${CI_COMMIT_TAG}.aab
"${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/StarLock/${StarLock_VERSION}/starlock-sky-release-${CI_COMMIT_TAG}.aab"' "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/StarLock/${StarLock_VERSION}/starlock-sky-release-${CI_COMMIT_TAG}.aab"'
- 'curl -i --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file build/app/outputs/flutter-ipa/starlock-xhj-release-${CI_COMMIT_TAG}.ipa - 'curl -i --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file build/app/outputs/flutter-ipa/starlock-xhj-release-${CI_COMMIT_TAG}.ipa
"${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/StarLock/${StarLock_VERSION}/starlock-xhj-release-${CI_COMMIT_TAG}.ipa"' "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/StarLock/${StarLock_VERSION}/starlock-xhj-release-${CI_COMMIT_TAG}.ipa"'
- 'curl -i --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file build/app/outputs/flutter-ipa/starlock-sky-release-${CI_COMMIT_TAG}.ipa - 'curl -i --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file build/app/outputs/flutter-ipa/starlock-sky-release-${CI_COMMIT_TAG}.ipa
"${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/StarLock/${StarLock_VERSION}/starlock-sky-release-${CI_COMMIT_TAG}.ipa"' "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/StarLock/${StarLock_VERSION}/starlock-sky-release-${CI_COMMIT_TAG}.ipa"'
release: release:
name: '$CI_COMMIT_TAG' name: '$CI_COMMIT_TAG'
description: 'Created Release By GitLab CI/CD' description: '$(cat changelog.md)'
tag_name: '$CI_COMMIT_TAG' tag_name: '$CI_COMMIT_TAG'
ref: '$CI_COMMIT_TAG' ref: '$CI_COMMIT_TAG'
assets: assets:

View File

@ -0,0 +1,56 @@
#!/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

47
tag_generator.sh Executable file
View File

@ -0,0 +1,47 @@
#!/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