Skip to content

Instantly share code, notes, and snippets.

@Kamrad117
Created April 14, 2017 09:45
Show Gist options
  • Save Kamrad117/3fd7f0a347501a87791f4bb24e53d452 to your computer and use it in GitHub Desktop.
Save Kamrad117/3fd7f0a347501a87791f4bb24e53d452 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Set of all jars, that script will manage
jars=( test1.jar test2.jar )
# Group id parsed to uri format com.asd => com/asd
GROUP_URI=$(echo $GROUP | tr . /)
echo Parameters:
echo REPO_ID = $REPO_ID
echo GROUP = $GROUP
echo VERSION = $VERSION
echo OLD_VERSION = $OLD_VERSION
echo DELETE_OLD_VERSION = $DELETE_OLD_VERSION
echo DELETE_DOWNLOADED_ARTIFACTS = $DELETE_DOWNLOADED_ARTIFACTS
echo USERNAME = $USERNAME
echo JENKINS_WS_URL = $JENKINS_WS_URL
echo NEXUS_URL = $NEXUS_URL
echo GROUP_URI = $GROUP_URI
# Gets name and extension from artifact name
function parse_artifact_name {
filename=$(basename "$jar")
extension="${filename##*.}"
filename="${filename%.*}"
}
# Download jars to current directory on local machine
function download_jars {
echo; echo Downloading jars from $JENKINS_WS_URL
for jar in ${jars[@]}
do
echo; echo Downloading $jar
wget -cNq $JENKINS_WS_URL/$jar
done
}
# Upload jars from current directory to nexus by specified GARV coordinates
function upload_jars {
echo; echo Uploading jars v$VERSION
for jar in ${jars[@]}
do
parse_artifact_name
echo; echo Uploading $jar
curl -F r=$REPO_ID -F hasPom=false -F e=$extension -F g=$GROUP -F a=$filename -F v=$VERSION -F p=$extension -F file=@$jar -u $USERNAME:$PASSWORD $NEXUS_URL/service/local/artifact/maven/content
done
}
# Delete jars from nexus
function delete_jars {
echo; echo Delete jars v$OLD_VERSION
for jar in ${jars[@]}
do
parse_artifact_name
echo; echo Deleting $jar v$OLD_VERSION
curl -XDELETE -u$USERNAME:$PASSWORD $NEXUS_URL/service/local/repositories/$REPO_ID/content/$GROUP_URI/$filename/$OLD_VERSION
done
}
# Delete metadata to make nexus rebuild it (required after deletion of jars)
function update_metadata {
for jar in ${jars[@]}
do
parse_artifact_name
echo; echo Updating metadata $jar
curl -XDELETE -u$USERNAME:$PASSWORD $NEXUS_URL/service/local/metadata/repositories/$REPO_ID/content/$GROUP_URI/$filename
done
}
# Download locally and upload to nexus new version
if [ -n "$VERSION" ]
then
mkdir -p $VERSION
cd $VERSION
download_jars
upload_jars
cd ..
else
echo Version to upload is null. Skipping upload ...
fi
# Delete old version
if [[ -n "$OLD_VERSION" ]] && $DELETE_OLD_VERSION
then
echo Deleting $OLD_VERSION
delete_jars
update_metadata
if [[ -d "$OLD_VERSION" ]]
then
rm -rf $OLD_VERSION
fi
else
echo Deletion of old jars is not requested. Skipping ...
fi
# Delete downloaded jars
if $DELETE_DOWNLOADED_ARTIFACTS
then
rm -rf "$VERSION"
else
echo Deletion of downloaded jars is not requested. Skipping ...
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment