diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml index ad6a499..7140ab4 100644 --- a/.gitea/workflows/build.yml +++ b/.gitea/workflows/build.yml @@ -1,71 +1,73 @@ -# Gitea Actions workflow for Node/Vue/React frontend -# Place at: /.gitea/workflows/build.yml - -name: Build & Deploy Frontend +name: Build & Deploy on: push: branches: [main] - pull_request: - branches: [main] env: REGISTRY: 127.0.0.1:5000 - IMAGE_NAME: web-frontend jobs: build: runs-on: self-hosted steps: - - name: Checkout - 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 }} + - name: Install build tools (apt) run: | - IMAGE_TAG="${{ env.REGISTRY }}/${IMAGE_NAME}:${GITHUB_SHA::8}" - docker build \ - --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 + apt-get update + apt-get install -y curl git nodejs npm docker.io 2>/dev/null || true - - name: Update ArgoCD app + - name: Generate demo content + Dockerfile run: | - # 触发 ArgoCD 重新同步 - curl -sk -X POST \ - -H "Authorization: Bearer ${{ secrets.ARGOCD_TOKEN }}" \ - "https://argocd.k3s-runner.cicd.svc.cluster.local/api/v1/applications/web-frontend/restart" + cd /workspace + [ -d demo-frontend ] || git clone http://joe:${GITEA_TOKEN}@192.168.1.212:3000/joe/demo-frontend.git + cd demo-frontend + git pull - deploy: - needs: build - runs-on: self-hosted - if: github.ref == 'refs/heads/main' - steps: - - name: Trigger ArgoCD sync + cat > package.json <<'EOF' + { + "name": "demo-frontend", + "version": "1.0.0", + "scripts": {"build": "mkdir -p dist && echo '

Demo Frontend

Build: $BUILD_ID

' > dist/index.html"} + } + EOF + + cat > Dockerfile <<'EOF' + FROM nginx:alpine + COPY dist/ /usr/share/nginx/html/ + EXPOSE 80 + EOF env: - IMAGE: ${{ needs.build.outputs.IMAGE_TAG }} + GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} + BUILD_ID: ${{ github.sha }} + + - name: Build frontend run: | - # Patch ArgoCD Application 指向新镜像 - kubectl -n argocd patch application web-frontend --type merge \ - -p "{\"spec\":{\"source\":{\"helm\":{\"parameters\":[{\"name\":\"image.tag\",\"value\":\"${GITHUB_SHA::8}\"}]}}}}" \ No newline at end of file + cd /workspace/demo-frontend + npm install --silent + 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 }} \ No newline at end of file