ci: complete JD01 PaC Gitea env reuse flow
This commit is contained in:
@@ -79,10 +79,20 @@ ensure_token() {
|
||||
|
||||
ensure_webhook() {
|
||||
hooks=$(gitea_api GET "repos/$UNIDESK_PAC_GITEA_OWNER/$UNIDESK_PAC_GITEA_REPO/hooks")
|
||||
hook_id=$(printf '%s' "$hooks" | tr '{' '\n' | awk -v url="$UNIDESK_PAC_WEBHOOK_URL" 'index($0, url)>0 { if (match($0, /"id"[[:space:]]*:[[:space:]]*[0-9]+/)) { print substr($0, RSTART); exit } }' | sed -n 's/[^0-9]*\([0-9][0-9]*\).*/\1/p')
|
||||
hook_ids=$(HOOKS_JSON="$hooks" node <<'NODE'
|
||||
const data = JSON.parse(process.env.HOOKS_JSON || '[]');
|
||||
const url = process.env.UNIDESK_PAC_WEBHOOK_URL;
|
||||
for (const item of data) if (item?.config?.url === url && item?.id) console.log(item.id);
|
||||
NODE
|
||||
)
|
||||
hook_id=$(printf '%s\n' "$hook_ids" | sed -n '1p')
|
||||
body=$(printf '{"type":"gitea","config":{"url":"%s","content_type":"json","secret":"%s"},"events":["push"],"active":true}' "$UNIDESK_PAC_WEBHOOK_URL" "$UNIDESK_PAC_WEBHOOK_SECRET")
|
||||
if [ -n "$hook_id" ]; then
|
||||
gitea_api PATCH "repos/$UNIDESK_PAC_GITEA_OWNER/$UNIDESK_PAC_GITEA_REPO/hooks/$hook_id" "$body" >/dev/null
|
||||
printf '%s\n' "$hook_ids" | sed -n '2,$p' | while IFS= read -r duplicate_id; do
|
||||
[ -n "$duplicate_id" ] || continue
|
||||
gitea_api DELETE "repos/$UNIDESK_PAC_GITEA_OWNER/$UNIDESK_PAC_GITEA_REPO/hooks/$duplicate_id" >/dev/null || true
|
||||
done
|
||||
else
|
||||
created=$(gitea_api POST "repos/$UNIDESK_PAC_GITEA_OWNER/$UNIDESK_PAC_GITEA_REPO/hooks" "$body")
|
||||
hook_id=$(printf '%s' "$created" | sed -n 's/.*"id"[[:space:]]*:[[:space:]]*\([0-9][0-9]*\).*/\1/p')
|
||||
@@ -164,9 +174,11 @@ condition_status() {
|
||||
}
|
||||
|
||||
pipeline_rows() {
|
||||
payload=$(kubectl -n "$UNIDESK_PAC_TARGET_NAMESPACE" get pipelinerun -o json 2>/dev/null || echo '{"items":[]}')
|
||||
PIPELINE_JSON="$payload" node <<'NODE'
|
||||
const input = process.env.PIPELINE_JSON || '{"items":[]}';
|
||||
payload_file=$(mktemp)
|
||||
kubectl -n "$UNIDESK_PAC_TARGET_NAMESPACE" get pipelinerun -o json >"$payload_file" 2>/dev/null || printf '{"items":[]}' >"$payload_file"
|
||||
node - "$payload_file" <<'NODE'
|
||||
const fs = require('node:fs');
|
||||
const input = fs.readFileSync(process.argv[2], 'utf8') || '{"items":[]}';
|
||||
const data = input ? JSON.parse(input) : { items: [] };
|
||||
function cond(item) {
|
||||
const c = (item.status?.conditions || []).find((x) => x.type === 'Succeeded') || {};
|
||||
@@ -198,12 +210,15 @@ const rows = (data.items || [])
|
||||
});
|
||||
process.stdout.write(JSON.stringify(rows));
|
||||
NODE
|
||||
rm -f "$payload_file"
|
||||
}
|
||||
|
||||
task_rows() {
|
||||
payload=$(kubectl -n "$UNIDESK_PAC_TARGET_NAMESPACE" get taskrun -o json 2>/dev/null || echo '{"items":[]}')
|
||||
TASK_JSON="$payload" node <<'NODE'
|
||||
const input = process.env.TASK_JSON || '{"items":[]}';
|
||||
payload_file=$(mktemp)
|
||||
kubectl -n "$UNIDESK_PAC_TARGET_NAMESPACE" get taskrun -o json >"$payload_file" 2>/dev/null || printf '{"items":[]}' >"$payload_file"
|
||||
node - "$payload_file" <<'NODE'
|
||||
const fs = require('node:fs');
|
||||
const input = fs.readFileSync(process.argv[2], 'utf8') || '{"items":[]}';
|
||||
const data = input ? JSON.parse(input) : { items: [] };
|
||||
const pr = process.env.UNIDESK_PAC_TARGET_PIPELINERUN || '';
|
||||
function cond(item) {
|
||||
@@ -226,6 +241,37 @@ const rows = (data.items || [])
|
||||
});
|
||||
process.stdout.write(JSON.stringify(rows));
|
||||
NODE
|
||||
rm -f "$payload_file"
|
||||
}
|
||||
|
||||
artifact_summary() {
|
||||
if [ -z "${UNIDESK_PAC_TARGET_PIPELINERUN:-}" ]; then
|
||||
printf '{}'
|
||||
return
|
||||
fi
|
||||
log_file=$(mktemp)
|
||||
kubectl -n "$UNIDESK_PAC_TARGET_NAMESPACE" logs -l "tekton.dev/pipelineRun=$UNIDESK_PAC_TARGET_PIPELINERUN" --all-containers --tail=240 >"$log_file" 2>/dev/null || true
|
||||
node - "$log_file" <<'NODE'
|
||||
const fs = require('node:fs');
|
||||
const lines = fs.readFileSync(process.argv[2], 'utf8').split(/\r?\n/);
|
||||
const records = [];
|
||||
for (const line of lines) {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed.startsWith('{') || !trimmed.endsWith('}')) continue;
|
||||
try { records.push(JSON.parse(trimmed)); } catch {}
|
||||
}
|
||||
const publish = [...records].reverse().find((item) => item.phase === 'gitops-publish' || item.gitopsCommit);
|
||||
const image = publish || [...records].reverse().find((item) => item.imageStatus || item.status === 'reused' || item.status === 'built');
|
||||
process.stdout.write(JSON.stringify(image ? {
|
||||
imageStatus: image.imageStatus || image.status || null,
|
||||
envIdentity: image.envIdentity || null,
|
||||
digest: image.digest || null,
|
||||
gitopsCommit: image.gitopsCommit || null,
|
||||
sourceCommit: image.sourceCommit || null,
|
||||
valuesPrinted: false,
|
||||
} : { valuesPrinted: false }));
|
||||
NODE
|
||||
rm -f "$log_file"
|
||||
}
|
||||
|
||||
hook_summary() {
|
||||
@@ -246,14 +292,15 @@ status_action() {
|
||||
latest=$(printf '%s' "$pipelines" | node -e 'const fs=require("fs"); const a=JSON.parse(fs.readFileSync(0,"utf8")||"[]"); process.stdout.write(a[0]?.name||"")')
|
||||
export UNIDESK_PAC_TARGET_PIPELINERUN="$latest"
|
||||
tasks=$(task_rows)
|
||||
artifact=$(artifact_summary)
|
||||
hooks=$(hook_summary)
|
||||
argo=$(kubectl -n "$UNIDESK_PAC_ARGO_NAMESPACE" get application "$UNIDESK_PAC_ARGO_APPLICATION" -o json 2>/dev/null | node -e 'const fs=require("fs"); const s=fs.readFileSync(0,"utf8").trim(); if(!s){process.stdout.write("{}"); process.exit(0)} const a=JSON.parse(s); process.stdout.write(JSON.stringify({sync:a.status?.sync?.status||null, health:a.status?.health?.status||null, revision:a.status?.sync?.revision||null}))' || echo '{}')
|
||||
printf '{"ok":%s,"crdPresent":%s,"controllerReady":"%s","repositoryCondition":"%s","webhooks":%s,"pipelineRuns":%s,"taskRuns":%s,"argo":%s,"valuesPrinted":false}\n' \
|
||||
printf '{"ok":%s,"crdPresent":%s,"controllerReady":"%s","repositoryCondition":"%s","webhooks":%s,"pipelineRuns":%s,"taskRuns":%s,"artifact":%s,"argo":%s,"valuesPrinted":false}\n' \
|
||||
"$( [ -n "$crd" ] && [ "$controller_ready" != "0/0" ] && echo true || echo false )" \
|
||||
"$( [ -n "$crd" ] && echo true || echo false )" \
|
||||
"$(json_string "$controller_ready")" \
|
||||
"$(json_string "$repository_condition")" \
|
||||
"$hooks" "$pipelines" "$tasks" "$argo"
|
||||
"$hooks" "$pipelines" "$tasks" "$artifact" "$argo"
|
||||
}
|
||||
|
||||
webhook_test_action() {
|
||||
|
||||
Reference in New Issue
Block a user