diff --git a/.gitea/workflows/cicd.yaml b/.gitea/workflows/cicd.yaml index d7efae0..76af792 100644 --- a/.gitea/workflows/cicd.yaml +++ b/.gitea/workflows/cicd.yaml @@ -8,32 +8,16 @@ on: - "*" # build & deploy for prod only when a tag is pushed jobs: - setup: + build-and-deploy: # Gitea Actions: runners are selected by label only (no runner groups). runs-on: ubuntu-latest - outputs: - matrix: ${{ steps.pick.outputs.matrix }} - steps: - - name: Pick matrix config from ref type - id: pick - shell: bash - run: | - if [[ "${{ github.ref_type }}" == "tag" ]]; then - echo 'matrix={"include":[{"env":"prod","image_name_server":"it-cost-dashboard-server-prod","image_name_db":"it-cost-dashboard-db-prod","latest_tag":"latest-prod","release":"it-cost-dashboard-prod","namespace":"it-cost-dashboard-prod","chart":"./helm","values_file":"./helm/values-prod.yaml"}]}' >> "$GITHUB_OUTPUT" - else - echo 'matrix={"include":[{"env":"dev","image_name_server":"it-cost-dashboard-server-dev","image_name_db":"it-cost-dashboard-db-dev","latest_tag":"latest-dev","release":"it-cost-dashboard-dev","namespace":"it-cost-dashboard-dev","chart":"./helm","values_file":"./helm/values-dev.yaml"}]}' >> "$GITHUB_OUTPUT" - fi - - build-and-deploy: - needs: setup - runs-on: ubuntu-latest - strategy: - matrix: ${{ fromJSON(needs.setup.outputs.matrix) }} # Per-environment group: a dev push must never cancel an in-flight prod - # deploy (the original used a hardcoded "prod" group for both refs). + # 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-${{ matrix.env }} + group: deploy-${{ github.ref_type == 'tag' && 'prod' || 'dev' }} cancel-in-progress: true env: @@ -52,8 +36,34 @@ jobs: 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) - id: guard if: github.ref_type == 'tag' shell: bash run: | @@ -96,47 +106,25 @@ jobs: -u "${{ secrets.HARBOR_USERNAME }}" \ --password-stdin - - name: Determine ref type and release tag - id: refinfo - shell: bash - run: | - echo "GITHUB_REF=${GITHUB_REF}" - echo "GITHUB_REF_TYPE=${GITHUB_REF_TYPE:-${{ github.ref_type }}}" - if [[ "${{ github.ref_type }}" == "tag" ]]; then - echo "ref_type=tag" >> $GITHUB_OUTPUT - echo "release_tag=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT - elif [[ "${{ github.ref_type }}" == "branch" ]]; then - echo "ref_type=branch" >> $GITHUB_OUTPUT - echo "release_tag=" >> $GITHUB_OUTPUT - else - echo "ref_type=unknown" >> $GITHUB_OUTPUT - echo "release_tag=" >> $GITHUB_OUTPUT - fi - - name: Pre-build diagnostics run: | echo "Ref: ${GITHUB_REF}" - echo "Ref type: ${{ steps.refinfo.outputs.ref_type }}" - echo "Release tag: ${{ steps.refinfo.outputs.release_tag }}" - echo "Environment: ${{ matrix.env }}" - echo "Server image: ${REGISTRY}/${{ matrix.image_name_server }}" - echo "DB image: ${REGISTRY}/${{ matrix.image_name_db }}" + 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: | - REF_TYPE="${{ steps.refinfo.outputs.ref_type }}" - RELEASE_TAG="${{ steps.refinfo.outputs.release_tag }}" - IMAGE="${REGISTRY}/${{ matrix.image_name_server }}" - BUILD_TAG_ARGS=( - -t "${IMAGE}:${{ matrix.latest_tag }}" - -t "${IMAGE}:${{ github.sha }}" + -t "${IMAGE_SERVER}:${LATEST_TAG}" + -t "${IMAGE_SERVER}:${{ github.sha }}" ) - if [[ "${REF_TYPE}" == "tag" && -n "${RELEASE_TAG}" ]]; then - BUILD_TAG_ARGS+=( -t "${IMAGE}:${RELEASE_TAG}" ) + 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 @@ -147,24 +135,20 @@ jobs: "${BUILD_TAG_ARGS[@]}" \ . - docker push "${IMAGE}:${{ matrix.latest_tag }}" - docker push "${IMAGE}:${{ github.sha }}" - if [[ "${REF_TYPE}" == "tag" && -n "${RELEASE_TAG}" ]]; then - docker push "${IMAGE}:${RELEASE_TAG}" + 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: | - REF_TYPE="${{ steps.refinfo.outputs.ref_type }}" - RELEASE_TAG="${{ steps.refinfo.outputs.release_tag }}" - IMAGE="${REGISTRY}/${{ matrix.image_name_db }}" - BUILD_TAG_ARGS=( - -t "${IMAGE}:${{ matrix.latest_tag }}" - -t "${IMAGE}:${{ github.sha }}" + -t "${IMAGE_DB}:${LATEST_TAG}" + -t "${IMAGE_DB}:${{ github.sha }}" ) - if [[ "${REF_TYPE}" == "tag" && -n "${RELEASE_TAG}" ]]; then - BUILD_TAG_ARGS+=( -t "${IMAGE}:${RELEASE_TAG}" ) + if [[ -n "${RELEASE_TAG}" ]]; then + BUILD_TAG_ARGS+=( -t "${IMAGE_DB}:${RELEASE_TAG}" ) fi docker build \ @@ -172,16 +156,16 @@ jobs: "${BUILD_TAG_ARGS[@]}" \ . - docker push "${IMAGE}:${{ matrix.latest_tag }}" - docker push "${IMAGE}:${{ github.sha }}" - if [[ "${REF_TYPE}" == "tag" && -n "${RELEASE_TAG}" ]]; then - docker push "${IMAGE}:${RELEASE_TAG}" + 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 "${{ matrix.namespace }}" \ + kubectl create namespace "${NAMESPACE}" \ --dry-run=client -o yaml | kubectl apply -f - # The kubelet pulls through the same Docker Desktop daemon that built the @@ -193,7 +177,7 @@ jobs: --docker-server="$(echo "${REGISTRY}" | cut -d'/' -f1)" \ --docker-username="${{ secrets.HARBOR_USERNAME }}" \ --docker-password="${{ secrets.HARBOR_PASSWORD }}" \ - --namespace "${{ matrix.namespace }}" \ + --namespace "${NAMESPACE}" \ --dry-run=client -o yaml | kubectl apply -f - # ---- DEV DEPLOY (branch dev only) ---- @@ -206,12 +190,12 @@ jobs: kubectl get nodes kubectl get ns - helm upgrade --install "${{ matrix.release }}" "${{ matrix.chart }}" \ - -f "${{ matrix.values_file }}" \ - --namespace "${{ matrix.namespace }}" \ - --set server.image.repository="${REGISTRY}/${{ matrix.image_name_server }}" \ + 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="${REGISTRY}/${{ matrix.image_name_db }}" \ + --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 }}" \ @@ -225,19 +209,18 @@ jobs: kubectl get nodes kubectl get ns - TAG="${GITHUB_REF#refs/tags/}" - if [[ -z "${TAG}" ]]; then + if [[ -z "${RELEASE_TAG}" ]]; then echo "No tag detected; aborting prod deploy." exit 1 fi - helm upgrade --install "${{ matrix.release }}" "${{ matrix.chart }}" \ - -f "${{ matrix.values_file }}" \ - --namespace "${{ matrix.namespace }}" \ - --set server.image.repository="${REGISTRY}/${{ matrix.image_name_server }}" \ - --set server.image.tag="${TAG}" \ - --set db.image.repository="${REGISTRY}/${{ matrix.image_name_db }}" \ - --set db.image.tag="${TAG}" \ + 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 }}" \ @@ -247,23 +230,23 @@ jobs: # 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() && (github.ref_type == 'branch' || github.ref_type == 'tag') + if: success() run: | kubectl rollout restart deployment \ - -l "app.kubernetes.io/instance=${{ matrix.release }},app.kubernetes.io/component=server" \ - -n "${{ matrix.namespace }}" + -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 "${{ matrix.namespace }}" -o wide || true + 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 "${{ matrix.namespace }}" -o wide || true + kubectl get pvc -n "${NAMESPACE}" -o wide || true echo "----- Events -----" - kubectl get events -n "${{ matrix.namespace }}" --sort-by=.lastTimestamp || true - for p in $(kubectl get po -n "${{ matrix.namespace }}" -o name); do + 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 "${{ matrix.namespace }}" || true - kubectl logs "$p" -n "${{ matrix.namespace }}" --all-containers --tail=200 || true + kubectl describe "$p" -n "${NAMESPACE}" || true + kubectl logs "$p" -n "${NAMESPACE}" --all-containers --tail=200 || true done