@@ -87,6 +87,50 @@ wait_for_deployment() {
8787 return 0
8888}
8989
90+ # Wait for the current deployment to fully complete (all instances replaced)
91+ wait_for_deployment_complete () {
92+ local app_name=" $1 "
93+ local max_wait=900 # 15 minutes max for full deployment
94+ local wait_interval=15
95+ local waited=0
96+
97+ echo " Waiting for deployment of $app_name to complete..."
98+
99+ local app_guid=$( cf app " $app_name " --guid)
100+
101+ while [ $waited -lt $max_wait ]; do
102+ # Get the most recent deployment status
103+ local deployment_info=$( cf curl " /v3/deployments?app_guids=${app_guid} &order_by=-created_at&per_page=1" 2> /dev/null)
104+ local status=$( echo " $deployment_info " | grep -o ' "value":"[^"]*"' | head -1 | cut -d' "' -f4 || echo " " )
105+ local reason=$( echo " $deployment_info " | grep -o ' "reason":"[^"]*"' | head -1 | cut -d' "' -f4 || echo " " )
106+
107+ if [ " $status " == " FINALIZED" ]; then
108+ if [ " $reason " == " DEPLOYED" ]; then
109+ echo " ✓ Deployment completed successfully"
110+ return 0
111+ elif [ " $reason " == " CANCELED" ]; then
112+ echo " ✗ Deployment was canceled"
113+ return 1
114+ else
115+ echo " ✗ Deployment finalized with reason: $reason "
116+ return 1
117+ fi
118+ fi
119+
120+ if [ " $status " == " ACTIVE" ]; then
121+ echo " Deployment in progress (status: $status ), waiting ${wait_interval} s... (waited ${waited} s of ${max_wait} s)"
122+ else
123+ echo " Deployment status: $status , reason: $reason "
124+ fi
125+
126+ sleep $wait_interval
127+ waited=$(( waited + wait_interval))
128+ done
129+
130+ echo " Warning: Timed out waiting for deployment to complete after ${max_wait} s"
131+ return 1
132+ }
133+
90134# Run migrations as a CF task and wait for completion
91135run_migrations () {
92136 local app_name=" $1 "
@@ -184,22 +228,47 @@ cf_push_with_retry() {
184228 set +e
185229 if [ -n " $manifest_path " ]; then
186230 echo " Using manifest: $manifest_path "
187- cf push " $app_name " -f " $manifest_path " --strategy rolling -t 180
231+ cf push " $app_name " -f " $manifest_path " --strategy rolling -t 180 --no-wait
188232 else
189- cf push " $app_name " --strategy rolling -t 180
233+ cf push " $app_name " --strategy rolling -t 180 --no-wait
190234 fi
191235 exit_code=$?
192236 set -e
193237
194238 if [ $exit_code -eq 0 ]; then
195- echo " Successfully pushed $app_name "
196- release_deploy_lock " $app_name "
197- trap - EXIT # Clear the trap
198- return 0
239+ echo " Push initiated successfully, waiting for full deployment to complete..."
240+ if wait_for_deployment_complete " $app_name " ; then
241+ echo " Successfully deployed $app_name "
242+ release_deploy_lock " $app_name "
243+ trap - EXIT # Clear the trap
244+ return 0
245+ else
246+ echo " Deployment did not complete successfully"
247+ # Continue to retry logic below
248+ fi
199249 fi
200250
201251 if [ $i -lt $max_retries ]; then
202- echo " Push failed (exit code: $exit_code ), waiting ${retry_delay} s before retry..."
252+ echo " Push failed or deployment incomplete (exit code: $exit_code ), checking for active deployments..."
253+
254+ # Check if there's an active deployment that we should wait for instead of retrying
255+ local app_guid=$( cf app " $app_name " --guid 2> /dev/null || echo " " )
256+ if [ -n " $app_guid " ]; then
257+ local active_deployment=$( cf curl " /v3/deployments?app_guids=${app_guid} &status_values=ACTIVE" 2> /dev/null | grep -c ' "ACTIVE"' || echo " 0" )
258+
259+ if [ " $active_deployment " -gt 0 ]; then
260+ echo " Active deployment detected, waiting for it to complete instead of retrying..."
261+ if wait_for_deployment_complete " $app_name " ; then
262+ echo " Existing deployment completed successfully"
263+ release_deploy_lock " $app_name "
264+ trap - EXIT
265+ return 0
266+ fi
267+ echo " Existing deployment did not complete successfully, will retry..."
268+ fi
269+ fi
270+
271+ echo " Waiting ${retry_delay} s before retry..."
203272 sleep $retry_delay
204273 # Re-check for in-progress deployments before retrying
205274 wait_for_deployment " $app_name "
0 commit comments