Created
April 19, 2023 09:05
-
-
Save yoranvanoirschot/0b1ae890854e75afdf90e309cd86c4b6 to your computer and use it in GitHub Desktop.
Migrate a Nexus repository to GitHub packages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sourceServer=https://... | |
sourceRepo= | |
sourceUser= | |
sourcePassword= | |
targetRepositoryUrl=https://maven.pkg.github.com/[owner]/[repository] | |
targetRepositoryId= | |
filters=() # Grab only the packages specificed in the filter or leave the variable blank to grab everything | |
logfile=$sourceRepo-backup.log | |
outputFile=$sourceRepo-artifacts.txt | |
# ======== GET DOWNLOAD URLs ========= | |
url=$sourceServer"/service/rest/v1/search?repository="$sourceRepo | |
contToken="initial" | |
while [ ! -z "$contToken" ]; do | |
if [ "$contToken" != "initial" ]; then | |
url=$sourceServer"/service/rest/v1/search?continuationToken="$contToken"&repository="$sourceRepo | |
fi | |
echo "Processing repository token: $contToken, url: $url" | tee -a $logfile | |
response=`curl -ksSL -u "$sourceUser:$sourcePassword" -X GET --header 'Accept: application/json' "$url"` | |
artifacts=($(echo $response | jq -r '.items[] | .assets[] | .downloadUrl' )) | |
printf "%s\n" "${artifacts[@]}" > artifacts.temp | |
if [ ! -z "$filters" ]; then | |
for filter in "${filters[@]}"; do | |
cat artifacts.temp | grep "$filter" >> $outputFile | |
done | |
else | |
cat artifacts.temp >> $outputFile | |
fi | |
contToken=$(echo $response | jq -r '.continuationToken' ) | |
# contToken='' # Stop after first page for test | |
done | |
# ======== DOWNLOAD EVERYTHING ========= | |
echo Downloading artifacts... | |
urls=$(cat $outputFile) | |
for url in $urls; do | |
# Sanitize the URL | |
url="$(echo -e "${url}" | sed -e 's/^[[:space:]]*//')" | |
if [[ -f $sourceRepo/${url#https://*/*/*/*} ]]; then | |
echo "Skipping, already downloaded artifact: $url" | |
else | |
# Create the parent directory | |
path=${url#https://*/*/*/} | |
dir=$sourceRepo"/"${path%/*} | |
curFolder=$(pwd) | |
mkdir -p $dir | |
cd $dir | |
# Download the artifact | |
url="$(echo -e "${url}" | sed -e 's/\s/%20/g')" | |
curl -vks -u "$sourceUser:$sourcePassword" -D response.header -X GET "$url" -O >> /dev/null 2>&1 | |
responseCode=`cat response.header | sed -n '1p' | cut -d' ' -f2` | |
if [ "$responseCode" == "200" ]; then | |
echo Successfully downloaded artifact: $url | |
else | |
echo ERROR: Failed to download artifact: $url with error code: $responseCode | |
fi | |
rm response.header > /dev/null 2>&1 | |
# Return to the root directory | |
cd $curFolder | |
fi | |
done | |
# ======== UPLOAD EVERYTHING ========= | |
curFolder=$(pwd) | |
directories=$(find $curFolder/$sourceRepo -type d) | |
for dir in $directories; do | |
# Head into the package directory | |
cd $dir | |
files=$(find . -maxdepth 1 -type f) | |
if [[ -z $files ]]; then | |
echo "Skipping directory: $dir, does not contain files" | |
else | |
if [[ -f is-github-deployed ]]; then | |
echo "Skipping artifact: $dir, already deployed" | |
else | |
fqn=${dir#$curFolder/$sourceRepo/*} | |
groupId=$(echo $fqn | sed -r 's/(.+)\/.+\/.+/\1/') | |
groupId=${groupId////.} | |
artifactId=$(echo $fqn | sed -r 's/.+\/(.+)\/.+/\1/') | |
version=$(echo $fqn | sed -r 's/.+\/.+\/(.+)/\1/') | |
echo "Deploying artifact: $groupId:$artifactId:$version" | |
pomFile=$(find . -maxdepth 1 -type f -a -name "*.pom") | |
jarFile=$(find . -maxdepth 1 -type f -a -name "*.jar" -not -name "*sources.jar") | |
maven_command="deploy:deploy-file -Durl=$targetRepositoryUrl -DrepositoryId=$targetRepositoryId -DgroupId=$groupId -DartifactId=$artifactId -Dversion=$version" | |
if [[ -f $jarFile ]]; then | |
sourcesJar=$(find . -maxdepth 1 -type f -a -name "*sources.jar") | |
maven_command="$maven_command -Dfile=$jarFile -DpomFile=$pomFile" | |
if [[ -f $sourcesJar ]]; then | |
maven_command="$maven_command -Dsources=$sourcesJar" | |
fi | |
elif [[ -f $pomFile ]]; then | |
maven_command="$maven_command -Dfile=$pomFile" | |
fi | |
# Execute maven and add file to indicate that this artifact was deployed | |
mvn $maven_command | |
if [ $? -eq 0 ]; then | |
touch is-github-deployed | |
fi | |
fi | |
fi | |
# Revert to the current directory | |
cd $curFolder | |
done |
Hello @yoranvanoirschot, do you mind clarifying what the $targetRepositoryId is and where I can get this value, Please?
Hi @Folami95 , this is the value of settings.servers.server.id
for the target Github credentials from your ~/.m2/settings.xml
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Credit for first part of this Gist goes to: https://stackoverflow.com/questions/70610642/download-entire-repository-from-nexus-3-37-1 .
You can run this script multiple times. Artifacts which are already migrated will be skipped.