CI: map nested Docker workspace
Signed-off-by: faumaray <faumaray@gmail.com>
This commit is contained in:
1 parent
2e8e1fc0ba
commit
523355a128
4 files changed
+69
-64
No files matched your search
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Translate a path visible inside a containerized CI job into the source path
|
||||
# understood by the Docker daemon. On a regular host this returns the input
|
||||
# unchanged.
|
||||
docker_daemon_path() {
|
||||
local path=$1
|
||||
local container_reference=${ECHOION_DOCKER_CONTAINER_ID:-}
|
||||
local mount_data=
|
||||
local mount_source mount_destination
|
||||
local best_source='' best_destination=''
|
||||
|
||||
if [[ -z $container_reference ]]; then
|
||||
container_reference=${JOB_CONTAINER_NAME:-${HOSTNAME:-}}
|
||||
fi
|
||||
|
||||
if [[ -n ${ECHOION_DOCKER_CONTAINER_ID:-} ||
|
||||
-f /.dockerenv || -f /run/.containerenv ]]; then
|
||||
if [[ -n $container_reference ]]; then
|
||||
mount_data=$(
|
||||
docker inspect \
|
||||
--format '{{range .Mounts}}{{printf "%s\t%s\n" .Source .Destination}}{{end}}' \
|
||||
"$container_reference" 2>/dev/null || true
|
||||
)
|
||||
fi
|
||||
fi
|
||||
|
||||
while IFS=$'\t' read -r mount_source mount_destination; do
|
||||
[[ -n $mount_source && -n $mount_destination ]] || continue
|
||||
case $path in
|
||||
"$mount_destination"|"$mount_destination"/*)
|
||||
if ((${#mount_destination} > ${#best_destination})); then
|
||||
best_source=$mount_source
|
||||
best_destination=$mount_destination
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
if [[ $mount_destination == / && $path == /* ]]; then
|
||||
best_source=$mount_source
|
||||
best_destination=$mount_destination
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
done <<<"$mount_data"
|
||||
|
||||
if [[ -z $best_destination ]]; then
|
||||
printf '%s\n' "$path"
|
||||
elif [[ $path == "$best_destination" ]]; then
|
||||
printf '%s\n' "$best_source"
|
||||
elif [[ $best_destination == / ]]; then
|
||||
printf '%s/%s\n' "${best_source%/}" "${path#/}"
|
||||
else
|
||||
printf '%s/%s\n' \
|
||||
"${best_source%/}" "${path#"$best_destination"/}"
|
||||
fi
|
||||
}
|
||||
+3
-57
@@ -60,6 +60,9 @@ canonical_release_version() {
|
||||
|
||||
script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)
|
||||
source_dir=$(cd -- "$script_dir/.." && pwd -P)
|
||||
# shellcheck source-path=SCRIPTDIR
|
||||
# shellcheck source=lib/docker-daemon-path.sh
|
||||
source "$script_dir/lib/docker-daemon-path.sh"
|
||||
version=
|
||||
build_dir="$source_dir/build-release"
|
||||
dist_dir="$source_dir/dist"
|
||||
@@ -205,63 +208,6 @@ container_path() {
|
||||
esac
|
||||
}
|
||||
|
||||
docker_daemon_path() {
|
||||
local path=$1
|
||||
local container_reference=${ECHOION_DOCKER_CONTAINER_ID:-}
|
||||
local mount_data=
|
||||
local mount_source mount_destination
|
||||
local best_source='' best_destination=''
|
||||
|
||||
if [[ -z $container_reference ]]; then
|
||||
container_reference=${JOB_CONTAINER_NAME:-${HOSTNAME:-}}
|
||||
fi
|
||||
|
||||
# A containerized CI job talks to the host Docker daemon through its
|
||||
# socket. Bind-mount source paths are therefore interpreted in the daemon
|
||||
# host namespace, not in this job container. Resolve the longest matching
|
||||
# mount from the current container so nested release containers see the
|
||||
# checked-out workspace instead of an empty host directory.
|
||||
if [[ -n ${ECHOION_DOCKER_CONTAINER_ID:-} ||
|
||||
-f /.dockerenv || -f /run/.containerenv ]]; then
|
||||
if [[ -n $container_reference ]]; then
|
||||
mount_data=$(
|
||||
docker inspect \
|
||||
--format '{{range .Mounts}}{{printf "%s\t%s\n" .Source .Destination}}{{end}}' \
|
||||
"$container_reference" 2>/dev/null || true
|
||||
)
|
||||
fi
|
||||
fi
|
||||
|
||||
while IFS=$'\t' read -r mount_source mount_destination; do
|
||||
[[ -n $mount_source && -n $mount_destination ]] || continue
|
||||
case $path in
|
||||
"$mount_destination"|"$mount_destination"/*)
|
||||
if ((${#mount_destination} > ${#best_destination})); then
|
||||
best_source=$mount_source
|
||||
best_destination=$mount_destination
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
if [[ $mount_destination == / && $path == /* ]]; then
|
||||
best_source=$mount_source
|
||||
best_destination=$mount_destination
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
done <<<"$mount_data"
|
||||
|
||||
if [[ -z $best_destination ]]; then
|
||||
printf '%s\n' "$path"
|
||||
elif [[ $path == "$best_destination" ]]; then
|
||||
printf '%s\n' "$best_source"
|
||||
elif [[ $best_destination == / ]]; then
|
||||
printf '%s/%s\n' "${best_source%/}" "${path#/}"
|
||||
else
|
||||
printf '%s/%s\n' \
|
||||
"${best_source%/}" "${path#"$best_destination"/}"
|
||||
fi
|
||||
}
|
||||
|
||||
if ((use_container)); then
|
||||
command -v docker >/dev/null || die "docker is required for --container"
|
||||
container_build_dir=$(container_path "$build_dir")
|
||||
|
||||
Reference in new issue
Block a user