ci: update build workflow [skip ci]

This commit is contained in:
2026-09-02 14:14:02 +08:00
parent fc9d53ed10
commit ca1e125896
+30 -15
View File
@@ -47,8 +47,9 @@ jobs:
# 通过 docker.sock 调宿主 dockerd,故 -v 路径按宿主解析;用 docker cp 进出源码/产物
# nuget 缓存挂宿主固定目录 /opt/docker/projman-dotnet-nuget,跨构建复用依赖
# 仓库 nuget.config 常指向域名(nexus.<域名>,本地走宿主 Caddy 反代);容器内改写为容器名 nexus:8081 直连
# 构建目标自动定位:根 sln > 根 csproj > Web SDK 项目 > OutputType=Exe > 第一个非 Test csproj
# (类库项目 publish 无入口产物,必须优先选可执行项目;.slnx 需 SDK 9+ 解析,不参与定位)
# 构建目标自愈式定位:候选排序(根 sln > 根 csproj > Web SDK > OutputType=Exe > 其余非 Test),
# 逐个 publish,产出 *.runtimeconfig.json(可执行项目唯一标志)者胜出——类库 publish 无 runtimeconfig,
# 自动跳过换下一个(grep csproj 特征不可靠,以实际产物为准);.slnx 需 SDK 9+ 解析,不参与定位
- name: 构建 publish 产物(dotnet sdk 容器,复用宿主 nuget 缓存)
run: |
set -e
@@ -58,27 +59,41 @@ jobs:
-v /opt/docker/projman-dotnet-nuget:/root/.nuget/packages \
-w /build mcr.microsoft.com/dotnet/sdk:5.0 sleep 3600
docker cp "${PWD}/." dotnetbuild:/build/
docker exec dotnetbuild sh -c "find /build -maxdepth 2 -name nuget.config -exec sed -i 's|//nexus.yunhaixingtong.com|//nexus:8081|g' {} +"
docker exec dotnetbuild sh -c "find /build -maxdepth 2 -iname nuget.config -exec sed -i 's|//nexus.yunhaixingtong.com|//nexus:8081|g' {} +"
docker exec dotnetbuild sh -c '
set -e
target=$(ls /build/*.sln 2>/dev/null | head -1)
if [ -z "$target" ]; then target=$(ls /build/*.csproj 2>/dev/null | head -1); fi
if [ -z "$target" ]; then
cands=$(find /build -mindepth 2 -name "*.csproj" -not -path "*/bin/*" -not -path "*/obj/*" | grep -vi test)
target=$(echo "$cands" | xargs grep -l "Microsoft.NET.Sdk.Web" 2>/dev/null | head -1)
if [ -z "$target" ]; then target=$(echo "$cands" | xargs grep -l "<OutputType>Exe" 2>/dev/null | head -1); fi
if [ -z "$target" ]; then target=$(echo "$cands" | head -1); fi
root_sln=$(ls /build/*.sln 2>/dev/null | head -1 || true)
root_csproj=$(ls /build/*.csproj 2>/dev/null | head -1 || true)
if [ -n "$root_sln" ] || [ -n "$root_csproj" ]; then
ordered="$root_sln $root_csproj"
else
cands=$(find /build -mindepth 2 -name "*.csproj" -not -path "*/bin/*" -not -path "*/obj/*" | grep -vi test || true)
web=$(echo "$cands" | xargs -r grep -l "Microsoft.NET.Sdk.Web" 2>/dev/null || true)
exe=$(echo "$cands" | xargs -r grep -l "<OutputType>Exe" 2>/dev/null || true)
ordered=$(printf "%s\n%s\n%s\n" "$web" "$exe" "$cands" | grep -v "^$" | awk "!seen[\$0]++")
fi
if [ -z "$target" ]; then echo "ERROR: 未找到 .sln/.csproj(多可执行项目请自备 Dockerfile)"; exit 1; fi
if [ -z "$ordered" ]; then echo "ERROR: 未找到 .sln/.csproj(请自备 Dockerfile)"; exit 1; fi
target=""
for t in $ordered; do
rm -rf /app
echo "try publish: $t"
if dotnet publish "$t" -c Release -o /app /p:UseAppHost=false 2>/tmp/pub.err; then
if ls /app/*.runtimeconfig.json >/dev/null 2>&1; then target="$t"; break; fi
echo " -> 非 executable(类库),换下一候选"
else
echo " -> publish 失败,换下一候选: $(tail -3 /tmp/pub.err)"
fi
done
if [ -z "$target" ]; then echo "ERROR: 所有候选均未产出可执行产物(多可执行项目请自备 Dockerfile)"; exit 1; fi
echo "publish target: $target"
dotnet publish "$target" -c Release -o /app /p:UseAppHost=false
'
mkdir -p "${PWD}/publish"
docker cp dotnetbuild:/app/. "${PWD}/publish/"
docker rm -f dotnetbuild
# 生成入口脚本:产物中 X.deps.json 对应可执行 X.dll(取第一个;多可执行项目需自备 Dockerfile)
entry=$(ls "${PWD}"/publish/*.deps.json | head -1)
dll=$(basename "${entry}" .deps.json)
# 入口脚本:runtimeconfig.json 是可执行项目唯一标志(X.runtimeconfig.json <-> X.dll),
# 类库 deps.json 无 runtimeconfig 兄弟文件,天然不会误选
entry=$(ls "${PWD}"/publish/*.runtimeconfig.json | head -1)
dll=$(basename "${entry}" .runtimeconfig.json)
echo "exec dotnet ${dll}.dll" > "${PWD}/publish/entrypoint.sh"
- name: 构建并推送镜像
run: |