This commit is contained in:
@@ -0,0 +1,209 @@
|
||||
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=${REGISTRY}/portal-${DEPLOY_ENV}"
|
||||
echo "LATEST_TAG=latest-${DEPLOY_ENV}"
|
||||
echo "RELEASE=portal-${DEPLOY_ENV}"
|
||||
echo "NAMESPACE=portal-${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
|
||||
|
||||
# Fail fast on chart/template errors before spending time on a build.
|
||||
- name: Lint Helm chart
|
||||
run: |
|
||||
helm lint "${CHART}" -f "${VALUES_FILE}"
|
||||
|
||||
- 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 "Image: ${IMAGE}"
|
||||
docker version
|
||||
helm version
|
||||
kubectl version --client
|
||||
|
||||
- name: Build & Push image
|
||||
run: |
|
||||
BUILD_TAG_ARGS=(
|
||||
-t "${IMAGE}:${LATEST_TAG}"
|
||||
-t "${IMAGE}:${{ github.sha }}"
|
||||
)
|
||||
if [[ -n "${RELEASE_TAG}" ]]; then
|
||||
BUILD_TAG_ARGS+=( -t "${IMAGE}:${RELEASE_TAG}" )
|
||||
fi
|
||||
|
||||
docker build \
|
||||
-f Dockerfile \
|
||||
"${BUILD_TAG_ARGS[@]}" \
|
||||
.
|
||||
|
||||
docker push "${IMAGE}:${LATEST_TAG}"
|
||||
docker push "${IMAGE}:${{ github.sha }}"
|
||||
if [[ -n "${RELEASE_TAG}" ]]; then
|
||||
docker push "${IMAGE}:${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}" \
|
||||
--set server.image.tag="${{ github.sha }}" \
|
||||
--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}" \
|
||||
--set server.image.tag="${RELEASE_TAG}" \
|
||||
--wait --timeout 10m --atomic --debug
|
||||
|
||||
- name: Post-deploy diagnostics (if failure)
|
||||
if: failure()
|
||||
run: |
|
||||
echo "----- Diagnostics -----"
|
||||
kubectl get deploy,po,svc,ing -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
|
||||
Reference in New Issue
Block a user