-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-valid-pr-flow.sh
More file actions
executable file
·125 lines (112 loc) · 5.04 KB
/
run-valid-pr-flow.sh
File metadata and controls
executable file
·125 lines (112 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env bash
# Run the full valid PR flow: create PR → run PR pipeline → poll until success → merge PR.
# Merging triggers the merge pipeline via webhook (if configured), or you can run merge pipeline manually after.
#
# Prerequisites: .env with GITHUB_TOKEN; app repo cloned; cluster with Tekton, registry, git-ssh-key.
# For Kind: unset STORAGE_CLASS or set --storage-class "" so PVCs bind.
#
# Usage:
# ./scripts/run-valid-pr-flow.sh --app demo-fe
# ./scripts/run-valid-pr-flow.sh --app demo-fe --stack stack-one.yaml [--storage-class ""]
#
# Options:
# --app App name (e.g. demo-fe). Required.
# --stack Stack file (default: stack-one.yaml)
# --storage-class Passed to generate-run (default: "" for Kind; set for AWS)
# --poll-interval Seconds between status checks (default: 30)
# --timeout Max seconds to wait for PR pipeline (default: 900)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/common.sh"
TEKTON_DAG_ROOT="$REPO_ROOT"
need kubectl
need yq
APP=""
STACK="stack-one.yaml"
STORAGE_CLASS=""
POLL_INTERVAL="${POLL_INTERVAL:-30}"
TIMEOUT="${TIMEOUT:-900}"
while [[ $# -gt 0 ]]; do
case "$1" in
--app) APP="$2"; shift 2 ;;
--stack) STACK="$2"; shift 2 ;;
--storage-class) STORAGE_CLASS="$2"; shift 2 ;;
--poll-interval) POLL_INTERVAL="$2"; shift 2 ;;
--timeout) TIMEOUT="$2"; shift 2 ;;
*) echo "Unknown option: $1" >&2; exit 1 ;;
esac
done
[[ -n "$APP" ]] || { echo "ERROR: --app is required (e.g. demo-fe)" >&2; exit 1; }
[[ -f "$STACKS_DIR/$STACK" ]] || { echo "ERROR: Stack not found: $STACKS_DIR/$STACK" >&2; exit 1; }
# Load .env for GITHUB_TOKEN
if [[ -f "$TEKTON_DAG_ROOT/.env" ]]; then
set -a
source "$TEKTON_DAG_ROOT/.env"
set +a
fi
echo "=============================================="
echo " Valid PR flow: create PR → run pipeline → poll → merge"
echo " App: $APP Stack: $STACK"
echo "=============================================="
# Step 1: Create PR in app repo
echo ""
echo "[1/4] Creating test PR in app repo..."
CREATE_OUT=$(cd "$TEKTON_DAG_ROOT" && set -a && source .env 2>/dev/null; set +a; unset STORAGE_CLASS; "$SCRIPT_DIR/create-test-pr.sh" --app "$APP" --stack "$STACK" 2>&1) || true
PR_NUMBER=$(echo "$CREATE_OUT" | sed -n 's/^PR_NUMBER=//p' | tr -d '\r')
BRANCH_NAME=$(echo "$CREATE_OUT" | sed -n 's/^BRANCH_NAME=//p' | tr -d '\r')
APP_FROM_OUT=$(echo "$CREATE_OUT" | sed -n 's/^APP=//p' | tr -d '\r')
[[ -n "$APP_FROM_OUT" ]] && APP="$APP_FROM_OUT"
if [[ -z "$PR_NUMBER" || "$PR_NUMBER" == " " ]]; then
echo "WARNING: No PR number from create-test-pr (PR may not have been created). Output:"
echo "$CREATE_OUT"
echo "Create the PR manually on GitHub and re-run with PR number, or fix token/repo and try again."
exit 1
fi
if [[ -z "$BRANCH_NAME" ]]; then
echo "ERROR: Could not get BRANCH_NAME from create-test-pr output." >&2
exit 1
fi
echo " APP=$APP PR_NUMBER=$PR_NUMBER BRANCH_NAME=$BRANCH_NAME"
# Step 2: Start PR pipeline (use empty storage class for Kind so PVCs bind)
echo ""
echo "[2/4] Starting PR pipeline..."
PIPE_OUT=$(cd "$TEKTON_DAG_ROOT" && set -a && source .env 2>/dev/null; set +a; unset STORAGE_CLASS 2>/dev/null; "$SCRIPT_DIR/generate-run.sh" --mode pr --stack "$STACK" --app "$APP" --pr "$PR_NUMBER" --app-revision "$APP:$BRANCH_NAME" --storage-class "" --apply 2>&1)
RUN_NAME=$(echo "$PIPE_OUT" | grep -oE 'stack-pr-[0-9]+-[a-z0-9]+' | head -1)
if [[ -z "$RUN_NAME" ]]; then
echo "ERROR: Could not get PipelineRun name from generate-run output." >&2
echo "$PIPE_OUT"
exit 1
fi
echo " PipelineRun: $RUN_NAME"
# Step 3: Poll until Succeeded or Failed
echo ""
echo "[3/4] Polling for pipeline success (interval ${POLL_INTERVAL}s, timeout ${TIMEOUT}s)..."
ELAPSED=0
while [[ $ELAPSED -lt $TIMEOUT ]]; do
STATUS=$(kubectl get pipelinerun "$RUN_NAME" -n "$NAMESPACE" -o jsonpath='{.status.conditions[0].status}' 2>/dev/null || echo "Unknown")
REASON=$(kubectl get pipelinerun "$RUN_NAME" -n "$NAMESPACE" -o jsonpath='{.status.conditions[0].reason}' 2>/dev/null || echo "")
if [[ "$STATUS" == "True" ]]; then
echo " Pipeline succeeded."
break
fi
if [[ "$STATUS" == "False" ]]; then
echo " Pipeline failed: $REASON" >&2
kubectl get pipelinerun "$RUN_NAME" -n "$NAMESPACE" -o yaml | tail -50 >&2
exit 1
fi
echo " $(date +%H:%M:%S) status=$STATUS (${ELAPSED}s elapsed)"
sleep "$POLL_INTERVAL"
ELAPSED=$((ELAPSED + POLL_INTERVAL))
done
if [[ $ELAPSED -ge $TIMEOUT ]]; then
echo " Timeout waiting for pipeline." >&2
exit 1
fi
# Step 4: Merge the PR (webhook will trigger merge pipeline if configured)
echo ""
echo "[4/4] Merging PR #$PR_NUMBER (app: $APP)..."
cd "$TEKTON_DAG_ROOT"
"$SCRIPT_DIR/merge-pr.sh" "$PR_NUMBER" --app "$APP"
echo ""
echo "Done. If the webhook is configured, the merge pipeline was triggered automatically."
echo "Otherwise run: ./scripts/generate-run.sh --mode merge --stack $STACK --app $APP --apply"
echo "Merge pipeline runs: kubectl get pipelinerun -n $NAMESPACE -l tekton.dev/pipeline=stack-merge-release"