Skip to content

Commit 8791c86

Browse files
owineclaude
andcommitted
fix: correct argument parsing in deploy-stacks.sh remote script
Fixes incorrect TARGET_REF extraction in SSH heredoc that was causing the script to use a stack name instead of the commit SHA. Previous buggy logic tried to detect if COMPOSE_ARGS was present by checking for hyphens/equals, but when COMPOSE_ARGS is an empty string it's still passed as an argument, causing incorrect parsing: - STACKS included the TARGET_REF SHA - TARGET_REF was set to the last stack name New logic is simpler and more reliable: - COMPOSE_ARGS is ALWAYS the last argument (could be empty "") - TARGET_REF is ALWAYS the second-to-last argument - STACKS are everything before the last 2 arguments This fixes output like: 'Updating repository to portainer...' Instead of: 'Updating repository to d14597a8b89b5aa58781cfb68036132fcbb63a99...' 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 1474507 commit 8791c86

File tree

1 file changed

+14
-32
lines changed

1 file changed

+14
-32
lines changed

scripts/deployment/deploy-stacks.sh

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -107,44 +107,26 @@ ssh_retry 3 10 "ssh -o \"StrictHostKeyChecking no\" $SSH_USER@$SSH_HOST env OP_S
107107
export COMPOSE_PARALLEL_LIMIT=8
108108
109109
# Get arguments passed to script (excluding sensitive OP_TOKEN)
110-
# Arguments: stack1 stack2 stack3 ... TARGET_REF [COMPOSE_ARGS]
111-
# COMPOSE_ARGS might be empty, so we need to handle variable arg count
110+
# Arguments: stack1 [stack2 ...] TARGET_REF COMPOSE_ARGS
111+
# COMPOSE_ARGS is always passed (could be empty string "")
112112
113113
TOTAL_ARGS=$#
114114
115-
# Extract stacks, target-ref, and optional compose-args
116-
# The last 1-2 args are: TARGET_REF [COMPOSE_ARGS]
117-
# TARGET_REF is a commit SHA (40 hex chars)
118-
# COMPOSE_ARGS is optional and could be empty
119-
120-
# Get the second-to-last argument as TARGET_REF
121-
if [ $TOTAL_ARGS -ge 2 ]; then
122-
TARGET_REF="${@:$((TOTAL_ARGS-1)):1}"
123-
124-
# If there's a third-from-last argument, it could be COMPOSE_ARGS
125-
if [ $TOTAL_ARGS -ge 3 ]; then
126-
LAST_ARG="${!TOTAL_ARGS}"
127-
# Check if last arg looks like compose args (contains hyphens or equals)
128-
if [[ "$LAST_ARG" =~ ^- ]] || [[ "$LAST_ARG" =~ = ]]; then
129-
COMPOSE_ARGS="$LAST_ARG"
130-
# Stacks are all args except last 2
131-
STACKS="${@:1:$((TOTAL_ARGS-2))}"
132-
else
133-
# Last arg is not compose args, so it must be a stack name
134-
COMPOSE_ARGS=""
135-
# Stacks are all args except last 1 (TARGET_REF)
136-
STACKS="${@:1:$((TOTAL_ARGS-1))}"
137-
fi
138-
else
139-
# Only 2 args total, so stacks is first arg, TARGET_REF is second
140-
COMPOSE_ARGS=""
141-
STACKS="${@:1:$((TOTAL_ARGS-1))}"
142-
fi
143-
else
144-
echo "❌ Insufficient arguments provided"
115+
# Validate minimum arguments (at least 1 stack + TARGET_REF + COMPOSE_ARGS)
116+
if [ $TOTAL_ARGS -lt 3 ]; then
117+
echo "❌ Insufficient arguments: expected at least 3 (stacks, target-ref, compose-args), got $TOTAL_ARGS"
145118
exit 1
146119
fi
147120
121+
# Last argument is always COMPOSE_ARGS (could be empty)
122+
COMPOSE_ARGS="${!TOTAL_ARGS}"
123+
124+
# Second-to-last argument is always TARGET_REF
125+
TARGET_REF="${@:$((TOTAL_ARGS-1)):1}"
126+
127+
# Everything before the last 2 arguments are stack names
128+
STACKS="${@:1:$((TOTAL_ARGS-2))}"
129+
148130
149131
# OP_SERVICE_ACCOUNT_TOKEN and timeouts are passed via 'env' command on remote side
150132
# They are already in the environment, no need to export again

0 commit comments

Comments
 (0)