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

This commit is contained in:
2026-03-14 20:56:01 -05:00
parent 8bfb5aeea3
commit 78d15dccad

View File

@@ -46,6 +46,16 @@ jobs:
method: kubeconfig method: kubeconfig
kubeconfig: ${{ secrets.KUBECONFIG }} kubeconfig: ${{ secrets.KUBECONFIG }}
- name: Cache Helm Dependencies
uses: actions/cache@v5
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
id: prepare-manifest-branch id: prepare-manifest-branch
run: | run: |
@@ -53,10 +63,12 @@ jobs:
BRANCH_NAME="${BRANCH_NAME_BASE}-$(date +%Y%m%d%H%M%S)" BRANCH_NAME="${BRANCH_NAME_BASE}-$(date +%Y%m%d%H%M%S)"
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 ">> Creating branch ..." echo ">> Creating branch ..."
git checkout -b $BRANCH_NAME git checkout -b $BRANCH_NAME
@@ -67,38 +79,31 @@ 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 ">> Checking for changes from HEAD^..HEAD ..." echo ">> Checking for changes from HEAD^..HEAD ..."
GIT_DIFF=$(git diff --name-only HEAD^..HEAD | xargs -I {} dirname {} | sort -u | grep -E "clusters/[^/]+/helm/[^/]+")
if [ -n "${GIT_DIFF}" ]; then # Extract the chart names from the git diff
echo ">> Changes detected:" RENDER_DIR=$(git diff --name-only HEAD^..HEAD | grep -E "^clusters/${CLUSTER}/helm/" | awk -F '/' '{print $4}' | sort -u || true)
echo "$GIT_DIFF"
for path in $GIT_DIFF; do
RENDER_DIR+=$(echo "$path" | awk -F '/' '{print $4}')
RENDER_DIR+=$(echo " ")
done
else
echo ">> No changes detected"
fi
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 ""
echo ">> No chart changes detected"
echo "changes-detected=false" >> "$GITEA_OUTPUT"
fi fi
- name: Add Repositories - name: Add Repositories
@@ -106,25 +111,31 @@ 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 "----"
@@ -134,15 +145,16 @@ jobs:
env: env:
RENDER_DIR: ${{ steps.check-dir-changes.outputs.render-dir }} RENDER_DIR: ${{ steps.check-dir-changes.outputs.render-dir }}
run: | run: |
cd ${MANIFEST_DIR} cd "${MANIFEST_DIR}"
echo ""
echo ">> Remove manfiest files and rebuild from source ..." echo ">> Remove manfiest files and rebuild from source ..."
for dir in ${RENDER_DIR}; do for DIR in ${RENDER_DIR}; do
chart_path=${MANIFEST_DIR}/clusters/${CLUSTER}/manifests/$dir CHART_PATH=${MANIFEST_DIR}/clusters/${CLUSTER}/manifests/${DIR}
echo "$chart_path" echo "${CHART_PATH}"
rm -rf $chart_path/* rm -rf ${CHART_PATH}/*
done done
echo "----" echo "----"
@@ -155,60 +167,57 @@ 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 --dry-run=server --api-versions "gateway.networking.k8s.io/v1/HTTPRoute)
# 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 for file in "$OUTPUT_FOLDER"/*; do
@@ -216,15 +225,23 @@ jobs:
done done
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 "----"
@@ -232,16 +249,18 @@ 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
@@ -254,19 +273,22 @@ jobs:
env: env:
BRANCH_NAME: ${{ steps.prepare-manifest-branch.outputs.BRANCH_NAME }} BRANCH_NAME: ${{ steps.prepare-manifest-branch.outputs.BRANCH_NAME }}
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 automerge" git commit -m "chore: Update manifests after automerge"
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 "push=true" >> $GITEA_OUTPUT echo "push=true" >> "$GITEA_OUTPUT"
- name: Create Pull Request - name: Create Pull Request
id: create-pull-request id: create-pull-request