ci: drop dynamic matrix (unsupported by act_runner), compute env config in bash
CI/CD (dev on branch, prod on tag) / build-and-deploy (push) Successful in 5m0s
CI/CD (dev on branch, prod on tag) / build-and-deploy (push) Successful in 5m0s
This commit is contained in:
+76
-93
@@ -8,32 +8,16 @@ on:
|
|||||||
- "*" # build & deploy for prod only when a tag is pushed
|
- "*" # build & deploy for prod only when a tag is pushed
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
setup:
|
build-and-deploy:
|
||||||
# Gitea Actions: runners are selected by label only (no runner groups).
|
# Gitea Actions: runners are selected by label only (no runner groups).
|
||||||
runs-on: ubuntu-latest
|
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
|
# 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:
|
concurrency:
|
||||||
group: deploy-${{ matrix.env }}
|
group: deploy-${{ github.ref_type == 'tag' && 'prod' || 'dev' }}
|
||||||
cancel-in-progress: true
|
cancel-in-progress: true
|
||||||
|
|
||||||
env:
|
env:
|
||||||
@@ -52,8 +36,34 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
fetch-depth: 0 # IMPORTANT to access tags
|
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)
|
- name: Verify tag is on prod branch (main)
|
||||||
id: guard
|
|
||||||
if: github.ref_type == 'tag'
|
if: github.ref_type == 'tag'
|
||||||
shell: bash
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
@@ -96,47 +106,25 @@ jobs:
|
|||||||
-u "${{ secrets.HARBOR_USERNAME }}" \
|
-u "${{ secrets.HARBOR_USERNAME }}" \
|
||||||
--password-stdin
|
--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
|
- name: Pre-build diagnostics
|
||||||
run: |
|
run: |
|
||||||
echo "Ref: ${GITHUB_REF}"
|
echo "Ref: ${GITHUB_REF}"
|
||||||
echo "Ref type: ${{ steps.refinfo.outputs.ref_type }}"
|
echo "Environment: ${DEPLOY_ENV}"
|
||||||
echo "Release tag: ${{ steps.refinfo.outputs.release_tag }}"
|
echo "Release tag: ${RELEASE_TAG}"
|
||||||
echo "Environment: ${{ matrix.env }}"
|
echo "Server image: ${IMAGE_SERVER}"
|
||||||
echo "Server image: ${REGISTRY}/${{ matrix.image_name_server }}"
|
echo "DB image: ${IMAGE_DB}"
|
||||||
echo "DB image: ${REGISTRY}/${{ matrix.image_name_db }}"
|
|
||||||
docker version
|
docker version
|
||||||
helm version
|
helm version
|
||||||
kubectl version --client
|
kubectl version --client
|
||||||
|
|
||||||
- name: Build & Push server image
|
- name: Build & Push server image
|
||||||
run: |
|
run: |
|
||||||
REF_TYPE="${{ steps.refinfo.outputs.ref_type }}"
|
|
||||||
RELEASE_TAG="${{ steps.refinfo.outputs.release_tag }}"
|
|
||||||
IMAGE="${REGISTRY}/${{ matrix.image_name_server }}"
|
|
||||||
|
|
||||||
BUILD_TAG_ARGS=(
|
BUILD_TAG_ARGS=(
|
||||||
-t "${IMAGE}:${{ matrix.latest_tag }}"
|
-t "${IMAGE_SERVER}:${LATEST_TAG}"
|
||||||
-t "${IMAGE}:${{ github.sha }}"
|
-t "${IMAGE_SERVER}:${{ github.sha }}"
|
||||||
)
|
)
|
||||||
if [[ "${REF_TYPE}" == "tag" && -n "${RELEASE_TAG}" ]]; then
|
if [[ -n "${RELEASE_TAG}" ]]; then
|
||||||
BUILD_TAG_ARGS+=( -t "${IMAGE}:${RELEASE_TAG}" )
|
BUILD_TAG_ARGS+=( -t "${IMAGE_SERVER}:${RELEASE_TAG}" )
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# AUTH_SECRET is build-time only (Auth.js v5 initializes NextAuth() at
|
# AUTH_SECRET is build-time only (Auth.js v5 initializes NextAuth() at
|
||||||
@@ -147,24 +135,20 @@ jobs:
|
|||||||
"${BUILD_TAG_ARGS[@]}" \
|
"${BUILD_TAG_ARGS[@]}" \
|
||||||
.
|
.
|
||||||
|
|
||||||
docker push "${IMAGE}:${{ matrix.latest_tag }}"
|
docker push "${IMAGE_SERVER}:${LATEST_TAG}"
|
||||||
docker push "${IMAGE}:${{ github.sha }}"
|
docker push "${IMAGE_SERVER}:${{ github.sha }}"
|
||||||
if [[ "${REF_TYPE}" == "tag" && -n "${RELEASE_TAG}" ]]; then
|
if [[ -n "${RELEASE_TAG}" ]]; then
|
||||||
docker push "${IMAGE}:${RELEASE_TAG}"
|
docker push "${IMAGE_SERVER}:${RELEASE_TAG}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
- name: Build & Push db image
|
- name: Build & Push db image
|
||||||
run: |
|
run: |
|
||||||
REF_TYPE="${{ steps.refinfo.outputs.ref_type }}"
|
|
||||||
RELEASE_TAG="${{ steps.refinfo.outputs.release_tag }}"
|
|
||||||
IMAGE="${REGISTRY}/${{ matrix.image_name_db }}"
|
|
||||||
|
|
||||||
BUILD_TAG_ARGS=(
|
BUILD_TAG_ARGS=(
|
||||||
-t "${IMAGE}:${{ matrix.latest_tag }}"
|
-t "${IMAGE_DB}:${LATEST_TAG}"
|
||||||
-t "${IMAGE}:${{ github.sha }}"
|
-t "${IMAGE_DB}:${{ github.sha }}"
|
||||||
)
|
)
|
||||||
if [[ "${REF_TYPE}" == "tag" && -n "${RELEASE_TAG}" ]]; then
|
if [[ -n "${RELEASE_TAG}" ]]; then
|
||||||
BUILD_TAG_ARGS+=( -t "${IMAGE}:${RELEASE_TAG}" )
|
BUILD_TAG_ARGS+=( -t "${IMAGE_DB}:${RELEASE_TAG}" )
|
||||||
fi
|
fi
|
||||||
|
|
||||||
docker build \
|
docker build \
|
||||||
@@ -172,16 +156,16 @@ jobs:
|
|||||||
"${BUILD_TAG_ARGS[@]}" \
|
"${BUILD_TAG_ARGS[@]}" \
|
||||||
.
|
.
|
||||||
|
|
||||||
docker push "${IMAGE}:${{ matrix.latest_tag }}"
|
docker push "${IMAGE_DB}:${LATEST_TAG}"
|
||||||
docker push "${IMAGE}:${{ github.sha }}"
|
docker push "${IMAGE_DB}:${{ github.sha }}"
|
||||||
if [[ "${REF_TYPE}" == "tag" && -n "${RELEASE_TAG}" ]]; then
|
if [[ -n "${RELEASE_TAG}" ]]; then
|
||||||
docker push "${IMAGE}:${RELEASE_TAG}"
|
docker push "${IMAGE_DB}:${RELEASE_TAG}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
- name: Ensure namespace exists
|
- name: Ensure namespace exists
|
||||||
run: |
|
run: |
|
||||||
# `helm --atomic` against a missing namespace fails outright.
|
# `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 -
|
--dry-run=client -o yaml | kubectl apply -f -
|
||||||
|
|
||||||
# The kubelet pulls through the same Docker Desktop daemon that built the
|
# 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-server="$(echo "${REGISTRY}" | cut -d'/' -f1)" \
|
||||||
--docker-username="${{ secrets.HARBOR_USERNAME }}" \
|
--docker-username="${{ secrets.HARBOR_USERNAME }}" \
|
||||||
--docker-password="${{ secrets.HARBOR_PASSWORD }}" \
|
--docker-password="${{ secrets.HARBOR_PASSWORD }}" \
|
||||||
--namespace "${{ matrix.namespace }}" \
|
--namespace "${NAMESPACE}" \
|
||||||
--dry-run=client -o yaml | kubectl apply -f -
|
--dry-run=client -o yaml | kubectl apply -f -
|
||||||
|
|
||||||
# ---- DEV DEPLOY (branch dev only) ----
|
# ---- DEV DEPLOY (branch dev only) ----
|
||||||
@@ -206,12 +190,12 @@ jobs:
|
|||||||
kubectl get nodes
|
kubectl get nodes
|
||||||
kubectl get ns
|
kubectl get ns
|
||||||
|
|
||||||
helm upgrade --install "${{ matrix.release }}" "${{ matrix.chart }}" \
|
helm upgrade --install "${RELEASE}" "${CHART}" \
|
||||||
-f "${{ matrix.values_file }}" \
|
-f "${VALUES_FILE}" \
|
||||||
--namespace "${{ matrix.namespace }}" \
|
--namespace "${NAMESPACE}" \
|
||||||
--set server.image.repository="${REGISTRY}/${{ matrix.image_name_server }}" \
|
--set server.image.repository="${IMAGE_SERVER}" \
|
||||||
--set server.image.tag="${{ github.sha }}" \
|
--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 db.image.tag="${{ github.sha }}" \
|
||||||
--set-string secrets.authSecret="${{ secrets.AUTH_SECRET_DEV }}" \
|
--set-string secrets.authSecret="${{ secrets.AUTH_SECRET_DEV }}" \
|
||||||
--set-string secrets.dbRootPassword="${{ secrets.DB_PASSWORD_DEV }}" \
|
--set-string secrets.dbRootPassword="${{ secrets.DB_PASSWORD_DEV }}" \
|
||||||
@@ -225,19 +209,18 @@ jobs:
|
|||||||
kubectl get nodes
|
kubectl get nodes
|
||||||
kubectl get ns
|
kubectl get ns
|
||||||
|
|
||||||
TAG="${GITHUB_REF#refs/tags/}"
|
if [[ -z "${RELEASE_TAG}" ]]; then
|
||||||
if [[ -z "${TAG}" ]]; then
|
|
||||||
echo "No tag detected; aborting prod deploy."
|
echo "No tag detected; aborting prod deploy."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
helm upgrade --install "${{ matrix.release }}" "${{ matrix.chart }}" \
|
helm upgrade --install "${RELEASE}" "${CHART}" \
|
||||||
-f "${{ matrix.values_file }}" \
|
-f "${VALUES_FILE}" \
|
||||||
--namespace "${{ matrix.namespace }}" \
|
--namespace "${NAMESPACE}" \
|
||||||
--set server.image.repository="${REGISTRY}/${{ matrix.image_name_server }}" \
|
--set server.image.repository="${IMAGE_SERVER}" \
|
||||||
--set server.image.tag="${TAG}" \
|
--set server.image.tag="${RELEASE_TAG}" \
|
||||||
--set db.image.repository="${REGISTRY}/${{ matrix.image_name_db }}" \
|
--set db.image.repository="${IMAGE_DB}" \
|
||||||
--set db.image.tag="${TAG}" \
|
--set db.image.tag="${RELEASE_TAG}" \
|
||||||
--set-string secrets.authSecret="${{ secrets.AUTH_SECRET_PROD }}" \
|
--set-string secrets.authSecret="${{ secrets.AUTH_SECRET_PROD }}" \
|
||||||
--set-string secrets.dbRootPassword="${{ secrets.DB_PASSWORD_PROD }}" \
|
--set-string secrets.dbRootPassword="${{ secrets.DB_PASSWORD_PROD }}" \
|
||||||
--set-string secrets.localAdminPassword="${{ secrets.LOCAL_ADMIN_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.
|
# pods and --atomic already verified health. Retained as belt-and-braces.
|
||||||
# Scoped to component=server so it does not bounce MySQL every push.
|
# Scoped to component=server so it does not bounce MySQL every push.
|
||||||
- name: Rollout restart (server only)
|
- name: Rollout restart (server only)
|
||||||
if: success() && (github.ref_type == 'branch' || github.ref_type == 'tag')
|
if: success()
|
||||||
run: |
|
run: |
|
||||||
kubectl rollout restart deployment \
|
kubectl rollout restart deployment \
|
||||||
-l "app.kubernetes.io/instance=${{ matrix.release }},app.kubernetes.io/component=server" \
|
-l "app.kubernetes.io/instance=${RELEASE},app.kubernetes.io/component=server" \
|
||||||
-n "${{ matrix.namespace }}"
|
-n "${NAMESPACE}"
|
||||||
|
|
||||||
- name: Post-deploy diagnostics (if failure)
|
- name: Post-deploy diagnostics (if failure)
|
||||||
if: failure()
|
if: failure()
|
||||||
run: |
|
run: |
|
||||||
echo "----- Diagnostics -----"
|
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) -----"
|
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 -----"
|
echo "----- Events -----"
|
||||||
kubectl get events -n "${{ matrix.namespace }}" --sort-by=.lastTimestamp || true
|
kubectl get events -n "${NAMESPACE}" --sort-by=.lastTimestamp || true
|
||||||
for p in $(kubectl get po -n "${{ matrix.namespace }}" -o name); do
|
for p in $(kubectl get po -n "${NAMESPACE}" -o name); do
|
||||||
echo "### $p"
|
echo "### $p"
|
||||||
kubectl describe "$p" -n "${{ matrix.namespace }}" || true
|
kubectl describe "$p" -n "${NAMESPACE}" || true
|
||||||
kubectl logs "$p" -n "${{ matrix.namespace }}" --all-containers --tail=200 || true
|
kubectl logs "$p" -n "${NAMESPACE}" --all-containers --tail=200 || true
|
||||||
done
|
done
|
||||||
|
|||||||
Reference in New Issue
Block a user