Refactor Kubernetes deployment process to use manifest files and environment variable substitution

This commit is contained in:
2025-10-01 14:45:51 +08:00
parent b7b024ca66
commit e64a396633
4 changed files with 74 additions and 61 deletions

View File

@@ -49,67 +49,17 @@ jobs:
run: |
export KUBECONFIG=kubeconfig
# Create deployment
cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
name: ${{ env.APP_NAME }}
namespace: ${{ env.NAMESPACE }}
labels:
app: ${{ env.APP_NAME }}
spec:
replicas: 2
selector:
matchLabels:
app: ${{ env.APP_NAME }}
template:
metadata:
labels:
app: ${{ env.APP_NAME }}
spec:
containers:
- name: ${{ env.APP_NAME }}
image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ inputs.image_tag }}
ports:
- containerPort: 5173
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"
---
apiVersion: v1
kind: Service
metadata:
name: ${{ env.APP_NAME }}-service
namespace: ${{ env.NAMESPACE }}
spec:
selector:
app: ${{ env.APP_NAME }}
ports:
- protocol: TCP
port: 80
targetPort: 5173
type: ClusterIP
---
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: ${{ env.APP_NAME }}-ingress
namespace: ${{ env.NAMESPACE }}
spec:
entryPoints:
- web
routes:
- match: Host(\`${{ env.HOST }}\`)
kind: Rule
services:
- name: ${{ env.APP_NAME }}-service
port: 80
EOF
# Replace variables in manifests and apply
for manifest in k8s/*.yaml; do
envsubst < "$manifest" | kubectl apply -f -
done
env:
APP_NAME: ${{ env.APP_NAME }}
NAMESPACE: ${{ env.NAMESPACE }}
REGISTRY: ${{ env.REGISTRY }}
IMAGE_NAME: ${{ env.IMAGE_NAME }}
IMAGE_TAG: ${{ inputs.image_tag }}
HOST: ${{ env.HOST }}
- name: Verify deployment
run: |