Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- set -e # exit on error
- SCRIPT_NAME="APACHE LINKER" # script name
- JENKINS_ROOT=${HUDSON_HOME:-"/var/lib/jenkins"} # set default if HUDSON_HOME is missing
- HUDSON_PUBLIC="$JENKINS_ROOT/public" # public folder
- HUDSON_BUILDS="$JENKINS_ROOT/builds" # builds folder
- HUDSON_BUILDS_JOBDIR="$HUDSON_BUILDS/$JOB_NAME" # builds jobdir folder
- HUDSON_PUBLIC_JOBDIR="$HUDSON_PUBLIC/$JOB_NAME" # public jobdir folder
- if [ -z "$JOB_NAME" ]; then
- echo "[$SCRIPT_NAME]: JOB_NAME does not exist"
- exit 1
- fi
- MODULES="../../jobs/$JOB_NAME/modules"
- TARGET="./"
- COMMIT="$(git rev-parse HEAD)"
- #BRANCHES="$(for branch in $(git branch -a | grep -v "(no branch)" | cut -c 3-); do [ "$(git rev-parse $branch)" == "$(git rev-parse HEAD)" ] && echo "$(basename $branch)"; done | uniq -u)"
- BRANCHES="$(for branch in $(git branch -a | grep -v "(no branch)" | cut -c 3-); do [ "$(git rev-parse $branch)" == "$(git rev-parse HEAD)" ] && echo "$(basename $branch)"; done | uniq)"
- BUILD_DATES=""
- send_message() {
- local message=$*
- echo -n "[$SCRIPT_NAME] "
- echo "$message"
- }
- send_message_nl() {
- echo "[$SCRIPT_NAME] ."
- }
- send_message_info() {
- local message=$*
- echo -n "[$SCRIPT_NAME] "
- echo "$message"
- }
- send_message_error() {
- local message=$*
- echo -n "[$SCRIPT_NAME]: "
- echo "$message"
- }
- send_message "------------------------------------------------------------------------"
- send_message "RUN $SCRIPT_NAME"
- send_message "------------------------------------------------------------------------"
- if [ ! -d "$HUDSON_PUBLIC_JOBDIR" ]; then
- mkdir -p "$HUDSON_PUBLIC_JOBDIR" ];
- fi
- send_message_info "Entering Jenkins public folder $HUDSON_PUBLIC_JOBDIR"
- cd "$HUDSON_PUBLIC_JOBDIR"
- send_message_info "Fetching active symlinks ..."
- for modulepath in $(ls -d $MODULES/*/); do
- buildspath="${modulepath}builds"
- builddate="$(ls $buildspath | sort -r | head -n 1)"
- buildpath="$buildspath/$builddate"
- if [ ! -d "$HUDSON_BUILDS_JOBDIR" ]; then
- mkdir -p "$HUDSON_BUILDS_JOBDIR"
- fi
- cp -arfp "$buildpath" "$HUDSON_BUILDS_JOBDIR"
- for file in $(find $buildpath -name '*.zip'); do
- filename="$(basename $file)"
- relativepath="../../builds/$JOB_NAME${file#$buildspath}"
- ln -sf "$relativepath" "$TARGET$filename"
- for branch in $BRANCHES; do
- ln -sf "$relativepath" "$TARGET$JOB_NAME-$branch-bin.zip"
- done
- done
- done
- SYMLINK_TARGETS="$(ls -l "$HUDSON_PUBLIC_JOBDIR" | awk -F "-> " '{print $2}' | cut -d"/" -f5 | uniq)"
- BUILD_DATES="$(find $HUDSON_BUILDS_JOBDIR/??*/ -maxdepth 0 -type d)"
- if [ -n "$BUILD_DATES" ]; then
- if [ ! -d "$HUDSON_BUILDS_JOBDIR" ]; then
- send_message_error "BUILD directory $HUDSON_BUILDS_JOBDIR does not exist"
- exit 1
- fi
- send_message_info "Current active build-dates for $JOB_NAME:"
- for build in $BUILD_DATES; do
- build=${build/$HUDSON_BUILDS_JOBDIR\//}
- build=${build/%\//}
- send_message_info ". $build"
- done
- send_message_nl
- send_message_info "Entering builds directory $HUDSON_BUILDS_JOBDIR"
- cd "$HUDSON_BUILDS_JOBDIR"
- send_message_info "Looking for inactive build folders ..."
- for directory in $BUILD_DATES; do
- ACTIVE_BUILD=0
- directory=${directory/$HUDSON_BUILDS_JOBDIR\//} # remove HUDSON_BUILDS_JOBDIR from string
- directory=${directory/%\//} # remove trailing slash
- for build in $SYMLINK_TARGETS; do
- if [ "$directory" == "$build" ]; then
- send_message_info "Found active build: $build skip deletion"
- ACTIVE_BUILD=1
- break
- fi
- done
- if [ "${directory:0:1}" == "/" ]; then
- send_message_error "directory starts with a slash! Possible script failure detected. Exiting ..."
- exit 1
- fi
- if [ "${#directory}" -le 4 ]; then
- send_message_error "directory has a length that is less or equal four! Possible script failure detected. Exiting ..."
- exit 1
- fi
- if [ $ACTIVE_BUILD -eq 0 ]; then
- send_message_info "Deleting unused build folder: $directory"
- rm -Rf ./"${directory}"
- fi
- done
- fi
- send_message "------------------------------------------------------------------------"
- exit 0
Add Comment
Please, Sign In to add comment