forked from mesosphere-backup/dcos-gitlab-runner-service
/
register_and_run.sh
executable file
·116 lines (90 loc) · 3.39 KB
/
register_and_run.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
106
107
108
109
110
111
112
113
114
115
116
#!/bin/sh
set -eu
# Ensure CI_SERVER_URL (custom defined variable). We need this to register runner
if [ -z ${CI_SERVER_URL+x} ]; then
echo "==> Need to set CI_SERVER_URL to the URL of GitLab"
exit 1
fi
# Ensure REGISTRATION_TOKEN
if [ -z ${REGISTRATION_TOKEN+x} ]; then
echo "==> Need to set REGISTRATION_TOKEN. You can get this token in GitLab -> Admin Area -> Overview -> Runners"
exit 1
fi
# Ensure RUNNER_EXECUTOR
if [ -z ${RUNNER_EXECUTOR+x} ]; then
echo "==> Need to set RUNNER_EXECUTOR. Please choose a valid executor, like 'shell' or 'docker' etc."
exit 1
fi
# Check for RUNNER_CONCURRENT_BUILDS variable (custom defined variable)
if [ -z ${RUNNER_CONCURRENT_BUILDS+x} ]; then
echo "==> Concurrency is set to 1"
else
sed -i -e "s|concurrent = 1|concurrent = ${RUNNER_CONCURRENT_BUILDS}|g" /etc/gitlab-runner/config.toml
echo "==> Concurrency is set to ${RUNNER_CONCURRENT_BUILDS}"
fi
# Include the original entrypoint contents
# Set data directory
DATA_DIR="/etc/gitlab-runner"
# Set config file
CONFIG_FILE=${CONFIG_FILE:-$DATA_DIR/config.toml}
# Set custom certificate authority paths
CA_CERTIFICATES_PATH=${CA_CERTIFICATES_PATH:-$DATA_DIR/certs/ca.crt}
LOCAL_CA_PATH="/usr/local/share/ca-certificates/ca.crt"
# Create update_ca function
update_ca() {
echo "==> Updating CA certificates..."
cp "${CA_CERTIFICATES_PATH}" "${LOCAL_CA_PATH}"
update-ca-certificates --fresh > /dev/null
}
# Compare the custom CA path to the current CA path
if [ -f "${CA_CERTIFICATES_PATH}" ]; then
# Update the CA if the custom CA is different than the current
cmp --silent "${CA_CERTIFICATES_PATH}" "${LOCAL_CA_PATH}" || update_ca
fi
# Derive the RUNNER_NAME from the MESOS_TASK_ID
export RUNNER_NAME=${MESOS_TASK_ID}
# Enable non-interactive registration the the main GitLab instance
export REGISTER_NON_INTERACTIVE=true
# Set the RUNNER_BUILDS_DIR
export RUNNER_BUILDS_DIR=${MESOS_SANDBOX}/builds
# Set the RUNNER_CACHE_DIR
export RUNNER_CACHE_DIR=${MESOS_SANDBOX}/cache
# Set the RUNNER_WORK_DIR
export RUNNER_WORK_DIR=${MESOS_SANDBOX}/work
# Create directories
mkdir -p $RUNNER_BUILDS_DIR $RUNNER_CACHE_DIR $RUNNER_WORK_DIR
# Print the environment for debugging purposes
echo "==> Printing the environment"
env
# Launch Docker daemon
# taken from https://github.com/mesosphere/jenkins-dind-agent/blob/master/wrapper.sh
# Check for DOCKER_EXTRA_OPTS. If not present set to empty value
if [ -z ${DOCKER_EXTRA_OPTS+x} ]; then
echo "==> Not using DOCKER_EXTRA_OPTS"
DOCKER_EXTRA_OPTS=
else
echo "==> Using DOCKER_EXTRA_OPTS"
echo ${DOCKER_EXTRA_OPTS}
fi
echo "==> Launching the Docker daemon..."
dind docker daemon --host=unix:///var/run/docker.sock --storage-driver=overlay $DOCKER_EXTRA_OPTS &
# Wait for the Docker daemon to start
while(! docker info > /dev/null 2>&1); do
echo "==> Waiting for the Docker daemon to come online..."
sleep 1
done
echo "==> Docker Daemon is up and running!"
# Termination function
_getTerminationSignal() {
echo "Caught SIGTERM signal! Deleting GitLab Runner!"
# Unregister (by name). See https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/tree/master/docs/commands#by-name
gitlab-runner unregister --name ${MESOS_TASK_ID}
# Exit with error code 0
exit 0
}
# Trap SIGTERM
trap _getTerminationSignal TERM
# Register the runner
gitlab-runner register
# Start the runner
gitlab-runner run --working-directory=${RUNNER_WORK_DIR}