Feature/git pages -> oma nginx perusteinen raporointipalvelu (#48)
CI Gitea Reports Main / Config load (push) Successful in 17s
CI Main / Config load (push) Successful in 20s
CI Gitea Reports Main / Latest version (push) Successful in 17s
CI Main / Latest versio (push) Successful in 19s
ci-helm-build-push Helm push 0.2.0
CI Gitea Reports Main / Build & Push Helm chart (push) Successful in 33s
gitops/gitea-ci-library/gitea-reports GitOps: gitea-reports 0.2.0
CI Gitea Reports Main / GitOps (push) Successful in 33s
CI Gitea Reports Main / Report Summary (push) Successful in 3s
CI Main / Bats tests (push) Failing after 1m16s
CI Main / Cucumber tests (push) Failing after 1m22s
CI Main / Build & Push Docker (push) Has been skipped
CI Main / GitOps (push) Has been skipped
CI Main / Move provider version tag (push) Has been skipped
CI Main / Report Summary (push) Successful in 3s
CI Gitea Reports Main / Config load (push) Successful in 17s
CI Main / Config load (push) Successful in 20s
CI Gitea Reports Main / Latest version (push) Successful in 17s
CI Main / Latest versio (push) Successful in 19s
ci-helm-build-push Helm push 0.2.0
CI Gitea Reports Main / Build & Push Helm chart (push) Successful in 33s
gitops/gitea-ci-library/gitea-reports GitOps: gitea-reports 0.2.0
CI Gitea Reports Main / GitOps (push) Successful in 33s
CI Gitea Reports Main / Report Summary (push) Successful in 3s
CI Main / Bats tests (push) Failing after 1m16s
CI Main / Cucumber tests (push) Failing after 1m22s
CI Main / Build & Push Docker (push) Has been skipped
CI Main / GitOps (push) Has been skipped
CI Main / Move provider version tag (push) Has been skipped
CI Main / Report Summary (push) Successful in 3s
Co-authored-by: moilanik <niko.moilanen@tietoevry.com> Reviewed-on: #48
This commit was merged in pull request #48.
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
#!/usr/bin/env bash
|
||||
# Retention cleanup: read PV directly, no git-pages API.
|
||||
# Removes old commit-level report dirs based on branch existence + age rules.
|
||||
set -euo pipefail
|
||||
|
||||
DATA_ROOT="${DATA_ROOT:-/app/data}"
|
||||
CONFIG="${RETENTION_CONFIG:-/etc/retention/retention.json}"
|
||||
GITEA_API_URL="${GITEA_API_URL:-}"
|
||||
GITEA_TOKEN="${GITEA_TOKEN:-}"
|
||||
|
||||
[ -f "$CONFIG" ] || { echo "ERROR: config missing: $CONFIG" >&2; exit 1; }
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
source "$SCRIPT_DIR/retention-lib.sh"
|
||||
|
||||
DELETED_COUNT=0
|
||||
ERROR_COUNT=0
|
||||
|
||||
for owner_dir in "$DATA_ROOT"/*/; do
|
||||
[ -d "$owner_dir" ] || continue
|
||||
owner=$(basename "$owner_dir")
|
||||
|
||||
for repo_dir in "$owner_dir"*/; do
|
||||
[ -d "$repo_dir" ] || continue
|
||||
repo=$(basename "$repo_dir")
|
||||
|
||||
for branch_dir in "$repo_dir"*/; do
|
||||
[ -d "$branch_dir" ] || continue
|
||||
branch=$(basename "$branch_dir")
|
||||
|
||||
echo "=== ${owner}/${repo}/${branch} ==="
|
||||
|
||||
if ! branch_exists "$owner" "$repo" "$branch"; then
|
||||
echo " Branch does not exist in Gitea -> DELETE ALL"
|
||||
rm -rf "$branch_dir"
|
||||
DELETED_COUNT=$((DELETED_COUNT + 1))
|
||||
continue
|
||||
fi
|
||||
|
||||
rules=$(jq -r ".branches.\"$branch\" // .branches.default" "$CONFIG")
|
||||
keep_min=$(echo "$rules" | jq -r '.keepMin // 5')
|
||||
min_age=$(echo "$rules" | jq -r '.minAgeDays // 7')
|
||||
|
||||
commits=$(ls -1t "$branch_dir" 2>/dev/null || true)
|
||||
count=0
|
||||
for sha8 in $commits; do
|
||||
[ -d "$branch_dir/$sha8" ] || continue
|
||||
count=$((count + 1))
|
||||
|
||||
[ "$count" -le "$keep_min" ] && continue
|
||||
|
||||
age=$(age_days_from_stat "$branch_dir/$sha8")
|
||||
[ "$age" -lt "$min_age" ] && continue
|
||||
|
||||
echo " DELETE: ${owner}/${repo}/${branch}/${sha8} (age=${age}d, keepMin=${keep_min})"
|
||||
rm -rf "$branch_dir/$sha8"
|
||||
DELETED_COUNT=$((DELETED_COUNT + 1))
|
||||
done
|
||||
done
|
||||
done
|
||||
done
|
||||
|
||||
echo ""
|
||||
echo "=== Summary ==="
|
||||
echo " Deleted: ${DELETED_COUNT}"
|
||||
echo " Errors: ${ERROR_COUNT}"
|
||||
@@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env bash
|
||||
# Shared functions for retention-cleanup.sh
|
||||
# Can be sourced by tests for unit testing
|
||||
|
||||
age_days_from_stat() {
|
||||
local dir="$1" epoch_dir now
|
||||
epoch_dir=$(stat -c %Y "$dir" 2>/dev/null || stat -f %m "$dir" 2>/dev/null || echo 0)
|
||||
[ "$epoch_dir" -eq 0 ] && echo 99999 && return
|
||||
now=$(date +%s)
|
||||
echo $(( (now - epoch_dir) / 86400 ))
|
||||
}
|
||||
|
||||
read_rule() {
|
||||
local config="$1" branch="$2" key="$3" default="$4"
|
||||
v=$(jq -r --arg b "$branch" --arg k "$key" '.branches[$b][$k] // empty' "$config")
|
||||
[ -n "$v" ] && echo "$v" || echo "$default"
|
||||
}
|
||||
|
||||
branch_exists() {
|
||||
local owner="$1" repo="$2" branch="$3"
|
||||
local branch_urlenc code
|
||||
|
||||
[ -z "$GITEA_API_URL" ] && return 0
|
||||
[ -z "$GITEA_TOKEN" ] && return 0
|
||||
|
||||
branch_urlenc=$(echo "$branch" | sed 's/\//%2F/g')
|
||||
code=$(curl -s -o /dev/null -w "%{http_code}" \
|
||||
-H "Authorization: token $GITEA_TOKEN" \
|
||||
"$GITEA_API_URL/api/v1/repos/$owner/$repo/branches/$branch_urlenc" 2>/dev/null || echo "000")
|
||||
|
||||
[ "$code" = "200" ]
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
# Run retention cleanup on PVC directly (no git-pages API).
|
||||
set -euo pipefail
|
||||
|
||||
/scripts/retention-cleanup.sh
|
||||
echo "Retention job done."
|
||||
Reference in New Issue
Block a user