ci: update build workflow [skip ci]

This commit is contained in:
2026-09-02 12:15:21 +08:00
parent c6d9619990
commit ffedc2acd6
+44 -6
View File
@@ -1,10 +1,10 @@
name: build
# .NET Core 应用 CI:clone 源码 -> docker build(dotnet restore/build 在 Dockerfile 内)-> push Nexus -> 通知 projman 建档
# .NET Core 应用 CI(对齐 springboot 模式:构建在独立 sdk 容器内,Dockerfile 只拷产物)
# 链路:clone 源码 -> dotnet sdk 容器 publish(入 infra-infra-net,容器名直连 nexus 拉 NuGet 包,
# 复用宿主 nuget 缓存卷)-> docker build -f docker/Dockerfile(COPY publish/ 产物,RUN 零网络)-> push Nexus -> 通知 projman 建档
# 触发:push 到 main / 手动 Run workflow(可勾选缓存重建)
# 注意:dotnet 构建发生在 docker build 内部,宿主卷挂不进去;其缓存 = docker 层缓存
# (Dockerfile 应按「COPY *.csproj -> dotnet restore -> COPY 其余」分层以命中层缓存)。
# cache_bust 勾选时清空 docker builder 层缓存,强制下次全量重建。
# cache_bust 勾选时清空宿主 nuget 缓存卷 + docker builder 层缓存,强制下次全量重建。
# 依赖仓库 Secrets:NEXUS_USER / NEXUS_PASSWORD(docker-hosted 推送)/ DEPLOY_TOKEN(webhook 鉴权)
# 依赖 docker/Dockerfile(由 projman 注入,开发者也可自备——已写则尊重不覆盖)
@@ -36,12 +36,50 @@ jobs:
rm -rf /tmp/src
COMMIT_MSG=$(git log -1 --pretty=%s | tr '\n' ' ' | sed 's/["\\]/ /g' | cut -c1-200)
echo "COMMIT_MSG=${COMMIT_MSG}" >> "$GITHUB_ENV"
# 可选:清空 docker 构建层缓存(仅手动勾选 cache_bust 时;.NET 构建缓存即 docker 层缓存)
# 可选:清空 nuget 缓存卷 + docker 构建层缓存(仅手动勾选 cache_bust 时)
- name: 可选清空构建缓存
if: ${{ inputs.cache_bust == 'true' }}
run: |
docker run --rm -v /opt/docker/projman-dotnet-nuget:/root/.nuget/packages alpine sh -c 'rm -rf /root/.nuget/packages/*'
docker builder prune -f
echo "docker build cache busted"
echo "nuget + docker build cache busted"
# dotnet 构建(对齐 maven 模式):起独立 sdk 容器(入 infra-infra-net 用容器名 nexus 直连拉 NuGet 包)
# 通过 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+ 解析,不参与定位)
- name: 构建 publish 产物(dotnet sdk 容器,复用宿主 nuget 缓存)
run: |
set -e
docker rm -f dotnetbuild 2>/dev/null || true
docker run -d --name dotnetbuild \
--network infra-infra-net \
-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 '
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
fi
if [ -z "$target" ]; then echo "ERROR: 未找到 .sln/.csproj(多可执行项目请自备 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)
echo "exec dotnet ${dll}.dll" > "${PWD}/publish/entrypoint.sh"
- name: 构建并推送镜像
run: |
set -e