forked from testcontainers/dind-drone-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.sh
105 lines (83 loc) · 3.13 KB
/
command.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/bash
set -euo pipefail # Abort on error, strict variable interpolation, fail if piped command fails
run_hook_scripts() {
for HOOK_SCRIPT in /dind-drone-plugin/hooks/$1/*; do
if [[ -x $HOOK_SCRIPT ]]; then
echo "📄 Running $1 hook script $HOOK_SCRIPT"
$HOOK_SCRIPT || exit 1
fi
done
}
if [[ "${PLUGIN_CMD:-}" == "" ]]; then
echo "One or more cmd arguments must be provided"
exit 1
fi
# If multiple cmd lines have been provided, chain them into something which we can execute with sh
# Note that Drone provides these lines in comma separated form without escaping, which means commas in commands are known to break
export PLUGIN_CMD=${PLUGIN_CMD//,/ && }
# Wrap command scriptlet in an invocation of sh
export PLUGIN_CMD="sh -c '${PLUGIN_CMD}'"
run_hook_scripts pre_daemon_start
echo "📦 Starting dind-drone-plugin"
echo "🐳 Starting docker-in-docker daemon"
/usr/local/bin/dockerd-entrypoint.sh dockerd \
--data-root /drone/docker \
-s ${PLUGIN_STORAGE_DRIVER:-overlay2} \
--log-level error \
-H tcp://0.0.0.0:2375 \
-H unix:///var/run/docker.sock &
for i in $(seq 1 30); do
echo "⏳ Pinging docker daemon ($i/30)"
docker ps &> /dev/null && break || true
sleep 1
done
docker ps &> /dev/null || exit 1
echo "✅ Docker-in-Docker is running..."
run_hook_scripts post_daemon_start
echo " Available images before build:"
docker image ls 2>&1 | sed 's/^/ /g'
cd ${CI_WORKSPACE}
# Ensure that secrets (passed through as env vars) are available. Iterate and purposefully omit newlines.
for k in $(compgen -e); do
touch ${PWD}/outer_env_vars.env
case "$k" in
# avoid overwriting container's variables
# Note that the env vars to blacklist may be found using
# `docker run -it --entrypoint env quay.io/testcontainers/dind-drone-plugin`
DIND_COMMIT|DOCKER_CHANNEL|DOCKER_TLS_CERTDIR|DOCKER_VERSION|HOME|HOSTNAME|PATH|PWD|SHLVL)
;;
*)
echo $k=${!k} >> ${PWD}/outer_env_vars.env
;;
esac
done
# Determine IP address at which dockerd and spawned containers can be reached
DOCKER_IP=$(ip route | awk '/docker0/ { print $7 }')
echo "DOCKER_HOST=tcp://${DOCKER_IP}:2375" >> ${PWD}/outer_env_vars.env
echo "ℹ️ Docker daemon will be available in the build container:"
echo " at /var/run/docker.sock"
echo " at tcp://${DOCKER_IP}:2375 (no TLS)"
echo "ℹ️ DOCKER_HOST will be set to tcp://${DOCKER_IP}:2375"
echo "ℹ️ Containers spawned by the build container will be accessible at ${DOCKER_IP} (do not hardcode this value)"
echo -e "\n\n"
run_hook_scripts pre_run
MSG="🚀 About to run command: ${PLUGIN_CMD} on image ${PLUGIN_BUILD_IMAGE} inside Docker-in-Docker"
echo -e $MSG
echo -n " $MSG" | sed 's/./=/g'
echo -e "\n\n"
CMD="docker run -v /var/run/docker.sock:/var/run/docker.sock \
-v ${PWD}:${PWD} -w ${PWD} --rm \
--env-file ${PWD}/outer_env_vars.env \
${EXTRA_DOCKER_OPTIONS:-} \
${PLUGIN_BUILD_IMAGE} ${PLUGIN_CMD}"
echo -n "$ "
echo $CMD
echo -e "\n\n"
set +e
eval $CMD
CMD_EXIT_CODE=$?
echo; echo
echo "🏁 Exit code: $CMD_EXIT_CODE"
run_hook_scripts post_run
rm outer_env_vars.env
exit $CMD_EXIT_CODE