-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredeploy.sh
More file actions
executable file
·101 lines (82 loc) · 3.65 KB
/
redeploy.sh
File metadata and controls
executable file
·101 lines (82 loc) · 3.65 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
#!/usr/bin/env bash
# =============================================================================
# redeploy.sh — Force-redeploy the agent to OpenShift with a fresh image pull
# =============================================================================
# Usage: ./redeploy.sh <project-namespace> [image-tag]
#
# Prerequisites:
# - oc CLI installed and logged in
# - Container image already pushed to the registry before running this script
# - Helm chart in chart/ directory
#
# This script is useful when OpenShift has cached an old image under the same
# tag (e.g. :latest). It sets imagePullPolicy=Always on the helm release and
# triggers a rollout restart so new pods pull the latest image from the registry.
# =============================================================================
set -euo pipefail
PROJECT="${1:?Usage: ./redeploy.sh <project-namespace> [image-tag]}"
IMAGE_TAG="${2:-latest}"
APP_NAME="$(basename "$(pwd)")"
CHART_DIR="chart"
# ---------------------------------------------------------------------------
# Reminder
# ---------------------------------------------------------------------------
echo "NOTE: This script does not build or push images."
echo " Ensure your image is already pushed before running:"
echo " podman push \${IMAGE_NAME}:${IMAGE_TAG} quay.io/your-org/\${IMAGE_NAME}:${IMAGE_TAG}"
echo ""
# ---------------------------------------------------------------------------
# Pre-flight checks
# ---------------------------------------------------------------------------
if ! command -v oc &>/dev/null; then
echo "Error: oc CLI not found. Install it from https://mirror.openshift.com/pub/openshift-v4/clients/ocp/"
exit 1
fi
if ! oc whoami &>/dev/null; then
echo "Error: Not logged in to OpenShift. Run 'oc login' first."
exit 1
fi
if ! command -v helm &>/dev/null; then
echo "Error: helm CLI not found. Install it from https://helm.sh/docs/intro/install/"
exit 1
fi
if [ ! -f "$CHART_DIR/Chart.yaml" ]; then
echo "Error: $CHART_DIR/Chart.yaml not found. Run this script from the agent project root."
exit 1
fi
# Ensure the namespace exists (create if missing and user has permission)
if ! oc get namespace "$PROJECT" &>/dev/null; then
echo "Namespace '$PROJECT' not found. Creating..."
oc new-project "$PROJECT" || {
echo "Error: Could not create namespace '$PROJECT'."
exit 1
}
fi
# ---------------------------------------------------------------------------
# Deploy with imagePullPolicy=Always
# ---------------------------------------------------------------------------
echo "Deploying '$APP_NAME' to namespace '$PROJECT' (image tag: $IMAGE_TAG)..."
helm upgrade --install "$APP_NAME" "$CHART_DIR" \
-n "$PROJECT" \
--set image.pullPolicy=Always \
--set image.tag="$IMAGE_TAG" \
--wait
# ---------------------------------------------------------------------------
# Force rollout restart so pods pull the fresh image
# ---------------------------------------------------------------------------
echo ""
echo "Triggering rollout restart to force fresh image pull..."
oc rollout restart deployment/"$APP_NAME" -n "$PROJECT"
echo "Waiting for rollout to complete (timeout: 120s)..."
oc rollout status deployment/"$APP_NAME" -n "$PROJECT" --timeout=120s
# ---------------------------------------------------------------------------
# Status summary
# ---------------------------------------------------------------------------
echo ""
echo "Pod status in '$PROJECT':"
oc get pods -n "$PROJECT"
echo ""
echo "Recent logs from '$APP_NAME':"
oc logs deployment/"$APP_NAME" -n "$PROJECT" --tail=20
echo ""
echo "Redeployment complete. Namespace: $PROJECT | Image tag: $IMAGE_TAG"