Skip to content

Commit 5a4af9b

Browse files
author
Jonathan Mabit
committed
feat: add support to private registries
1 parent 9633089 commit 5a4af9b

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

docker-pussh

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,19 @@ 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"
332+
return 0
333+
else
334+
error "Error parsing reference $IMAGE is not a valid repository/tag: invalid reference format"
335+
fi
336+
}
324337

325338
# Function to cleanup resources
326339
# TODO: review cleanup
@@ -375,8 +388,9 @@ if is_additional_tunneling_needed; then
375388
success "Additional tunnel created: localhost:$PUSH_PORT → localhost:$LOCAL_PORT"
376389
fi
377390

391+
IMAGE_NAME_TAG=$(extract_image_name_and_tag)
378392
# Tag and push the image to unregistry through the forwarded port.
379-
REGISTRY_IMAGE="localhost:$PUSH_PORT/$IMAGE"
393+
REGISTRY_IMAGE="localhost:$PUSH_PORT/$IMAGE_NAME_TAG"
380394
docker tag "$IMAGE" "$REGISTRY_IMAGE"
381395
info "Pushing '$REGISTRY_IMAGE' to unregistry..."
382396

@@ -390,20 +404,24 @@ if ! docker push ${DOCKER_PUSH_OPTS[@]+"${DOCKER_PUSH_OPTS[@]}"} "$REGISTRY_IMAG
390404
error "Failed to push image."
391405
fi
392406

407+
REMOTE_REGISTRY_IMAGE=""
393408
# Pull image from unregistry if remote Docker doesn't uses containerd image store.
394409
# shellcheck disable=SC2029
395410
if ! ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker info -f '{{ .DriverStatus }}' | grep -q 'containerd.snapshotter'"; then
396411
info "Remote Docker doesn't use containerd image store. Pulling image from unregistry..."
397412

398-
remote_registry_image="localhost:$UNREGISTRY_PORT/$IMAGE"
399-
if ! ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker pull $remote_registry_image"; then
413+
REMOTE_REGISTRY_IMAGE="localhost:$UNREGISTRY_PORT/$IMAGE_NAME_TAG"
414+
if ! ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker pull $REMOTE_REGISTRY_IMAGE"; then
400415
error "Failed to pull image from unregistry on remote host."
401416
fi
402-
if ! ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker tag $remote_registry_image $IMAGE"; then
403-
error "Failed to retag image on remote host $remote_registry_image$IMAGE"
404-
fi
405-
ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker rmi $remote_registry_image" >/dev/null || true
417+
else
418+
REMOTE_REGISTRY_IMAGE="$REGISTRY_IMAGE"
419+
fi
420+
421+
if ! ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker tag $REMOTE_REGISTRY_IMAGE $IMAGE"; then
422+
error "Failed to retag image on remote host $REMOTE_REGISTRY_IMAGE$IMAGE"
406423
fi
424+
ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker rmi $REMOTE_REGISTRY_IMAGE" >/dev/null || true
407425

408426
info "Removing unregistry container on remote host..."
409427
# shellcheck disable=SC2029

0 commit comments

Comments
 (0)