feat: refactor and improve steps
All checks were successful
renovate / renovate (push) Successful in 3m1s

This commit is contained in:
2026-03-14 19:57:46 -05:00
parent 67163611af
commit 2785f337cf

View File

@@ -43,24 +43,39 @@ jobs:
method: kubeconfig method: kubeconfig
kubeconfig: ${{ secrets.KUBECONFIG }} kubeconfig: ${{ secrets.KUBECONFIG }}
- name: Cache Helm Dependencies
uses: actions/cache@v4
with:
path: |
~/.cache/helm
~/.config/helm
key: helm-cache-${{ runner.os }}-${{ hashFiles('infrastructure/clusters/cl01tl/helm/**/Chart.yaml', 'infrastructure/clusters/cl01tl/helm/**/Chart.lock') }}
restore-keys: |
helm-cache-${{ runner.os }}-
- name: Prepare Manifest Branch - name: Prepare Manifest Branch
run: | run: |
cd ${MANIFEST_DIR} cd "${MANIFEST_DIR}"
echo ""
echo ">> Configure git to use gitea-bot as user ..." echo ">> Configure git to use gitea-bot as user ..."
git config user.name "gitea-bot" git config user.name "gitea-bot"
git config user.email "gitea-bot@alexlebens.net" git config user.email "gitea-bot@alexlebens.net"
echo ""
echo ">> Checking if PR branch exists ..." echo ">> Checking if PR branch exists ..."
if [[ $(git ls-remote --heads origin "${BRANCH_NAME}" | wc -l) -gt 0 ]]; then if git ls-remote --exit-code --heads origin "${BRANCH_NAME}" > /dev/null 2>&1; then
echo ""
echo ">> Branch '${BRANCH_NAME}' exists, pulling changes ..." echo ">> Branch '${BRANCH_NAME}' exists, pulling changes ..."
git fetch origin "${BRANCH_NAME}" git fetch origin "${BRANCH_NAME}"
git checkout "${BRANCH_NAME}" git checkout "${BRANCH_NAME}"
git pull --rebase git pull --rebase
else else
echo ""
echo ">> Branch '${BRANCH_NAME}' does not exist, creating ..." echo ">> Branch '${BRANCH_NAME}' does not exist, creating ..."
git checkout -b $BRANCH_NAME git checkout -b "${BRANCH_NAME}"
fi fi
echo "----" echo "----"
@@ -68,25 +83,29 @@ jobs:
- name: Check which Directories have Changes - name: Check which Directories have Changes
id: check-dir-changes id: check-dir-changes
run: | run: |
cd ${MAIN_DIR} cd "${MAIN_DIR}"
RENDER_DIR=()
echo ""
echo ">> Triggered on dispatch, will check all paths ..." echo ">> Triggered on dispatch, will check all paths ..."
RENDER_DIR+=$(ls clusters/cl01tl/helm/)
# Extract names of charts
RENDER_DIR=$(find "clusters/${CLUSTER}/helm" -mindepth 1 -maxdepth 1 -type d -exec basename {} \; | sort -u)
if [ -n "${RENDER_DIR}" ]; then if [ -n "${RENDER_DIR}" ]; then
echo ""
echo ">> Directories to Render:" echo ">> Directories to Render:"
echo "$(echo "${RENDER_DIR}" | sort -u)" echo "${RENDER_DIR}"
echo "----" echo "----"
echo "changes-detected=true" >> $GITEA_OUTPUT echo "changes-detected=true" >> "$GITEA_OUTPUT"
echo "render-dir<<EOF" >> $GITEA_OUTPUT echo "render-dir<<EOF" >> "$GITEA_OUTPUT"
echo "$(echo "${RENDER_DIR}" | sort -u)" >> $GITEA_OUTPUT echo "${RENDER_DIR}" >> "$GITEA_OUTPUT"
echo "EOF" >> $GITEA_OUTPUT echo "EOF" >> "$GITEA_OUTPUT"
else else
echo "changes-detected=false" >> $GITEA_OUTPUT echo ">> No directories found"
echo "changes-detected=false" >> "$GITEA_OUTPUT"
fi fi
- name: Add Repositories - name: Add Repositories
@@ -94,29 +113,54 @@ jobs:
env: env:
RENDER_DIR: ${{ steps.check-dir-changes.outputs.render-dir }} RENDER_DIR: ${{ steps.check-dir-changes.outputs.render-dir }}
run: | run: |
cd ${MAIN_DIR} cd "${MAIN_DIR}"
echo ""
echo ">> Adding repositories for chart dependencies ..." echo ">> Adding repositories for chart dependencies ..."
for dir in ${RENDER_DIR}; do for DIR in ${RENDER_DIR}; do
helm dependency list --max-col-width 120 ${MAIN_DIR}/clusters/${CLUSTER}/helm/$dir 2> /dev/null \ helm dependency list --max-col-width 120 "${MAIN_DIR}/clusters/${CLUSTER}/helm/${DIR}" 2> /dev/null \
| tail +2 | head -n -1 \ | tail -n +2 \
| awk '{ print "helm repo add " $1 " " $3 }' \ | awk 'NF > 0 { print $1, $3 }' \
| while read cmd; do | while read -r REPO_NAME REPO_URL; do
if [[ "$cmd" == "*oci://*" ]]; then if [[ "${REPO_URL}" == oci://* ]]; then
echo ">> Ignoring OCI repo" echo ""
else echo ">> Ignoring OCI repo: ${REPO_URL}"
echo "$cmd" | sh;
elif [[ -n "${REPO_NAME}" && -n "${REPO_URL}" ]]; then
helm repo add "${REPO_NAME}" "${REPO_URL}"
fi fi
done || true done || true
done done
if helm repo list | tail +2 | read -r; then if helm repo list > /dev/null 2>&1; then
echo ""
echo ">> Update repository cache ..." echo ">> Update repository cache ..."
helm repo update helm repo update
fi fi
echo "----" echo "----"
- name: Remove Changed Manifest Files
if: steps.check-dir-changes.outputs.changes-detected == 'true'
env:
RENDER_DIR: ${{ steps.check-dir-changes.outputs.render-dir }}
run: |
cd "${MANIFEST_DIR}"
echo ""
echo ">> Remove manfiest files and rebuild from source ..."
for DIR in ${RENDER_DIR}; do
local CHART_PATH=${MANIFEST_DIR}/clusters/${CLUSTER}/manifests/${DIR}
echo "${CHART_PATH}"
rm -rf ${CHART_PATH}/*
done
echo "----"
- name: Render Helm Manifests - name: Render Helm Manifests
id: render-manifests id: render-manifests
if: steps.check-dir-changes.outputs.changes-detected == 'true' if: steps.check-dir-changes.outputs.changes-detected == 'true'
@@ -125,76 +169,81 @@ jobs:
run: | run: |
cd ${MAIN_DIR} cd ${MAIN_DIR}
echo ""
echo ">> Rendering Manifests ..." echo ">> Rendering Manifests ..."
for dir in ${RENDER_DIR}; do render_chart() {
chart_path=${MAIN_DIR}/clusters/${CLUSTER}/helm/$dir local DIR="$1"
chart_name=$(basename "$chart_path") local CHART_PATH="${MAIN_DIR}/clusters/${CLUSTER}/helm/${DIR}"
local CHART_NAME=$(basename "${CHART_PATH}")
echo "" echo ""
echo "" echo ">> Rendering ..."
echo ">> Rendering chart: $chart_name" echo ">> Chart: ${CHART_NAME}"
echo ">> Chart path $chart_path" echo ">> Path: ${CHART_PATH}"
if [ -f "$chart_path/Chart.yaml" ]; then if [ -f "${CHART_PATH}/Chart.yaml" ]; then
OUTPUT_FOLDER="${MANIFEST_DIR}/clusters/${CLUSTER}/manifests/$chart_name/" local OUTPUT_FOLDER="${MANIFEST_DIR}/clusters/${CLUSTER}/manifests/${CHART_NAME}"
TEMPLATE=""
mkdir -p ${MANIFEST_DIR}/clusters/${CLUSTER}/manifests/$chart_name mkdir -p "${OUTPUT_FOLDER}"
cd "${CHART_PATH}"
cd $chart_path
echo "" echo ""
echo ">> Updating helm dependency ..." echo ">> Updating helm dependencies ..."
helm dependency update --skip-refresh helm dependency update --skip-refresh > /dev/null
echo "" echo ""
echo ">> Building helm dependency ..." echo ">> Linting helm chart ..."
helm dependency build --skip-refresh helm lint --namespace "${CHART_NAME}" --quiet
echo "" local NAMESPACE="${CHART_NAME}"
echo ">> Linting helm ..." case "${CHART_NAME}" in
helm lint --namespace "$chart_name"
echo ""
echo ">> Rendering templates ..."
case "$chart_name" in
"stack") "stack")
NAMESPACE="argocd"
echo "" echo ""
echo ">> Special Rendering for stack into argocd namespace ..." echo ">> Special Rendering into 'argocd' namespace ..."
TEMPLATE=$(helm template $chart_name ./ --namespace argocd --include-crds --dry-run=server --api-versions "gateway.networking.k8s.io/v1/HTTPRoute")
;; ;;
"cilium" | "coredns" | "metrics-server" | "prometheus-operator-crds") "cilium" | "coredns" | "metrics-server" | "prometheus-operator-crds")
NAMESPACE="kube-system"
echo "" echo ""
echo ">> Special Rendering for $chart_name into kube-system namespace ..." echo ">> Special Rendering for ${CHART_NAME} into 'kube-system' namespace ..."
TEMPLATE=$(helm template $chart_name ./ --namespace kube-system --include-crds --dry-run=server --api-versions "gateway.networking.k8s.io/v1/HTTPRoute")
;; ;;
*) *)
echo "" echo ""
echo ">> Standard Rendering for $chart_name ..." echo ">> Standard Rendering for ${CHART_NAME} ..."
TEMPLATE=$(helm template "$chart_name" ./ --namespace "$chart_name" --include-crds --dry-run=server --api-versions "gateway.networking.k8s.io/v1/HTTPRoute")
;;
esac esac
echo "" echo ""
echo ">> Formating rendered template ..." echo ">> Formating rendered template ..."
echo "$TEMPLATE" | yq '... comments=""' | yq 'select(. != null)' | yq -s '"'"$OUTPUT_FOLDER"'" + .kind + "-" + .metadata.name + ".yaml"' local TEMPLATE
TEMPLATE=$(helm template "${CHART_NAME}" ./ --namespace "${NAMESPACE}" --include-crds)
# Format and split rendered template
echo "${TEMPLATE}" | yq '... comments=""' | yq 'select(. != null) | yq -s '"'"${OUTPUT_FOLDER}"'/" + .kind + "-" + .metadata.name + ".yaml"'
# Strip comments again to ensure formatting correctness # Strip comments again to ensure formatting correctness
for file in "$OUTPUT_FOLDER"/*; do if ls "${OUTPUT_FOLDER}"/*.yaml 1> /dev/null 2>&1; then
yq -i '... comments=""' $file yq -i '... comments=""' "${OUTPUT_FOLDER}"/*.yaml
done fi
echo "" echo ""
echo ">> Manifests for $chart_name rendered to $OUTPUT_FOLDER" echo ">> Manifests for ${CHART_NAME} rendered to ${OUTPUT_FOLDER}:"
ls $OUTPUT_FOLDER ls $OUTPUT_FOLDER
echo "" echo ""
else else
echo "" echo ""
echo ">> Directory $chart_path does not contain a Chart.yaml. Skipping ..." echo ">> Directory ${CHART_PATH} does not contain a Chart.yaml. Skipping ..."
echo "" echo ""
fi fi
done }
export -f render_chart
export MAIN_DIR CLUSTER MANIFEST_DIR
# Run rendering in parallel
for DIR in ${RENDER_DIR}; do
echo "${DIR}"
done | xargs -n 1 -P 4 -I {} bash -c 'render_chart "$@"' _ {}
echo "----" echo "----"
@@ -202,40 +251,46 @@ jobs:
id: check-changes id: check-changes
if: steps.check-dir-changes.outputs.changes-detected == 'true' if: steps.check-dir-changes.outputs.changes-detected == 'true'
run: | run: |
cd ${MANIFEST_DIR} cd "${MANIFEST_DIR}"
GIT_CHANGES=$(git status --porcelain) GIT_CHANGES=$(git status --porcelain)
if [ -n "$GIT_CHANGES" ]; then if [ -n "${GIT_CHANGES}" ]; then
echo ""
echo ">> Changes detected" echo ">> Changes detected"
git status --porcelain git status --porcelain
echo "changes-detected=true" >> $GITEA_OUTPUT echo "changes-detected=true" >> $GITEA_OUTPUT
else else
echo ""
echo ">> No changes detected, skipping PR creation" echo ">> No changes detected, skipping PR creation"
fi fi
echo "----" echo "----"
echo "----"
- name: Commit and Push Changes - name: Commit and Push Changes
id: commit-push id: commit-push
if: steps.check-changes.outputs.changes-detected == 'true' if: steps.check-changes.outputs.changes-detected == 'true'
run: | run: |
cd ${MANIFEST_DIR} cd "${MANIFEST_DIR}"
echo ""
echo ">> Commiting changes to ${BRANCH_NAME} ..." echo ">> Commiting changes to ${BRANCH_NAME} ..."
git add . git add .
git commit -m "chore: Update manifests after change" git commit -m "chore: Update manifests after change"
REPO_URL="${{ secrets.REPO_URL }}/${{ gitea.repository }}" REPO_URL="${{ secrets.REPO_URL }}/${{ gitea.repository }}"
echo ">> Pushing changes to $REPO_URL ..." echo ""
git push -u "https://oauth2:${{ secrets.BOT_TOKEN }}@$(echo $REPO_URL | sed -e 's|https://||')" ${BRANCH_NAME} echo ">> Pushing changes to ${REPO_URL} ..."
git push -u "https://oauth2:${{ secrets.BOT_TOKEN }}@${REPO_URL#*://}" "${BRANCH_NAME}"
echo "----" echo "----"
echo "HEAD_BRANCH=${BRANCH_NAME}" >> $GITEA_OUTPUT echo "HEAD_BRANCH=${BRANCH_NAME}" >> "$GITEA_OUTPUT"
echo "push=true" >> $GITEA_OUTPUT echo "push=true" >> "$GITEA_OUTPUT"
- name: Check for Pull Request - name: Check for Pull Request
id: check-for-pull-requst id: check-for-pull-requst