Files
it_cost_dashboard/.gitea/workflows/cicd.yaml
T
Tom Chien (IQ-AS) b3c14b5c86
CI/CD (dev on branch, prod on tag) / build-and-deploy (push) Successful in 5m0s
ci: drop dynamic matrix (unsupported by act_runner), compute env config in bash
2026-11-17 15:05:28 +08:00

253 lines
10 KiB
YAML

name: CI/CD (dev on branch, prod on tag)
on:
push:
branches:
- dev # build & deploy for dev only
tags:
- "*" # build & deploy for prod only when a tag is pushed
jobs:
build-and-deploy:
# Gitea Actions: runners are selected by label only (no runner groups).
runs-on: ubuntu-latest
# Per-environment group: a dev push must never cancel an in-flight prod
# deploy. NOTE: act_runner does not support dynamic matrix (fromJSON of a
# job output expands every matrix.* to ""), so per-env config is computed
# in the "Resolve environment config" bash step below instead.
concurrency:
group: deploy-${{ github.ref_type == 'tag' && 'prod' || 'dev' }}
cancel-in-progress: true
env:
# Harbor on the Mac mini host. The runner pod mounts the Docker Desktop
# daemon socket, so from the daemon's point of view the registry is
# reachable as host.docker.internal:8080 (plain HTTP — the daemon must
# list it under "insecure-registries"). Images live in Harbor's
# "library" project.
REGISTRY: host.docker.internal:8080/library
DOCKER_BUILDKIT: "1"
COMPOSE_DOCKER_CLI_BUILD: "1"
steps:
- name: Checkout (with tags)
uses: actions/checkout@v4
with:
fetch-depth: 0 # IMPORTANT to access tags
# Replaces the former setup job + dynamic matrix (unsupported on
# act_runner). Everything derived from the ref type lands in GITHUB_ENV
# so later steps can use plain $VARS.
- name: Resolve environment config
shell: bash
run: |
if [[ "${{ github.ref_type }}" == "tag" ]]; then
DEPLOY_ENV=prod
RELEASE_TAG="${GITHUB_REF#refs/tags/}"
else
DEPLOY_ENV=dev
RELEASE_TAG=""
fi
{
echo "DEPLOY_ENV=${DEPLOY_ENV}"
echo "RELEASE_TAG=${RELEASE_TAG}"
echo "IMAGE_SERVER=${REGISTRY}/it-cost-dashboard-server-${DEPLOY_ENV}"
echo "IMAGE_DB=${REGISTRY}/it-cost-dashboard-db-${DEPLOY_ENV}"
echo "LATEST_TAG=latest-${DEPLOY_ENV}"
echo "RELEASE=it-cost-dashboard-${DEPLOY_ENV}"
echo "NAMESPACE=it-cost-dashboard-${DEPLOY_ENV}"
echo "CHART=./helm"
echo "VALUES_FILE=./helm/values-${DEPLOY_ENV}.yaml"
} >> "$GITHUB_ENV"
echo "Resolved environment: ${DEPLOY_ENV} (release tag: '${RELEASE_TAG}')"
- name: Verify tag is on prod branch (main)
if: github.ref_type == 'tag'
shell: bash
run: |
git fetch origin main --quiet
if git merge-base --is-ancestor "${{ github.sha }}" origin/main; then
echo "Tag commit is on main. Proceeding with prod deploy."
else
echo "::error::Tag ${GITHUB_REF#refs/tags/} points at a commit not on main. Refusing prod deploy."
exit 1
fi
# The runner's job image ships docker but not kubectl/helm, so install
# them at job start (static binaries, linux/amd64).
- name: Install kubectl & helm
run: |
if ! command -v kubectl >/dev/null 2>&1; then
KVER=$(curl -sfL https://dl.k8s.io/release/stable.txt)
curl -sfL -o /usr/local/bin/kubectl "https://dl.k8s.io/release/${KVER}/bin/linux/amd64/kubectl"
chmod +x /usr/local/bin/kubectl
fi
if ! command -v helm >/dev/null 2>&1; then
curl -sfL https://get.helm.sh/helm-v3.16.4-linux-amd64.tar.gz | tar xz -C /tmp
mv /tmp/linux-amd64/helm /usr/local/bin/helm
fi
kubectl version --client
helm version
- name: Set up kubeconfig
run: |
echo "KUBECONFIG=${RUNNER_TEMP}/kubeconfig" >> "$GITHUB_ENV"
echo "${{ secrets.KUBE_CONFIG_DATA }}" | base64 -d > "${RUNNER_TEMP}/kubeconfig"
chmod 600 "${RUNNER_TEMP}/kubeconfig"
- name: Login to Harbor
run: |
# REGISTRY is host/project but `docker login` takes only the host.
REGISTRY_HOST=$(echo "${REGISTRY}" | cut -d'/' -f1)
echo "Logging in to ${REGISTRY_HOST}"
echo "${{ secrets.HARBOR_PASSWORD }}" | docker login "${REGISTRY_HOST}" \
-u "${{ secrets.HARBOR_USERNAME }}" \
--password-stdin
- name: Pre-build diagnostics
run: |
echo "Ref: ${GITHUB_REF}"
echo "Environment: ${DEPLOY_ENV}"
echo "Release tag: ${RELEASE_TAG}"
echo "Server image: ${IMAGE_SERVER}"
echo "DB image: ${IMAGE_DB}"
docker version
helm version
kubectl version --client
- name: Build & Push server image
run: |
BUILD_TAG_ARGS=(
-t "${IMAGE_SERVER}:${LATEST_TAG}"
-t "${IMAGE_SERVER}:${{ github.sha }}"
)
if [[ -n "${RELEASE_TAG}" ]]; then
BUILD_TAG_ARGS+=( -t "${IMAGE_SERVER}:${RELEASE_TAG}" )
fi
# AUTH_SECRET is build-time only (Auth.js v5 initializes NextAuth() at
# module load during `next build`). The real secret comes from k8s.
docker build \
-f Dockerfile \
--build-arg AUTH_SECRET=build-placeholder-not-used-at-runtime \
"${BUILD_TAG_ARGS[@]}" \
.
docker push "${IMAGE_SERVER}:${LATEST_TAG}"
docker push "${IMAGE_SERVER}:${{ github.sha }}"
if [[ -n "${RELEASE_TAG}" ]]; then
docker push "${IMAGE_SERVER}:${RELEASE_TAG}"
fi
- name: Build & Push db image
run: |
BUILD_TAG_ARGS=(
-t "${IMAGE_DB}:${LATEST_TAG}"
-t "${IMAGE_DB}:${{ github.sha }}"
)
if [[ -n "${RELEASE_TAG}" ]]; then
BUILD_TAG_ARGS+=( -t "${IMAGE_DB}:${RELEASE_TAG}" )
fi
docker build \
-f Dockerfile.db \
"${BUILD_TAG_ARGS[@]}" \
.
docker push "${IMAGE_DB}:${LATEST_TAG}"
docker push "${IMAGE_DB}:${{ github.sha }}"
if [[ -n "${RELEASE_TAG}" ]]; then
docker push "${IMAGE_DB}:${RELEASE_TAG}"
fi
- name: Ensure namespace exists
run: |
# `helm --atomic` against a missing namespace fails outright.
kubectl create namespace "${NAMESPACE}" \
--dry-run=client -o yaml | kubectl apply -f -
# The kubelet pulls through the same Docker Desktop daemon that built the
# image, so the pull server must match the push host exactly
# (host.docker.internal:8080).
- name: Ensure Harbor pull secret exists
run: |
kubectl create secret docker-registry harbor-pull-secret \
--docker-server="$(echo "${REGISTRY}" | cut -d'/' -f1)" \
--docker-username="${{ secrets.HARBOR_USERNAME }}" \
--docker-password="${{ secrets.HARBOR_PASSWORD }}" \
--namespace "${NAMESPACE}" \
--dry-run=client -o yaml | kubectl apply -f -
# ---- DEV DEPLOY (branch dev only) ----
# Deployed by commit SHA, not latest-dev: an immutable tag sidesteps the
# moving-tag + IfNotPresent stale-cache trap and makes `kubectl describe
# pod` show exactly which commit is live.
- name: Deploy DEV with Helm
if: github.ref_type == 'branch' && github.ref == 'refs/heads/dev'
run: |
kubectl get nodes
kubectl get ns
helm upgrade --install "${RELEASE}" "${CHART}" \
-f "${VALUES_FILE}" \
--namespace "${NAMESPACE}" \
--set server.image.repository="${IMAGE_SERVER}" \
--set server.image.tag="${{ github.sha }}" \
--set db.image.repository="${IMAGE_DB}" \
--set db.image.tag="${{ github.sha }}" \
--set-string secrets.authSecret="${{ secrets.AUTH_SECRET_DEV }}" \
--set-string secrets.dbRootPassword="${{ secrets.DB_PASSWORD_DEV }}" \
--set-string secrets.localAdminPassword="${{ secrets.LOCAL_ADMIN_PASSWORD_DEV }}" \
--wait --timeout 10m --atomic --debug
# ---- PROD DEPLOY (tag push only) ----
- name: Deploy PROD with Helm
if: github.ref_type == 'tag'
run: |
kubectl get nodes
kubectl get ns
if [[ -z "${RELEASE_TAG}" ]]; then
echo "No tag detected; aborting prod deploy."
exit 1
fi
helm upgrade --install "${RELEASE}" "${CHART}" \
-f "${VALUES_FILE}" \
--namespace "${NAMESPACE}" \
--set server.image.repository="${IMAGE_SERVER}" \
--set server.image.tag="${RELEASE_TAG}" \
--set db.image.repository="${IMAGE_DB}" \
--set db.image.tag="${RELEASE_TAG}" \
--set-string secrets.authSecret="${{ secrets.AUTH_SECRET_PROD }}" \
--set-string secrets.dbRootPassword="${{ secrets.DB_PASSWORD_PROD }}" \
--set-string secrets.localAdminPassword="${{ secrets.LOCAL_ADMIN_PASSWORD_PROD }}" \
--wait --timeout 10m --atomic --debug
# Strictly redundant with immutable image tags: Helm already rolled the
# pods and --atomic already verified health. Retained as belt-and-braces.
# Scoped to component=server so it does not bounce MySQL every push.
- name: Rollout restart (server only)
if: success()
run: |
kubectl rollout restart deployment \
-l "app.kubernetes.io/instance=${RELEASE},app.kubernetes.io/component=server" \
-n "${NAMESPACE}"
- name: Post-deploy diagnostics (if failure)
if: failure()
run: |
echo "----- Diagnostics -----"
kubectl get deploy,sts,po,svc,ing -n "${NAMESPACE}" -o wide || true
echo "----- PVCs (a Pending PVC = missing/wrong StorageClass; invisible in pod logs) -----"
kubectl get pvc -n "${NAMESPACE}" -o wide || true
echo "----- Events -----"
kubectl get events -n "${NAMESPACE}" --sort-by=.lastTimestamp || true
for p in $(kubectl get po -n "${NAMESPACE}" -o name); do
echo "### $p"
kubectl describe "$p" -n "${NAMESPACE}" || true
kubectl logs "$p" -n "${NAMESPACE}" --all-containers --tail=200 || true
done