Compare commits
9 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0754ecc257 | |||
| 1f105ee069 | |||
| 2cc5b7d903 | |||
| d46e36e916 | |||
| 585ca1cedd | |||
| 97f72a36d8 | |||
| 5bd748872b | |||
| 060aaa9e20 | |||
| bbb4cb9fb1 |
5 changed files with 329 additions and 6 deletions
238
scripts/release.sh
Executable file
238
scripts/release.sh
Executable file
|
|
@ -0,0 +1,238 @@
|
|||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
|
||||
ROOT_DIR=$(cd -- "$SCRIPT_DIR/.." && pwd)
|
||||
cd "$ROOT_DIR"
|
||||
|
||||
PARENT_POM="simple-starter-dependencies/pom.xml"
|
||||
BOM_POM="simple-starter-bom/pom.xml"
|
||||
REPO_ID="xcynet-simple-starter"
|
||||
REPO_URL="https://packages.aliyun.com/6916925f9add35de197b8556/maven/repo-mtndz"
|
||||
|
||||
MODE=""
|
||||
VERSION_ARG=""
|
||||
YES=0
|
||||
DRY_RUN=0
|
||||
SKIP_TESTS=0
|
||||
PUSH_TAG=1
|
||||
|
||||
usage() {
|
||||
cat <<EOF
|
||||
用法: scripts/release.sh <snapshot|release> [version] [选项]
|
||||
|
||||
模式:
|
||||
snapshot [x.y.z] 部署快照到云效仓库(可重复覆盖)。
|
||||
不传 version 时使用 POM 中的 revision 加 -SNAPSHOT。
|
||||
release <x.y.z> 部署正式版到云效仓库,并创建/推送 git tag。
|
||||
必须显式传入版本号,防止误发。
|
||||
|
||||
选项:
|
||||
-y, --yes 跳过交互确认(CI 环境使用)
|
||||
-n, --dry-run 只打印将执行的命令,不真正执行
|
||||
--skip-tests 跳过测试(snapshot 默认跳过;release 默认执行,请谨慎)
|
||||
--no-push-tag release 模式只创建本地 tag,不推送到远端
|
||||
-h, --help 显示本帮助
|
||||
|
||||
环境变量:
|
||||
MAVEN_SETTINGS 指定 settings.xml,默认 ~/.m2/settings.xml
|
||||
ALLOW_RELEASE_BRANCH=1 允许在非 main/master 分支发布正式版
|
||||
|
||||
发布仓库:
|
||||
id : ${REPO_ID}
|
||||
url : ${REPO_URL}
|
||||
|
||||
流程说明:
|
||||
1. 校验 POM 版本一致性、distributionManagement 与 settings 中的仓库配置、工作区干净。
|
||||
2. 执行 spotless:apply;若产生源码改动会中止,请先提交格式化结果。
|
||||
3. 使用 -Drevision=<version> -Dmaven.resolver.transport=wagon 部署(不激活 release profile)。
|
||||
4. release 模式部署成功后创建 annotated tag 并推送。
|
||||
|
||||
示例:
|
||||
scripts/release.sh snapshot
|
||||
scripts/release.sh snapshot 2.7.0
|
||||
scripts/release.sh release 2.7.0
|
||||
scripts/release.sh release 2.7.0 --dry-run
|
||||
|
||||
发布到 Maven Central 不在本脚本范围内,见 docs/publish-to-maven-guide.md。
|
||||
EOF
|
||||
}
|
||||
|
||||
die() {
|
||||
printf 'ERROR: %s\n' "$1" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
log() {
|
||||
printf '\n==> %s\n' "$1"
|
||||
}
|
||||
|
||||
run() {
|
||||
printf '+ %s\n' "$*"
|
||||
if [[ $DRY_RUN -eq 0 ]]; then
|
||||
"$@"
|
||||
fi
|
||||
}
|
||||
|
||||
while (( $# )); do
|
||||
case "$1" in
|
||||
snapshot|release)
|
||||
MODE="$1"
|
||||
shift
|
||||
;;
|
||||
-y|--yes)
|
||||
YES=1
|
||||
shift
|
||||
;;
|
||||
-n|--dry-run)
|
||||
DRY_RUN=1
|
||||
shift
|
||||
;;
|
||||
--skip-tests)
|
||||
SKIP_TESTS=1
|
||||
shift
|
||||
;;
|
||||
--no-push-tag)
|
||||
PUSH_TAG=0
|
||||
shift
|
||||
;;
|
||||
-h|--help)
|
||||
usage
|
||||
exit 0
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
-*)
|
||||
die "未知参数: $1"
|
||||
;;
|
||||
*)
|
||||
if [[ -z "$VERSION_ARG" ]]; then
|
||||
VERSION_ARG="$1"
|
||||
shift
|
||||
else
|
||||
die "多余参数: $1"
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
[[ -n "$MODE" ]] || { usage; exit 1; }
|
||||
[[ -f "$PARENT_POM" && -f "$BOM_POM" ]] || die "请在仓库根目录执行本脚本"
|
||||
|
||||
command -v mvn >/dev/null 2>&1 || die "未找到 mvn 命令"
|
||||
command -v git >/dev/null 2>&1 || die "未找到 git 命令"
|
||||
|
||||
read_revision() {
|
||||
sed -n 's/.*<revision>\([^<]*\)<\/revision>.*/\1/p' "$1" | head -n 1
|
||||
}
|
||||
|
||||
CURRENT_VERSION=$(read_revision "$PARENT_POM")
|
||||
[[ -n "$CURRENT_VERSION" ]] || die "无法从 $PARENT_POM 读取 <revision>"
|
||||
|
||||
BOM_VERSION=$(read_revision "$BOM_POM")
|
||||
[[ "$BOM_VERSION" == "$CURRENT_VERSION" ]] || die \
|
||||
"版本不一致:${PARENT_POM}=${CURRENT_VERSION},${BOM_POM}=${BOM_VERSION},请先同步"
|
||||
|
||||
if [[ "$MODE" == "snapshot" ]]; then
|
||||
if [[ -n "$VERSION_ARG" ]]; then
|
||||
VERSION="$VERSION_ARG"
|
||||
[[ "$VERSION" == *-SNAPSHOT ]] || VERSION="${VERSION}-SNAPSHOT"
|
||||
else
|
||||
VERSION="${CURRENT_VERSION}-SNAPSHOT"
|
||||
fi
|
||||
[[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+-SNAPSHOT$ ]] || \
|
||||
die "快照版本号格式应为 x.y.z-SNAPSHOT,当前为: $VERSION"
|
||||
else
|
||||
VERSION="$VERSION_ARG"
|
||||
[[ -n "$VERSION" ]] || die "release 模式必须显式指定版本,当前 POM revision 为 $CURRENT_VERSION"
|
||||
[[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || \
|
||||
die "正式版本号格式应为 x.y.z,当前为: $VERSION"
|
||||
fi
|
||||
|
||||
read_distribution_repo_id() {
|
||||
sed -n '/<distributionManagement>/,/<\/distributionManagement>/s/.*<id>\([^<]*\)<\/id>.*/\1/p' "$1" | head -n 1
|
||||
}
|
||||
POM_REPO_ID=$(read_distribution_repo_id "$PARENT_POM")
|
||||
[[ "$POM_REPO_ID" == "$REPO_ID" ]] || \
|
||||
die "$PARENT_POM 的 distributionManagement 仓库 id 为 ${POM_REPO_ID:-<空>},预期 $REPO_ID"
|
||||
BOM_REPO_ID=$(read_distribution_repo_id "$BOM_POM")
|
||||
[[ "$BOM_REPO_ID" == "$REPO_ID" ]] || \
|
||||
die "$BOM_POM 的 distributionManagement 仓库 id 为 ${BOM_REPO_ID:-<空>},预期 $REPO_ID"
|
||||
|
||||
SETTINGS_FILE="${MAVEN_SETTINGS:-$HOME/.m2/settings.xml}"
|
||||
[[ -f "$SETTINGS_FILE" ]] || die "未找到 Maven settings: $SETTINGS_FILE"
|
||||
grep -q "<id>${REPO_ID}</id>" "$SETTINGS_FILE" || \
|
||||
die "$SETTINGS_FILE 中缺少 <id>${REPO_ID}</id> 的 server 配置"
|
||||
|
||||
if grep -q 'altReleaseDeploymentRepository\|altSnapshotDeploymentRepository' "$SETTINGS_FILE"; then
|
||||
printf 'WARN: %s 中存在 altReleaseDeploymentRepository/altSnapshotDeploymentRepository,可能覆盖 POM 中的发布仓库。\n' "$SETTINGS_FILE"
|
||||
fi
|
||||
|
||||
if [[ -n "$(git status --porcelain)" ]]; then
|
||||
die "工作区不干净,请先提交或 stash 改动:$(git status --short)"
|
||||
fi
|
||||
|
||||
if [[ "$MODE" == "release" ]]; then
|
||||
BRANCH=$(git symbolic-ref --short HEAD 2>/dev/null || true)
|
||||
if [[ "$BRANCH" != "main" && "$BRANCH" != "master" && -z "${ALLOW_RELEASE_BRANCH:-}" ]]; then
|
||||
die "正式版要求在 main/master 分支发布(当前: ${BRANCH:-detached HEAD});如确需其他分支,请设置 ALLOW_RELEASE_BRANCH=1"
|
||||
fi
|
||||
|
||||
if git rev-parse -q --verify "refs/tags/${VERSION}" >/dev/null; then
|
||||
die "本地 tag $VERSION 已存在"
|
||||
fi
|
||||
REMOTE_TAG=$(git ls-remote --tags origin "refs/tags/${VERSION}" 2>/dev/null || true)
|
||||
[[ -z "$REMOTE_TAG" ]] || die "远端 tag $VERSION 已存在"
|
||||
fi
|
||||
|
||||
log "统一代码格式(spotless)"
|
||||
# 走 compile 生命周期:spotless 绑定在 compile 阶段,只在声明了它的模块执行。
|
||||
# 直接调用插件 goal 会对整个 reactor 的每个模块执行,而没有 parent 的 simple-starter-bom
|
||||
# 并未声明该插件,会报 "Spotless plugin absent from the project"。
|
||||
run mvn -B -ntp -q -DskipTests compile
|
||||
if [[ $DRY_RUN -eq 0 && -n "$(git status --porcelain)" ]]; then
|
||||
die "spotless 产生了源码改动,请提交后再发布:$(git status --short)"
|
||||
fi
|
||||
|
||||
if [[ "$MODE" == "release" && $YES -eq 0 && $DRY_RUN -eq 0 ]]; then
|
||||
SKIP_NOTE=""
|
||||
if [[ $SKIP_TESTS -eq 1 ]]; then
|
||||
SKIP_NOTE="(将跳过测试)"
|
||||
fi
|
||||
printf '即将发布正式版 %s 到云效仓库 %s(%s)%s,并创建 tag。确认? [y/N] ' \
|
||||
"$VERSION" "$REPO_ID" "$REPO_URL" "$SKIP_NOTE"
|
||||
read -r REPLY
|
||||
[[ "$REPLY" =~ ^[Yy]$ ]] || die "已取消"
|
||||
fi
|
||||
|
||||
MVN_ARGS=(
|
||||
-B
|
||||
-ntp
|
||||
"-Drevision=${VERSION}"
|
||||
-Dmaven.resolver.transport=wagon
|
||||
-Dspotless.apply.skip=true
|
||||
)
|
||||
if [[ -n "${MAVEN_SETTINGS:-}" ]]; then
|
||||
MVN_ARGS+=(-s "$MAVEN_SETTINGS")
|
||||
fi
|
||||
if [[ "$MODE" == "snapshot" || $SKIP_TESTS -eq 1 ]]; then
|
||||
MVN_ARGS+=(-DskipTests)
|
||||
fi
|
||||
|
||||
log "部署 $VERSION 到云效仓库 ${REPO_ID}(${REPO_URL})"
|
||||
run mvn "${MVN_ARGS[@]}" clean deploy
|
||||
|
||||
if [[ "$MODE" == "release" ]]; then
|
||||
log "创建 tag $VERSION"
|
||||
run git tag -a "$VERSION" -m "Release $VERSION"
|
||||
if [[ $PUSH_TAG -eq 1 ]]; then
|
||||
log "推送 tag $VERSION"
|
||||
run git push origin "$VERSION"
|
||||
else
|
||||
printf '跳过推送 tag(--no-push-tag)\n'
|
||||
fi
|
||||
fi
|
||||
|
||||
log "完成:$MODE $VERSION"
|
||||
|
|
@ -47,12 +47,12 @@
|
|||
仓库为 Hybrid 模式,Release 与 Snapshot 共用同一地址。 -->
|
||||
<distributionManagement>
|
||||
<repository>
|
||||
<id>repo-mtndz</id>
|
||||
<id>xcynet-simple-starter</id>
|
||||
<name>Alyun Devops Maven Repository</name>
|
||||
<url>https://packages.aliyun.com/6916925f9add35de197b8556/maven/repo-mtndz</url>
|
||||
</repository>
|
||||
<snapshotRepository>
|
||||
<id>repo-mtndz</id>
|
||||
<id>xcynet-simple-starter</id>
|
||||
<name>Alyun Devops Maven Repository</name>
|
||||
<url>https://packages.aliyun.com/6916925f9add35de197b8556/maven/repo-mtndz</url>
|
||||
</snapshotRepository>
|
||||
|
|
@ -62,7 +62,7 @@
|
|||
<!-- 本 POM 无 parent,版本号只能在此单独维护;
|
||||
发布时统一用 -Drevision=x.y.z 覆盖(用户属性优先级高于此处),
|
||||
若手工改版本,必须同步修改 simple-starter-dependencies/pom.xml 中的 revision -->
|
||||
<revision>2.6.4</revision>
|
||||
<revision>2.7.1</revision>
|
||||
<!-- 必须与 simple-starter-dependencies 的 spring-boot.version 保持一致 -->
|
||||
<spring-boot.version>4.1.2</spring-boot.version>
|
||||
</properties>
|
||||
|
|
|
|||
|
|
@ -50,12 +50,12 @@
|
|||
该 id 取自云效自动生成的 settings.xml,即本仓库的仓库 ID。 -->
|
||||
<distributionManagement>
|
||||
<repository>
|
||||
<id>repo-mtndz</id>
|
||||
<id>xcynet-simple-starter</id>
|
||||
<name>Alyun Devops Maven Repository</name>
|
||||
<url>https://packages.aliyun.com/6916925f9add35de197b8556/maven/repo-mtndz</url>
|
||||
</repository>
|
||||
<snapshotRepository>
|
||||
<id>repo-mtndz</id>
|
||||
<id>xcynet-simple-starter</id>
|
||||
<name>Alyun Devops Maven Repository</name>
|
||||
<url>https://packages.aliyun.com/6916925f9add35de197b8556/maven/repo-mtndz</url>
|
||||
</snapshotRepository>
|
||||
|
|
@ -63,7 +63,7 @@
|
|||
|
||||
<properties>
|
||||
<!-- Project Version -->
|
||||
<revision>2.6.4</revision>
|
||||
<revision>2.7.1</revision>
|
||||
<!-- Core Framework Versions -->
|
||||
<spring-boot.version>4.1.2</spring-boot.version>
|
||||
<spring-cloud.version>2025.1.4</spring-cloud.version>
|
||||
|
|
|
|||
|
|
@ -16,10 +16,15 @@
|
|||
|
||||
package reliable.simple.starter.trace.autoconfigure;
|
||||
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
import ch.qos.logback.classic.turbo.TurboFilter;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import reliable.simple.starter.core.util.GeneralPropertySourceFactory;
|
||||
import reliable.simple.starter.trace.filter.EnsureTraceMdcTurboFilter;
|
||||
|
||||
/**
|
||||
* 链路追踪自动配置基类(基于 Micrometer Tracing + OpenTelemetry)
|
||||
|
|
@ -37,4 +42,20 @@ import reliable.simple.starter.core.util.GeneralPropertySourceFactory;
|
|||
@PropertySource(value = "classpath:default-trace.yml", factory = GeneralPropertySourceFactory.class)
|
||||
public class TraceAutoConfiguration {
|
||||
|
||||
/**
|
||||
* 启动后挂上,让后台线程的日志也写出 mdc.traceId。
|
||||
*/
|
||||
@Bean
|
||||
public EnsureTraceMdcTurboFilter ensureTraceMdcTurboFilter() {
|
||||
LoggerContext context = (LoggerContext)LoggerFactory.getILoggerFactory();
|
||||
for (TurboFilter existing : context.getTurboFilterList()) {
|
||||
if (existing instanceof EnsureTraceMdcTurboFilter filter) {
|
||||
return filter;
|
||||
}
|
||||
}
|
||||
EnsureTraceMdcTurboFilter filter = new EnsureTraceMdcTurboFilter();
|
||||
filter.start();
|
||||
context.addTurboFilter(filter);
|
||||
return filter;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
* Copyright (c) 2022-present Xcynet Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package reliable.simple.starter.trace.filter;
|
||||
|
||||
import ch.qos.logback.classic.Level;
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.turbo.TurboFilter;
|
||||
import ch.qos.logback.core.spi.FilterReply;
|
||||
import io.opentelemetry.api.trace.Span;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import org.slf4j.MDC;
|
||||
import org.slf4j.Marker;
|
||||
import reliable.simple.starter.trace.constant.TraceMdcKeys;
|
||||
|
||||
/**
|
||||
* 每条日志都带 mdc.traceId。请求线程沿用入口过滤器写入的值,其余线程补一个并留在该线程上。
|
||||
*
|
||||
* @author Copyright with xcynet
|
||||
*/
|
||||
public class EnsureTraceMdcTurboFilter extends TurboFilter {
|
||||
|
||||
@Override
|
||||
public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) {
|
||||
String traceId = MDC.get(TraceMdcKeys.TRACE_ID);
|
||||
if (traceId != null && !traceId.isBlank()) {
|
||||
return FilterReply.NEUTRAL;
|
||||
}
|
||||
Span span = Span.current();
|
||||
if (span.getSpanContext().isValid()) {
|
||||
MDC.put(TraceMdcKeys.TRACE_ID, span.getSpanContext().getTraceId());
|
||||
if (MDC.get(TraceMdcKeys.SPAN_ID) == null) {
|
||||
MDC.put(TraceMdcKeys.SPAN_ID, span.getSpanContext().getSpanId());
|
||||
}
|
||||
return FilterReply.NEUTRAL;
|
||||
}
|
||||
MDC.put(TraceMdcKeys.TRACE_ID, randomTraceId());
|
||||
return FilterReply.NEUTRAL;
|
||||
}
|
||||
|
||||
private static String randomTraceId() {
|
||||
byte[] bytes = new byte[16];
|
||||
ThreadLocalRandom.current().nextBytes(bytes);
|
||||
StringBuilder builder = new StringBuilder(32);
|
||||
for (byte value : bytes) {
|
||||
builder.append(Character.forDigit((value >> 4) & 0xF, 16));
|
||||
builder.append(Character.forDigit(value & 0xF, 16));
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue