- 删除了docker.yml中docker:dind服务相关配置 - 去除了DOCKER_HOST和DOCKER_TLS_VERIFY环境变量设置 - 简化了工作流以减少不必要的服务启动 - 保留了checkout步骤以保证代码获取正常运行
142 lines
4.3 KiB
YAML
142 lines
4.3 KiB
YAML
name: Build and Deploy
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- main
|
|
- master
|
|
pull_request:
|
|
branches:
|
|
- main
|
|
- master
|
|
|
|
env:
|
|
REGISTRY: gitea.shtao1.cn
|
|
IMAGE_NAME: ${{ gitea.repository }}
|
|
DOTNET_VERSION: '10.0'
|
|
|
|
jobs:
|
|
build:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- 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 Registry
|
|
uses: docker/login-action@v3
|
|
with:
|
|
registry: ${{ env.REGISTRY }}
|
|
username: ${{ gitea.actor }}
|
|
password: ${{ secrets.GITEA_TOKEN }}
|
|
|
|
- name: Extract metadata
|
|
id: meta
|
|
uses: docker/metadata-action@v5
|
|
with:
|
|
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
|
|
tags: |
|
|
type=ref,event=branch
|
|
type=sha,prefix=
|
|
type=raw,value=latest,enable={{is_default_branch}}
|
|
|
|
- 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
|