-
Notifications
You must be signed in to change notification settings - Fork 45
/
docker-cargo.sh
executable file
·87 lines (76 loc) · 2.33 KB
/
docker-cargo.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash -x
REPO_ROOT=$(readlink -f $(dirname $(readlink -f $0)))
CODE_ROOT=${CODE_ROOT:-${REPO_ROOT}}
OUTPUT_DIR=docker-cargo/
CARGO_HOME=${CARGO_HOME:-$HOME/.cargo}
DOCKER_BUILDER_IMAGE=${DOCKER_BUILDER_IMAGE:-mangatasolutions/node-builder:multi-nightly-2023-05-22}
DOCKER_USER="$(id -u):$(id -g)"
DOCKER_JOB_NAME=cargo-wrapper
if [ -n "${DISABLE_TTY}" ]; then
ALLOCATE_TTY_OR_NOT="-i"
else
ALLOCATE_TTY_OR_NOT="-it"
fi
CARGO_COMMAND=$1
CARGO_ARGS=${@:2}
if [ "$CARGO_COMMAND" == "kill" ]; then
docker kill ${DOCKER_JOB_NAME}
exit 0
fi
if ! which docker >/dev/null; then
echo "docker not installed" >&2
exit 1
fi
if docker inspect ${DOCKER_BUILDER_IMAGE} >/dev/null; then
echo "building using docker image ${DOCKER_BUILDER_IMAGE}"
else
echo "docker image ${DOCKER_BUILDER_IMAGE} not found - pulling" >&2
docker pull ${DOCKER_BUILDER_IMAGE}
fi
if [ -n "${REUSE_LOCAL_CACHE}" ]; then
if [ -e ${CARGO_HOME} ]; then
CARGO_CACHE_GIT=${CARGO_HOME}/git
CARGO_CACHE_REGISTRY=${CARGO_HOME}/registry
if [ ! -e ${CARGO_CACHE_GIT} ]; then
mkdir -p ${CARGO_CACHE_GIT}
fi
if [ ! -e ${CARGO_CACHE_REGISTRY} ]; then
mkdir -p ${CARGO_CACHE_REGISTRY}
fi
else
echo "CARGO_HOME not set" >&2
exit 1
fi
if ! [ -e ${SCCACHE_DIR} ]; then
echo "SCCACHE_DIR not set" >&2
exit 1
fi
else
CARGO_CACHE_GIT=${REPO_ROOT}/${OUTPUT_DIR}/cache/cargo/git
CARGO_CACHE_REGISTRY=${REPO_ROOT}/${OUTPUT_DIR}/cache/cargo/registry
SCCACHE_DIR=${REPO_ROOT}/${OUTPUT_DIR}/cache/sccache
if [ ! -e ${CARGO_CACHE_REGISTRY} ]; then
mkdir -p ${CARGO_CACHE_REGISTRY}
fi
if [ ! -e ${CARGO_CACHE_GIT} ]; then
mkdir -p ${CARGO_CACHE_GIT}
fi
if [ ! -e ${SCCACHE_DIR} ]; then
mkdir -p ${SCCACHE_DIR}
fi
fi
if [ -n "${DISABLE_CARGO_CACHE}" ]; then
DOCKER_MOUNT_CACHE_VOLUMES=""
else
DOCKER_MOUNT_CACHE_VOLUMES="-v ${CARGO_CACHE_GIT}:/usr/local/cargo/git -v ${CARGO_CACHE_REGISTRY}:/usr/local/cargo/registry -v ${SCCACHE_DIR}:/.cache/sccache"
fi
docker run \
--rm \
--name=${DOCKER_JOB_NAME} \
--user $DOCKER_USER \
-v ${CODE_ROOT}:/code \
${DOCKER_MOUNT_CACHE_VOLUMES} \
${DOCKER_RUN_EXTRA_ARGS} \
-e CARGO_TARGET_DIR="/code/${OUTPUT_DIR}" \
${ALLOCATE_TTY_OR_NOT} ${DOCKER_BUILDER_IMAGE} \
/bin/bash -c "cargo ${CARGO_COMMAND} --manifest-path=/code/Cargo.toml ${CARGO_ARGS} && sccache --show-stats"