92 lines
3.0 KiB
YAML
92 lines
3.0 KiB
YAML
name: Build & Deploy
|
|
|
|
on:
|
|
push:
|
|
branches: [main]
|
|
|
|
env:
|
|
REGISTRY: 192.168.1.212:1180/library
|
|
|
|
jobs:
|
|
build:
|
|
runs-on: self-hosted
|
|
steps:
|
|
- name: Install build tools
|
|
run: |
|
|
apk add --no-cache curl git nodejs npm docker-cli 2>/dev/null || true
|
|
|
|
- name: Generate demo content + Dockerfile
|
|
run: |
|
|
WORK=/root/.cache/demo-frontend
|
|
mkdir -p $WORK
|
|
cd $WORK
|
|
[ -d .git ] || git clone http://joe:${GITEA_TOKEN}@192.168.1.212:3000/joe/demo-frontend.git .
|
|
git pull
|
|
|
|
SHORT_SHA=${GITHUB_SHA::8}
|
|
cat > package.json <<EOF
|
|
{
|
|
"name": "demo-frontend",
|
|
"version": "1.0.0",
|
|
"scripts": {"build": "mkdir -p dist && echo '<html><body style=\"font-family:sans-serif;padding:40px;text-align:center\"><h1>Demo Frontend</h1><p>Build: ${SHORT_SHA}</p><p>Commit: ${GITHUB_SHA}</p><p>Time: $(date)</p></body></html>' > dist/index.html && echo 'OK' > dist/healthz"}
|
|
}
|
|
EOF
|
|
|
|
cat > Dockerfile <<EOF
|
|
FROM nginx:alpine
|
|
COPY dist/ /usr/share/nginx/html/
|
|
EXPOSE 80
|
|
HEALTHCHECK --interval=30s --timeout=3s CMD wget -q -O- http://localhost/healthz || exit 1
|
|
EOF
|
|
env:
|
|
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
|
GITHUB_SHA: ${{ github.sha }}
|
|
|
|
- name: Build frontend
|
|
run: |
|
|
cd /root/.cache/demo-frontend
|
|
npm install --silent
|
|
npm run build
|
|
env:
|
|
GITHUB_SHA: ${{ github.sha }}
|
|
|
|
- name: Login to Harbor
|
|
run: docker login 192.168.1.212:1180 -u admin -p "$HARBOR_PASSWORD"
|
|
env:
|
|
HARBOR_PASSWORD: ${{ secrets.HARBOR_PASSWORD }}
|
|
|
|
- name: Docker build & push
|
|
run: |
|
|
cd /root/.cache/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 }}
|
|
REGISTRY: ${{ env.REGISTRY }}
|
|
|
|
- name: Install jq
|
|
run: apk add --no-cache jq 2>/dev/null || true
|
|
|
|
- name: Patch ArgoCD
|
|
run: |
|
|
SHORT=${GITHUB_SHA::8}
|
|
for i in {1..10}; do
|
|
if kubectl -n cicd get application demo-frontend &>/dev/null; then
|
|
break
|
|
fi
|
|
echo "Waiting for ArgoCD application..."
|
|
sleep 3
|
|
done
|
|
PATCH=$(jq -n --arg tag "$SHORT" --arg reg "$REGISTRY" \
|
|
'{"spec":{"source":{"helm":{"parameters":[{"name":"image.registry","value":$reg},{"name":"image.tag","value":$tag}]}}}}')
|
|
kubectl -n cicd patch application demo-frontend --type=merge -p "$PATCH"
|
|
echo "✓ ArgoCD updated with tag ${SHORT}"
|
|
env:
|
|
GITHUB_SHA: ${{ github.sha }}
|
|
REGISTRY: ${{ env.REGISTRY }}
|