1+ #! /bin/bash
2+
3+ # RUN IN CLOUD SHELL!!!!
4+
5+ REMOTE_REGISTRY=" lhr.ocir.io" # Replace it with your own regional registry
6+ CREATE_OCIR_PUBLIC_REGISTRY=true
7+ COMPARTMENT_ID=" ocid1.compartment.oc1..." # Compartment where images will be created
8+
9+ unset -v HELM_CHART;
10+
11+ while getopts c: flag
12+ do
13+ case " ${flag} " in
14+ c) HELM_CHART=${OPTARG} ;;
15+ * ) echo ' Error in command line parsing' >&2
16+ exit 1
17+ esac
18+ done
19+
20+ if [ -z " $HELM_CHART " ]; then
21+ echo ' Missing -c (helm chart)' >&2
22+ exit 1
23+ fi
24+
25+ # Extract image names and tags
26+ images=$( helm template $HELM_CHART | grep -oE ' image:[[:space:]]*[^[:space:]]+' | sed ' s/image:[[:space:]]*//' )
27+ # Remove duplicates
28+ images=$( echo " $images " | tr ' ' ' \n' | sort -u | tr ' \n' ' ' )
29+
30+ create_repo (){
31+ # Checking if registry creation necessary
32+ repo_id=$( oci artifacts container repository list --compartment-id " $COMPARTMENT_ID " --display-name " $1 " --limit 1 --query data.items[0].id --raw-output) ;
33+ # Create repo if it does not exist!
34+ [ -z " $repo_id " ] && repo_id=$( oci artifacts container repository create --display-name " $1 " --compartment-id " $COMPARTMENT_ID " --is-public $CREATE_OCIR_PUBLIC_REGISTRY --query data.id --raw-output) ;
35+ # If repo_id is still empty, an error has occurred
36+ [ -z " $repo_id " ] && exit 1
37+ }
38+
39+ for image in $images ; do
40+
41+ # Remove quotes and double quotes
42+ image=${image// [\"\']/ }
43+
44+ echo " Processing image: $image "
45+ base_image=" ${image#*/ } "
46+ image_name=" ${base_image%%:* } "
47+ image_tag=" ${base_image#*: } "
48+
49+ REPO_NAMESPACE=$( oci artifacts container configuration get --compartment-id " $COMPARTMENT_ID " --query data.namespace --raw-output) ;
50+
51+ create_repo " $image_name "
52+
53+ TOKEN=$( oci raw-request --http-method GET --target-uri " https://${REMOTE_REGISTRY} /20180419/docker/token" | jq -r .data.token) ;
54+
55+ skopeo copy " docker://$image " " docker://$REMOTE_REGISTRY /$REPO_NAMESPACE /$image_name :$image_tag " --multi-arch system --override-os " linux" --dest-creds BEARER_TOKEN:$TOKEN
56+
57+ echo " Mirrored $image to $REMOTE_REGISTRY /$REPO_NAMESPACE /$image_name :$image_tag "
58+
59+ done
0 commit comments