forked from testcontainers/dind-drone-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
command.sh
96 lines (77 loc) · 2.9 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
#!/bin/bash
# Abort on error, strict variable interpolation, fail if piped command fails
set -euo pipefail
IMAGE_CACHE_DIR=${CI_WORKSPACE}/.dind
if [[ "${PLUGIN_COMMANDS:-}" == "" ]]; then
echo "One or more commands 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_COMMANDS=${PLUGIN_COMMANDS//,/ && }
# Wrap command scriptlet in an invocation of sh
export PLUGIN_COMMANDS="sh -c '${PLUGIN_COMMANDS}'"
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:-vfs} --log-level error &
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..."
set +e
if [[ -d $IMAGE_CACHE_DIR ]]; then
echo "💾 Importing docker images from cache ($IMAGE_CACHE_DIR)"
for IMAGE in $(ls $IMAGE_CACHE_DIR); do
cat $IMAGE_CACHE_DIR/$IMAGE | gunzip | docker image load
done
fi
set -e
if [[ "${PLUGIN_DOCKER_LOGIN_COMMAND:-}" != "" ]]; then
echo "🛠 Executing Docker login command"
sh -c "${PLUGIN_DOCKER_LOGIN_COMMAND}" 2>&1 | sed "s/^/ /g"
fi
if [[ "${PLUGIN_PREFETCH_IMAGES:-}" != "" ]]; then
echo "🚚 Prefetching images in background:"
for IMG in $(echo ${PLUGIN_PREFETCH_IMAGES} | sed "s/,/ /g"); do
echo " $IMG"
$(docker pull "$IMG" > /dev/null) &
done
fi
cd ${CI_WORKSPACE}
echo "🚚 Pulling build image: ${PLUGIN_IMAGE}"
docker pull ${PLUGIN_IMAGE} 2>&1 | sed 's/^/ /g'
# Ensure that secrets (passed through as env vars) are available. Iterate and purposefully omit newlines.
for k in $(compgen -e | grep -v PATH); do
echo $k=${!k} >> ${PWD}/outer_env_vars.env
done
echo -e "\n\n"
MSG="🚀 About to run command: ${PLUGIN_COMMANDS} on image ${PLUGIN_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 \
$(for i in $(echo ${PLUGIN_MOUNTS:-} | sed "s/,/ /g"); do echo " -v $i:$i"; done)
-v ${PWD}:${PWD} -w ${PWD} --rm \
--env-file ${PWD}/outer_env_vars.env \
${EXTRA_DOCKER_OPTIONS:-} \
${PLUGIN_IMAGE} ${PLUGIN_COMMANDS}"
echo -n "$ "
echo $CMD
echo -e "\n\n"
set +e
eval $CMD
CMD_EXIT_CODE=$?
echo; echo
echo "🏁 Exit code: $CMD_EXIT_CODE"
mkdir -p $IMAGE_CACHE_DIR
docker image prune -f
echo "💾 Exporting docker images to cache ($IMAGE_CACHE_DIR)"
for IMAGE in $(docker image ls -q); do
if [[ ! -f $IMAGE_CACHE_DIR/$IMAGE.tar.gz ]]; then
docker image save $IMAGE | gzip > $IMAGE_CACHE_DIR/$IMAGE.tar.gz
fi
done
exit $CMD_EXIT_CODE