feat: minimal workflow using host tools
Some checks failed
Build & Deploy / build (push) Failing after 0s

This commit is contained in:
joe
2026-07-19 02:19:22 +08:00
parent 354ab8aab8
commit fafa5b22a9

View File

@@ -1,71 +1,73 @@
# Gitea Actions workflow for Node/Vue/React frontend name: Build & Deploy
# Place at: <repo>/.gitea/workflows/build.yml
name: Build & Deploy Frontend
on: on:
push: push:
branches: [main] branches: [main]
pull_request:
branches: [main]
env: env:
REGISTRY: 127.0.0.1:5000 REGISTRY: 127.0.0.1:5000
IMAGE_NAME: web-frontend
jobs: jobs:
build: build:
runs-on: self-hosted runs-on: self-hosted
steps: steps:
- name: Checkout - name: Install build tools (apt)
uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: npm ci || npm install
- name: Build
run: npm run build
env:
# 注入构建时环境变量
VITE_API_URL: ${{ vars.API_URL }}
- name: Build & push Docker image
env:
REGISTRY: ${{ env.REGISTRY }}
run: | run: |
IMAGE_TAG="${{ env.REGISTRY }}/${IMAGE_NAME}:${GITHUB_SHA::8}" apt-get update
docker build \ apt-get install -y curl git nodejs npm docker.io 2>/dev/null || true
--build-arg VITE_API_URL="${{ vars.API_URL }}" \
-t "${IMAGE_TAG}" \
-t "${REGISTRY}/${IMAGE_NAME}:latest" \
-f Dockerfile .
docker push "${IMAGE_TAG}"
docker push "${REGISTRY}/${IMAGE_NAME}:latest"
echo "IMAGE_TAG=${IMAGE_TAG}" >> $GITHUB_OUTPUT
echo "DEPLOY_IMAGE=${REGISTRY}/${IMAGE_NAME}:${GITHUB_SHA::8}" >> $GITHUB_ENV
- name: Update ArgoCD app - name: Generate demo content + Dockerfile
run: | run: |
# 触发 ArgoCD 重新同步 cd /workspace
curl -sk -X POST \ [ -d demo-frontend ] || git clone http://joe:${GITEA_TOKEN}@192.168.1.212:3000/joe/demo-frontend.git
-H "Authorization: Bearer ${{ secrets.ARGOCD_TOKEN }}" \ cd demo-frontend
"https://argocd.k3s-runner.cicd.svc.cluster.local/api/v1/applications/web-frontend/restart" git pull
deploy: cat > package.json <<'EOF'
needs: build {
runs-on: self-hosted "name": "demo-frontend",
if: github.ref == 'refs/heads/main' "version": "1.0.0",
steps: "scripts": {"build": "mkdir -p dist && echo '<html><body style=\"font-family:sans-serif;padding:40px;text-align:center\"><h1>Demo Frontend</h1><p>Build: $BUILD_ID</p></body></html>' > dist/index.html"}
- name: Trigger ArgoCD sync }
EOF
cat > Dockerfile <<'EOF'
FROM nginx:alpine
COPY dist/ /usr/share/nginx/html/
EXPOSE 80
EOF
env: env:
IMAGE: ${{ needs.build.outputs.IMAGE_TAG }} GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
BUILD_ID: ${{ github.sha }}
- name: Build frontend
run: | run: |
# Patch ArgoCD Application 指向新镜像 cd /workspace/demo-frontend
kubectl -n argocd patch application web-frontend --type merge \ npm install --silent
-p "{\"spec\":{\"source\":{\"helm\":{\"parameters\":[{\"name\":\"image.tag\",\"value\":\"${GITHUB_SHA::8}\"}]}}}}" npm run build
- name: Docker build & push
run: |
cd /workspace/demo-frontend
SHORT=${GITHUB_SHA::8}
TAG="${REGISTRY}/demo-frontend:${SHORT}"
LATEST="${REGISTRY}/demo-frontend:latest"
docker build -t "${TAG}" -t "${LATEST}" .
docker push "${TAG}"
docker push "${LATEST}"
echo "✓ Pushed ${TAG}"
env:
GITHUB_SHA: ${{ github.sha }}
- name: Patch ArgoCD Application
run: |
SHORT=${GITHUB_SHA::8}
if kubectl -n argocd get application demo-frontend &>/dev/null; then
kubectl -n argocd patch application demo-frontend --type merge \
-p "{\"spec\":{\"source\":{\"helm\":{\"parameters\":[{\"name\":\"image.tag\",\"value\":\"${SHORT}\"}]}}}}"
echo "✓ ArgoCD app patched"
else
echo "⚠ demo-frontend Application not found in argocd namespace (create it manually)"
fi
env:
GITHUB_SHA: ${{ github.sha }}