I got empty String in variable IMAGE_TAG
when trying to extract a substring :R8A144
from string:
Loaded image: rcsmw-ee:R8A144
by grep -oP
in Jenkins execute shell:
Here is the code:
ssh -o "StrictHostKeyChecking=no" -o UserKnownHostsFile=/dev/null eccd@${DIRECTOR_IP_NUM} '
LOADED_IMAGE=$(sudo su root -c "docker load -i rcsmw-ee-5940688.4.tar")
IMAGE_TAG=$(echo $LOADED_IMAGE | grep -oP '\(:[A-ZA]\)\w+')
echo $IMAGE_TAG
'
here is the output:
bash: command substitution: line 5: syntax error near unexpected token `('
bash: command substitution: line 5: `echo $LOADED_IMAGE | grep -oP (:[A-ZA])w+)'
Error parsing reference: "rcsmw-ee:" is not a valid repository/tag: invalid reference format
You have a whole set of commands inside single quotes, so you cannot use single quotes around the grep
pattern.
Also, the "$LOADED_IMAGE"
is also better used in double quotes since it may cause trouble if it contains whitespaces.
Besides, the A
after A-Z
is redundant, as is the capturing group, you may remove the parentheses in the pattern and use
IMAGE_TAG=$(echo "$LOADED_IMAGE" | grep -oP -m 1 ":[A-Z]\w*")
Or, using an equivalent POSIX BRE regex:
IMAGE_TAG=$(echo "$LOADED_IMAGE" | grep -o -m 1 ":[[:upper:]][[:alnum:]_]*")
Note -m 1
with grep
will extract the first match only, which seems to be what you are after here.