chore(ci): 添加集中管理NuGet包版本并优化GitHub Actions工作流
Some checks failed
Build and Deploy / build (push) Successful in 36s
Build and Deploy / docker (push) Failing after 30s
Build and Deploy / deploy (push) Has been skipped

- 在Directory.Build.props和Directory.Packages.props中启用包版本集中管理
- 统一管理多个关键依赖项的版本号,包括Fengling服务发现、Microsoft和Serilog组件
- 重构YarpGateway.csproj从项目引用改为包引用以支持包版本集中管理
- 新增NuGet.Config文件配置企业私有源和官方源凭证
- 完善GitHub Actions工作流,添加.NET环境设置及依赖恢复
- 拆分构建、Docker构建推送及部署步骤,增加Kubernetes部署实现
- 支持push和pr触发,使用动态标签和metadata管理镜像版本
- 自动更新Kubernetes部署镜像标签并进行回滚状态检查与验证
This commit is contained in:
movingsam 2026-02-27 21:11:13 +08:00
parent b9aea78495
commit b2478fb25b
5 changed files with 152 additions and 20 deletions

View File

@ -1,29 +1,95 @@
name: Build and Push Docker
name: Build and Deploy
on:
push:
branches: [main]
tags:
- "v*"
branches:
- main
- master
pull_request:
branches:
- main
- master
env:
REGISTRY: gitea.shtao1.cn
IMAGE_NAME: fengling/fengling-gateway
IMAGE_NAME: ${{ gitea.repository }}
DOTNET_VERSION: '10.0'
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Checkout
uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
- name: Configure NuGet
run: |
cat > NuGet.Config << 'NUGET'
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="gitea" value="https://gitea.shtao1.cn/api/packages/fengling/nuget/index.json" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
<packageSourceCredentials>
<gitea>
<add key="Username" value="${{ secrets.NUGET_USERNAME }}" />
<add key="ClearTextPassword" value="${{ secrets.NUGET_TOKEN }}" />
</gitea>
</packageSourceCredentials>
</configuration>
NUGET
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Publish
run: dotnet publish --configuration Release --no-build --output ./publish
docker:
needs: build
runs-on: ubuntu-latest
if: gitea.event_name == 'push'
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Configure NuGet for Docker
run: |
cat > NuGet.Config << 'NUGET'
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="gitea" value="https://gitea.shtao1.cn/api/packages/fengling/nuget/index.json" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
<packageSourceCredentials>
<gitea>
<add key="Username" value="${{ secrets.NUGET_USERNAME }}" />
<add key="ClearTextPassword" value="${{ secrets.NUGET_TOKEN }}" />
</gitea>
</packageSourceCredentials>
</configuration>
NUGET
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Gitea
- name: Login to Gitea Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: fengling
username: ${{ gitea.actor }}
password: ${{ secrets.GITEATOKEN }}
- name: Extract metadata
@ -33,13 +99,43 @@ jobs:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=sha
type=sha,prefix=
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
deploy:
needs: docker
runs-on: ubuntu-latest
if: gitea.event_name == 'push' && (gitea.ref == 'refs/heads/main' || gitea.ref == 'refs/heads/master')
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up kubectl
uses: azure/setup-kubectl@v3
- name: Configure kubectl
run: |
mkdir -p ~/.kube
echo "${{ secrets.KUBE_CONFIG }}" | base64 -d > ~/.kube/config
chmod 600 ~/.kube/config
- name: Deploy to Kubernetes
run: |
TAG=$(echo ${{ gitea.sha }} | cut -c1-7)
sed -i "s|image:.*fengling-gateway.*|image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${TAG}|g" k8s/deployment.yaml
kubectl apply -f k8s/deployment.yaml -n fengling
kubectl apply -f k8s/service.yaml -n fengling
kubectl rollout status deployment/fengling-gateway -n fengling --timeout=300s
- name: Verify deployment
run: |
kubectl get pods -n fengling -l app=fengling-gateway
kubectl get services -n fengling fengling-gateway

View File

@ -1,5 +1,6 @@
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

27
Directory.Packages.props Normal file
View File

@ -0,0 +1,27 @@
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<!-- Fengling ServiceDiscovery Packages (from Gitea) -->
<PackageVersion Include="Fengling.ServiceDiscovery.Core" Version="1.0.0" />
<PackageVersion Include="Fengling.ServiceDiscovery.Kubernetes" Version="1.0.0" />
<PackageVersion Include="Fengling.ServiceDiscovery.Static" Version="1.0.0" />
<!-- Microsoft Packages -->
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.2" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.3" />
<!-- Database -->
<PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.0" />
<!-- Serilog -->
<PackageVersion Include="Serilog.AspNetCore" Version="9.0.0" />
<PackageVersion Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageVersion Include="Serilog.Sinks.File" Version="7.0.0" />
<!-- Others -->
<PackageVersion Include="StackExchange.Redis" Version="2.8.31" />
<PackageVersion Include="Yarp.ReverseProxy" Version="2.3.0" />
</ItemGroup>
</Project>

8
NuGet.Config Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="gitea" value="https://gitea.shtao1.cn/api/packages/fengling/nuget/index.json" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>

View File

@ -22,10 +22,10 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Fengling.ServiceDiscovery\Fengling.ServiceDiscovery.Core\Fengling.ServiceDiscovery.Core.csproj" />
<ProjectReference Include="..\Fengling.ServiceDiscovery\Fengling.ServiceDiscovery.Kubernetes\Fengling.ServiceDiscovery.Kubernetes.csproj" />
<ProjectReference Include="..\Fengling.ServiceDiscovery\Fengling.ServiceDiscovery.Static\Fengling.ServiceDiscovery.Static.csproj" />
</ItemGroup>
<PackageReference Include="Fengling.ServiceDiscovery.Core" />
<PackageReference Include="Fengling.ServiceDiscovery.Kubernetes" />
<PackageReference Include="Fengling.ServiceDiscovery.Static" />
</ItemGroup>
<ItemGroup>
<Content Include="..\..\.dockerignore">