Skip to content

Commit da2720e

Browse files
committed
fix(plugin): Use correct and slightly randomized remote image name
1 parent 7b59d9f commit da2720e

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

docker-pussh

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -321,17 +321,17 @@ if [ -n "$SSH_KEY" ] && [ ! -f "$SSH_KEY" ]; then
321321
error "SSH key file not found: $SSH_KEY"
322322
fi
323323

324-
extract_image_name_and_tag() {
325-
local regex="^(.*:.*?\/)?(.*\/)?(.*)$"
326-
local image_name_tag
327-
328-
if [[ $IMAGE =~ $regex ]]
329-
then
330-
image_name_tag=${BASH_REMATCH[3]}
331-
echo "$image_name_tag"
324+
get_temp_image_name() {
325+
local image=$1
326+
local regex="^(.*:.*?\/)?(.*\/)?(.+)$"
327+
local repo_with_tag
328+
329+
if [[ $image =~ $regex ]]; then
330+
repo_with_tag=${BASH_REMATCH[3]}
331+
echo "${RANDOM}-${repo_with_tag}"
332332
return 0
333333
else
334-
error "Error parsing reference $IMAGE is not a valid repository/tag: invalid reference format"
334+
error "Error parsing reference: ${image} is not a valid image reference."
335335
fi
336336
}
337337

@@ -388,7 +388,7 @@ if is_additional_tunneling_needed; then
388388
success "Additional tunnel created: localhost:$PUSH_PORT → localhost:$LOCAL_PORT"
389389
fi
390390

391-
IMAGE_NAME_TAG=$(extract_image_name_and_tag)
391+
IMAGE_NAME_TAG=$(get_temp_image_name "${IMAGE}")
392392
# Tag and push the image to unregistry through the forwarded port.
393393
REGISTRY_IMAGE="localhost:$PUSH_PORT/$IMAGE_NAME_TAG"
394394
docker tag "$IMAGE" "$REGISTRY_IMAGE"
@@ -407,20 +407,22 @@ fi
407407
REMOTE_REGISTRY_IMAGE=""
408408
# Pull image from unregistry if remote Docker doesn't uses containerd image store.
409409
# shellcheck disable=SC2029
410-
if ! ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker info -f '{{ .DriverStatus }}' | grep -q 'containerd.snapshotter'"; then
410+
if ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker info -f '{{ .DriverStatus }}' | grep -q 'containerd.snapshotter'"; then
411+
# Remote image store uses containerd, so we can use the image directly.
412+
REMOTE_REGISTRY_IMAGE="$IMAGE_NAME_TAG"
413+
else
411414
info "Remote Docker doesn't use containerd image store. Pulling image from unregistry..."
412-
413415
REMOTE_REGISTRY_IMAGE="localhost:$UNREGISTRY_PORT/$IMAGE_NAME_TAG"
414416
if ! ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker pull $REMOTE_REGISTRY_IMAGE"; then
415417
error "Failed to pull image from unregistry on remote host."
416418
fi
417-
else
418-
REMOTE_REGISTRY_IMAGE="$REGISTRY_IMAGE"
419419
fi
420420

421+
# shellcheck disable=SC2029
421422
if ! ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker tag $REMOTE_REGISTRY_IMAGE $IMAGE"; then
422423
error "Failed to retag image on remote host $REMOTE_REGISTRY_IMAGE$IMAGE"
423424
fi
425+
# shellcheck disable=SC2029
424426
ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker rmi $REMOTE_REGISTRY_IMAGE" >/dev/null || true
425427

426428
info "Removing unregistry container on remote host..."

0 commit comments

Comments
 (0)