Skip to content

Instantly share code, notes, and snippets.

@eyalzek
Created March 10, 2025 13:22
Show Gist options
  • Save eyalzek/52549f0c339652a9ac6b82efd85f595e to your computer and use it in GitHub Desktop.
Save eyalzek/52549f0c339652a9ac6b82efd85f595e to your computer and use it in GitHub Desktop.
Bash script to copy all maven packages from a remote Artifact Registry repo to a standard Artifact Registry repo.
#!/usr/bin/env bash
set -e
# This script download artifacts from a remote maven AR repo [1]
# then upload them to a standard maven AR repo.
# Useful for transitioning gradually from 3rd party artifact repositories into AR
# [1] https://cloud.google.com/artifact-registry/docs/repositories/remote-overview
# [2] https://cloud.google.com/artifact-registry/docs/repositories#standard-repository
TMP_DIR=$(mktemp -d)
read -p "Enter GCP project name " GCP_PROJECT
read -p "Enter Remote AR repo name " REMOTE_REPO
read -p "Enter AR repos location " LOCATION
read -p "Enter target standard AR repo name " TARGET_REPO
POM_FILE=""
JAR_FILE=""
for p in $(gcloud artifacts packages list --repository=${REMOTE_REPO} --location=${LOCATION} --format='value(name)'); do
mkdir ${TMP_DIR}/${p}
for v in $(gcloud artifacts versions list --package=${p} --repository=${REMOTE_REPO} --location=${LOCATION} --format='value(name)'); do
mkdir ${TMP_DIR}/${p}/${v}
echo "package: ${p}, version: ${v}"
for f in $(gcloud artifacts files list --package=${p} --version=${v} --repository=${REMOTE_REPO} --location=${LOCATION} --format='value(name)'); do
echo "Downloading ${f}"
gcloud artifacts files download \
--project=${GCP_PROJECT} \
--location=${LOCATION} \
--repository=${REMOTE_REPO} \
--destination=${TMP_DIR}/${p}/${v} \
${f}
done
POM_FILE=$(ls ${TMP_DIR}/${p}/${v} |grep -E '.pom$')
JAR_FILE=$(ls ${TMP_DIR}/${p}/${v} |grep -E '.jar$')
echo "Uploading to ${TARGET_REPO}, POM file: ${TMP_DIR}/${p}/${v}/${POM_FILE}, JAR file: ${TMP_DIR}/${p}/${v}/${JAR_FILE}"
mvn deploy:deploy-file \
-Durl=artifactregistry://${LOCATION}-maven.pkg.dev/${GCP_PROJECT}/${TARGET_REPO} \
-DpomFile=${TMP_DIR}/${p}/${v}/${POM_FILE} -Dfile=${TMP_DIR}/${p}/${v}/${JAR_FILE}
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment