I'm trying to write a script for a gitlab-ci pipeline to automate the process of pulling an image, tagging it and pushing it to my custom registry. I'm using buildah commands to do this and I keep getting errors. Can someone tell me how I need to fix this? Thanks in advance :)
script:
- |
while IFS= read -r line || [ -n "$line" ]; do
if [[ ! $line =~ ^\s*(#|$) ]]; then
elements=($line)
if [ ${#elements[@]} -eq 4 ]; then
registry="${elements[0]}/${elements[1]}"
repository="${elements[2]}"
tag="${elements[3]}"
else
registry=$(echo "$line" | awk '{print $1}')
repository=$(echo "$line" | awk '{print $2}')
tag=$(echo "$line" | awk '{print $3}')
fi
echo "registry= $registry"
echo "repository= $repository"
echo "tag= $tag"
echo "gcloud artifacts docker images list $DOCKER_REGISTRY/$repository --include-tags | grep $tag"
if gcloud artifacts docker images list $DOCKER_REGISTRY/$repository --include-tags | grep $tag; then
echo "Image $repository:$tag already exists in the repository. Skipping build and push.";
else
buildah pull "docker://$registry/$repository:$tag"
# Tag the pulled image with the target repository and tag
export IMAGE_ID=$(buildah images | grep '$repository' | grep '$tag' | awk '{print $3}')
echo "image-id= $IMAGE_ID"
buildah tag "$IMAGE_ID" "$DOCKER_REGISTRY/$repository:$tag"
# Push the image to the Docker registry using Buildah
buildah push "$DOCKER_REGISTRY/$repository:$tag"
echo "Image $repository:$tag has been pushed to the artifact registry"
fi
fi
done < images.txt