GwWuYou 2d4ed82782 chore(ci): 简化NuGet发布工作流并优化版本提取逻辑
- 移除打包后的调试步骤和冗余的包信息检查
- 使用标签名直接确定版本号,支持去除前导'v'或'V'
- 更新打包命令以使用提取的版本号
- 简化NuGet推送步骤中的包查找逻辑
- 在推送命令中增加详细输出选项以便调试
- 调整工作流名称更准确地反映其功能
2025-12-09 17:57:28 +08:00

89 lines
2.6 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

name: Publish to NuGet
# 触发条件:当有标签被推送到仓库时触发该工作流(例如 v1.0.0 或 1.0.0
on:
push:
tags:
- '*'
# 顶级权限:允许创建 Releasecontents: write和写 packages如果需要
permissions:
contents: write
packages: write
jobs:
build-and-publish:
runs-on: ubuntu-latest
# 允许此 job 请求短时 OIDC tokenNuGet/login 使用)
permissions:
id-token: write
contents: write
packages: write
steps:
- name: Checkout repository (at tag)
uses: actions/checkout@v4
with:
fetch-depth: 0
persist-credentials: true
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 9.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore -c Release
- name: Test
run: dotnet test --no-build -c Release --verbosity normal
- name: Determine tag version
id: tag_version
run: |
set -e
# GITHUB_REF example: refs/tags/v0.0.1 or refs/tags/0.0.1
echo "GITHUB_REF = ${GITHUB_REF}"
TAG=${GITHUB_REF#refs/tags/}
# remove leading 'v' or 'V' if present
VERSION=${TAG#v}
VERSION=${VERSION#V}
echo "tag='$TAG' -> version='$VERSION'"
echo "version=$VERSION" >> $GITHUB_OUTPUT
- name: Pack (use tag version)
run: |
set -e
echo "Packing with version=${{ steps.tag_version.outputs.version }}"
dotnet pack --no-build -c Release -o ./packages -p:PackageVersion=${{ steps.tag_version.outputs.version }}
- name: Show packages
run: ls -la ./packages || true
- name: NuGet login (OIDC → temp API key)
id: login
uses: NuGet/login@v1
with:
# 推荐把用户名放到仓库 Secret不是邮箱例如 ${{ secrets.NUGET_USER }}
# 也可以直接写用户名(不推荐),但通常使用 secret 更安全
user: ${{ secrets.NUGET_USER }}
- name: NuGet push (using short-lived API key)
run: |
set -e
PKG=$(find ./packages -name "*.nupkg" | head -n1)
if [ -z "$PKG" ]; then
echo "No package found"
exit 1
fi
echo "Pushing $PKG to nuget.org..."
dotnet nuget push "$PKG" \
--api-key "${{ steps.login.outputs.NUGET_API_KEY }}" \
--source https://api.nuget.org/v3/index.json \
--skip-duplicate --verbosity detailed