jenkins-pipelines/scripts/clean-build.sh

153 lines
5.0 KiB
Bash
Executable File

#!/bin/bash
#
# Copyright (c) 2022 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
set -e
source $(dirname "$0")/lib/job_utils.sh
require_job_env BUILD_HOME
require_job_env CLEAN_PACKAGES
require_job_env CLEAN_REPOMGR
require_job_env CLEAN_DOWNLOADS
require_job_env CLEAN_DOCKER
require_job_env CLEAN_ISO
require_job_env IMPORT_BUILD
declare_job_env IMPORT_BUILD_DIR
load_build_env
#VERBOSE_ARG=--verbose
VERBOSE_ARG=
clean_or_import() {
local -a exclude_toplevel_patterns
while [[ "$1" == "--exclude-toplevel" ]] ; do
exclude_toplevel_patterns+=("$2")
shift 2
done
local src_subdir="$1"
local dst_subdir="$2"
local clean_requested="$3"
local allow_merge="${4:-false}"
local src_dir="$IMPORT_BUILD_DIR/$src_subdir"
local dst_dir="$BUILD_HOME/$dst_subdir"
if $IMPORT_BUILD && [[ -n "$IMPORT_BUILD_DIR" ]] && [[ -d "$src_dir" ]] ; then
local real_src_dir
real_src_dir="$(readlink -f "$src_dir")"
local delete_arg
if ! $allow_merge ; then
delete_arg="--delete"
fi
local -a exclude_args
local exclude_msg_suffix
if [[ "${#exclude_toplevel_patterns[@]}" -gt 0 ]] ; then
local pattern
for pattern in "${exclude_toplevel_patterns[@]}" ; do
exclude_args+=("--exclude" "/$pattern")
done
exclude_msg_suffix=" except [${exclude_toplevel_patterns[*]}]"
fi
notice "importing $src_subdir from ${IMPORT_BUILD_DIR}/${exclude_msg_suffix}"
mkdir -p "$dst_dir"
safe_copy_dir $DRY_RUN_ARG $VERBOSE_ARG $delete_arg "${exclude_args[@]}" \
"$real_src_dir/" "$dst_dir/"
return
fi
if $clean_requested && [[ -d "$dst_dir" ]] ; then
local exclude_msg_suffix
local -a find_args=(
"$dst_dir" -mindepth 1 -maxdepth 1
)
if [[ "${#exclude_toplevel_patterns[@]}" -gt 0 ]] ; then
local -i i
local -a find_exclude_args
for (( i=0 ; i<"${#exclude_toplevel_patterns[@]}" ; ++i)) ; do
local pattern="${exclude_toplevel_patterns[$i]}"
if [[ $i -gt 0 ]] ; then
find_exclude_args+=("-o")
fi
find_exclude_args+=("-name" "$pattern")
done
exclude_msg_suffix=" except [${exclude_toplevel_patterns[*]}]"
find_args+=('(' '(' "${find_exclude_args[@]}" ')' '-prune' ')' '-o')
fi
find_args+=('-print')
local find_res="$(find "${find_args[@]}")" || exit 1
local -a rm_files
if [[ -n "$find_res" ]] ; then
readarray -t rm_files <<<"$find_res" || exit 1
fi
if [[ "${#rm_files[@]}" -gt 0 ]] ; then
notice "removing all files from $dst_subdir/${exclude_msg_suffix}"
safe_rm $DRY_RUN_ARG $VERBOSE_ARG "${rm_files[@]}"
fi
fi
}
if [[ -d "$BUILD_HOME/localdisk/loadbuild" ]] ; then
# If user has changed, there may be subdirectories remaining under
# the innder localdisk/loadbuild named after a different user. Delete them.
declare -a rm_dirs
readarray -t rm_dirs < <(
find "$BUILD_HOME/localdisk/loadbuild" -mindepth 1 -maxdepth 1 \
-type d \! -name "$USER"
)
# If project name has changed, there may be subdirectories named after
# the old project name(s), delete them too.
if [[ -d "$BUILD_HOME/localdisk/loadbuild/$USER" ]] ; then
readarray -O "${#rm_dirs[@]}" -t rm_dirs < <(
find "$BUILD_HOME/localdisk/loadbuild/$USER" -mindepth 1 -maxdepth 1 \
-type d \! -name "$PROJECT"
)
fi
if [[ "${#rm_dirs[@]}" -gt 0 ]] ; then
safe_rm $DRY_RUN_ARG $VERBOSE_ARG "${rm_dirs[@]}"
fi
fi
clean_or_import --exclude-toplevel 'BUILD' \
"workspace" "$WORKSPACE_ROOT_SUBDIR" $CLEAN_PACKAGES
clean_or_import "mirrors" "mirrors" $CLEAN_DOWNLOADS true
clean_or_import "aptly" "aptly" $CLEAN_REPOMGR
clean_or_import "docker" "docker" $CLEAN_DOCKER
clean_or_import "localdisk/deploy" "localdisk/deploy" $CLEAN_ISO
# these files can't be imported, always delete them
misc_rm=(
"$BUILD_HOME"/localdisk/*.log \
"$BUILD_HOME"/localdisk/log \
"$BUILD_HOME"/localdisk/channel \
"$BUILD_HOME"/localdisk/pkgbuilder \
"$BUILD_HOME"/localdisk/workdir \
"$BUILD_HOME"/localdisk/sub_workdir \
"$BUILD_HOME"/localdisk/tmp \
"$BUILD_HOME"/workspace/*.log \
"$BUILD_HOME"/workspace/std/build-images \
"$BUILD_HOME"/workspace/std/build-wheels* \
"$BUILD_HOME"/workspace/std/build-helm \
"$BUILD_HOME"/workspace/"export" \
"$BUILD_HOME"/workspace/helm-charts \
"$BUILD_HOME"/tmp \
)
rm_args=()
for path in "${misc_rm[@]}" ; do
[[ -e "$path" ]] || continue
rm_args+=("$path")
done
if [[ "${#rm_args[@]}" -gt 0 ]] ; then
notice "removing misc files"
safe_rm $DRY_RUN_ARG $VERBOSE_ARG "${rm_args[@]}"
fi