generated from wayfair-incubator/oss-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommand
executable file
·166 lines (135 loc) · 4.72 KB
/
command
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/bin/bash
set -euo pipefail
# retry <number-of-retries> <command>
function retry {
local retries=$1
shift
local attempts=1
local status=0
until "$@"; do
status=$?
echo "Exited with $status"
if ((retries == "0")); then
return $status
elif ((attempts == retries)); then
echo "Failed $attempts retries"
return $status
else
echo "Retrying $((retries - attempts)) more times..."
attempts=$((attempts + 1))
sleep $(((attempts - 2) * 2))
fi
done
}
PLUGIN_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/.."
rm -rf /tmp/service_account.json
PIPELINE_FILE="/tmp/service_account.json"
debug_mode="false"
if [[ ${BUILDKITE_PLUGIN_CLOUD_FUNCTIONS_DEBUG_MODE:-false} =~ (true|on|1) ]]; then
echo "--- :hammer: Enabling debug mode"
debug_mode="true"
fi
workdir="/workdir"
default_image="python:3.10.1-buster"
image="${BUILDKITE_PLUGIN_CLOUD_FUNCTIONS_CUSTOM_IMAGE:-$default_image}"
if [ -z "$BUILDKITE_PLUGIN_CLOUD_FUNCTIONS_GCP_PROJECT" ]; then
echo "ERROR: gcp project (gcp_project) key not set"
exit 1
fi
gcp_project="${BUILDKITE_PLUGIN_CLOUD_FUNCTIONS_GCP_PROJECT}"
if [ -z "${BUILDKITE_PLUGIN_CLOUD_FUNCTIONS_GCP_REGION}" ]; then
echo "ERROR: gcp region (gcp_region) not set"
exit 1
fi
gcp_region="${BUILDKITE_PLUGIN_CLOUD_FUNCTIONS_GCP_REGION}"
if [ -z "${BUILDKITE_PLUGIN_CLOUD_FUNCTIONS_CLOUD_FUNCTION_NAME}" ]; then
echo "ERROR: cloud function name (cloud_function_name) not set"
exit 1
fi
cloud_function_name="${BUILDKITE_PLUGIN_CLOUD_FUNCTIONS_CLOUD_FUNCTION_NAME}"
if [ -z "${BUILDKITE_PLUGIN_CLOUD_FUNCTIONS_CLOUD_FUNCTION_DIRECTORY}" ]; then
echo "ERROR: cloud function directory (cloud_function_directory) not set"
exit 1
fi
cloud_function_directory="${BUILDKITE_PLUGIN_CLOUD_FUNCTIONS_CLOUD_FUNCTION_DIRECTORY}"
if [ -z "${gcp_service_account}" ]; then
echo "ERROR: gcp service account (gcp_service_account) not set"
exit 1
fi
echo "$gcp_service_account" >"$PIPELINE_FILE"
if [[ ${debug_mode} == "true" ]]; then
echo "Configuration:"
echo "GCP Project: ${gcp_project}"
echo "GCP Region: ${gcp_region}"
echo "Cloud Function Name: ${cloud_function_name}"
echo "Cloud Function Directory: ${cloud_function_directory}"
echo "Docker Image: ${image}"
fi
args=("-it" "--rm" "--init" "--workdir" "${workdir}")
# Propagate all environment variables into the container
if [[ -n ${BUILDKITE_ENV_FILE:-} ]]; then
# Read in the env file and convert to --env params for docker
# This is because --env-file doesn't support newlines or quotes per https://docs.docker.com/compose/env-file/#syntax-rules
while read -r var; do
args+=(--env "${var%%=*}")
done <"$BUILDKITE_ENV_FILE"
else
echo '🚨 Not propagating environment variables to container as $BUILDKITE_ENV_FILE is not set'
fi
# Mount the buildkite-agent
if [[ -z ${BUILDKITE_AGENT_BINARY_PATH:-} ]]; then
if ! command -v buildkite-agent >/dev/null 2>&1; then
echo "+++ 🚨 Failed to find buildkite-agent in PATH to mount into container"
exit 1
else
BUILDKITE_AGENT_BINARY_PATH=$(command -v buildkite-agent)
fi
fi
args+=(
"--env" "BUILDKITE_JOB_ID"
"--env" "BUILDKITE_BUILD_ID"
"--env" "BUILDKITE_AGENT_ACCESS_TOKEN"
"--env" "gcp_project=$gcp_project"
"--env" "gcp_region=$gcp_region"
"--env" "cloud_function_name=$cloud_function_name"
"--env" "cloud_function_directory=$cloud_function_directory"
"--env" "debug_mode=$debug_mode"
"--env" "credentials=$(</tmp/service_account.json)"
"--volume" "$BUILDKITE_AGENT_BINARY_PATH:/usr/bin/buildkite-agent"
)
# Add the image in before the shell and command
args+=("${image}")
# Assemble the shell and command arguments into the docker arguments
command=()
display_command=()
shell=("/bin/sh" "-e" "-c")
args+=("${shell[@]}")
display_command+=("${shell[@]}")
command+=("pip -q install -r plugin_scripts/requirements.lock && python -m plugin_scripts.__init__")
# join command lines
command_string="$(
IFS=$'\n'
echo "${command[*]}"
)"
args+=("${command_string}")
display_command+=("'${command_string}'")
echo "--- :docker: Pulling ${image}"
if ! retry "${BUILDKITE_PLUGIN_CLOUD_FUNCTIONS_DOCKER_PULL_RETRIES:-3}" \
docker pull "${image}"; then
rv=$?
echo "--- :docker: Pull failed."
exit $rv
fi
echo '--- :docker: Logging "docker create" command'
echo "$ docker create" >&2
echo "--- :docker: Running command in ${image}"
echo "$ ${display_command[*]}" >&2
if [[ ${debug_mode} == "true" ]]; then
echo "docker create" >&2
echo "docker cp \"${PWD}/.\" \"dockerContainerID:${workdir}\"" >&2
fi
# # For copy-checkout, we have to `docker create`, then `docker cp "${PWD}:${workdir}"`, then `docker start`
DOCKERID=$(docker create "${args[@]}") # substitute any backticks
docker cp "${PWD}/." "${DOCKERID}:${workdir}"
docker cp "${PLUGIN_DIR}/plugin_scripts" "${DOCKERID}:${workdir}/plugin_scripts"
docker start -a "${DOCKERID}"