forked from buildkite/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.sh
executable file
·227 lines (183 loc) · 7.04 KB
/
bootstrap.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/bin/bash
BUILDBOX_PROMPT="\033[90m$\033[0m"
function buildbox-prompt-and-run {
echo -e "$BUILDBOX_PROMPT $1"
eval $1
}
function buildbox-run {
echo -e "$BUILDBOX_PROMPT $1"
eval $1
EVAL_EXIT_STATUS=$?
if [ $EVAL_EXIT_STATUS -ne 0 ]
then
exit $EVAL_EXIT_STATUS
fi
}
if [ "$BUILDBOX_BIN_DIR" != "" ]
then
echo "Deprecation warning: BUILDBOX_BIN_DIR has been renamed to BUILDBOX_BIN_PATH"
BUILDBOX_BIN_PATH=$BUILDBOX_BIN_DIR
fi
if [ "$BUILDBOX_DIR" != "" ]
then
echo "Deprecation warning: BUILDBOX_DIR has been renamed to BUILDBOX_PATH"
BUILDBOX_PATH=$BUILDBOX_DIR
fi
echo '--- setup environment'
# Provide a default BUILDBOX_PATH
if [ -z "$BUILDBOX_PATH" ]; then
# This will return the location of this file. We assume that the buildbox-artifact
# tool is in the same folder. You can of course customize the locations
# and edit this file.
BUILDBOX_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
fi
# If no BUILDBOX_BIN_PATH has been provided, make one up
if [ -z "$BUILDBOX_BIN_PATH" ]; then
if [ -d "$BUILDBOX_PATH/bin" ]; then
BUILDBOX_BIN_PATH="$BUILDBOX_PATH/bin"
else
BUILDBOX_BIN_PATH="$BUILDBOX_PATH"
fi
fi
# Add the $BUILDBOX_BIN to the $PATH
export PATH="$BUILDBOX_BIN_PATH:$PATH"
# Create the build directory
SANITIZED_AGENT_NAME=$(echo $BUILDBOX_AGENT_NAME | tr -d '"')
PROJECT_FOLDER_NAME="$SANITIZED_AGENT_NAME/$BUILDBOX_PROJECT_SLUG"
# Allow overiding the location that builds get run in
if [ -z "$BUILDBOX_BUILD_PATH" ]; then
BUILDBOX_PROJECT_PATH="$BUILDBOX_PATH/builds/$PROJECT_FOLDER_NAME"
else
BUILDBOX_PROJECT_PATH="$BUILDBOX_BUILD_PATH/$PROJECT_FOLDER_NAME"
fi
# Show the ENV variables if DEBUG is on
if [ "$BUILDBOX_AGENT_DEBUG" == "true" ]
then
buildbox-run "env | grep BUILDBOX"
fi
buildbox-run "mkdir -p \"$BUILDBOX_PROJECT_PATH\""
buildbox-run "cd \"$BUILDBOX_PROJECT_PATH\""
# Do we need to do a git checkout?
if [ ! -d ".git" ]
then
# If it's a first time SSH git clone it will prompt to accept the host's
# fingerprint. To avoid this add the host's key to ~/.ssh/known_hosts ahead
# of time:
# ssh-keyscan -H host.com >> ~/.ssh/known_hosts
buildbox-run "git clone "$BUILDBOX_REPO" . -qv"
fi
# Default empty branch names
if [ "$BUILDBOX_BRANCH" == "" ]
then
BUILDBOX_BRANCH="master"
fi
buildbox-run "git clean -fdq"
buildbox-run "git fetch -q"
if [ "$BUILDBOX_PULL_REQUEST" != "false" ]
then
echo "building result of pull request $BUILDBOX_PULL_REQUEST"
buildbox-run "git fetch origin +refs/pull/$BUILDBOX_PULL_REQUEST/merge:"
buildbox-run "git checkout -qf FETCH_HEAD"
else
if [ "$BUILDBOX_TAG" == "" ]
then
# Only reset to the branch if we're not on a tag
buildbox-run "git reset --hard origin/$BUILDBOX_BRANCH"
fi
buildbox-run "git checkout -qf \"$BUILDBOX_COMMIT\""
fi
if [ "$BUILDBOX_SCRIPT_PATH" == "" ]
then
echo "ERROR: No script to run. Please go to \"Project Settings\" and configure your build step's \"Script to Run\""
exit 1
fi
## Docker
if [ "$BUILDBOX_DOCKER" != "" ]
then
DOCKER_CONTAINER="buildbox_"$BUILDBOX_JOB_ID"_container"
DOCKER_IMAGE="buildbox_"$BUILDBOX_JOB_ID"_image"
function docker-cleanup {
docker rm -f $DOCKER_CONTAINER
# Enabling the following line will prevent your build server from filling up,
# but will slow down your builds because it'll be built from scratch each time.
#
# docker rmi -f $DOCKER_IMAGE
}
trap docker-cleanup EXIT
# Build the Docker image, namespaced to the job
buildbox-run "docker build -t $DOCKER_IMAGE ."
echo "--- running $BUILDBOX_SCRIPT_PATH (in Docker container $DOCKER_IMAGE)"
# Run the build script command in a one-off container
buildbox-prompt-and-run "docker run --name $DOCKER_CONTAINER $DOCKER_IMAGE ./$BUILDBOX_SCRIPT_PATH"
## Fig
elif [ "$BUILDBOX_FIG_CONTAINER" != "" ]
then
# Fig strips dashes and underscores, so we'll remove them to match the docker container names
FIG_PROJ_NAME="buildbox"${BUILDBOX_JOB_ID//-}
# The name of the docker container fig creates when it creates the adhoc run
FIG_CONTAINER_NAME=$FIG_PROJ_NAME"_"$BUILDBOX_FIG_CONTAINER
function fig-cleanup {
fig -p $FIG_PROJ_NAME kill
fig -p $FIG_PROJ_NAME rm --force
# The adhoc run container isn't cleaned up by fig, so we have to do it ourselves
echo "Killing "$FIG_CONTAINER_NAME"_run_1..."
docker rm -f $FIG_CONTAINER_NAME"_run_1"
# Enabling the following line will prevent your build server from filling up,
# but will slow down your builds because it'll be built from scratch each time.
#
# docker rmi -f $FIG_CONTAINER_NAME
}
trap fig-cleanup EXIT
# Build the Docker images using Fig, namespaced to the job
buildbox-run "fig -p $FIG_PROJ_NAME build"
echo "--- running $BUILDBOX_SCRIPT_PATH (in Fig container '$BUILDBOX_FIG_CONTAINER')"
# Run the build script command in the service specified in BUILDBOX_FIG_CONTAINER
buildbox-prompt-and-run "fig -p $FIG_PROJ_NAME run $BUILDBOX_FIG_CONTAINER ./$BUILDBOX_SCRIPT_PATH"
## Standard
else
echo "--- running $BUILDBOX_SCRIPT_PATH"
# Run the step's build script
."/$BUILDBOX_SCRIPT_PATH"
fi
# Capture the exit status for the end
EXIT_STATUS=$?
if [ "$BUILDBOX_ARTIFACT_PATHS" != "" ]
then
# NOTE: In agent version 1.0 and above, the location and the name of the
# buildbox artifact binary changed. As of this verison, builbdox-artifact has
# been rolled into buildbox-agent.
if buildbox-agent --help | grep build-artifact
then
# If you want to upload artifacts to your own server, uncomment the lines below
# and replace the AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY with keys to your
# own bucket.
#
# export AWS_SECRET_ACCESS_KEY=yyy
# export AWS_ACCESS_KEY_ID=xxx
# export AWS_S3_ACL=private
# buildbox-run "buildbox-agent build-artifact upload \"$BUILDBOX_ARTIFACT_PATHS\" \"s3://name-of-your-s3-bucket/$BUILDBOX_JOB_ID\""
# Show the output of the artifact uploder when in debug mode
if [ "$BUILDBOX_AGENT_DEBUG" == "true" ]
then
echo '--- uploading artifacts'
buildbox-run "buildbox-agent build-artifact upload \"$BUILDBOX_ARTIFACT_PATHS\""
else
buildbox-run "buildbox-agent build-artifact upload \"$BUILDBOX_ARTIFACT_PATHS\" > /dev/null 2>&1"
fi
elif [[ -e $BUILDBOX_PATH/buildbox-artifact ]]
then
# If you want to upload artifacts to your own server, uncomment the lines below
# and replace the AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY with keys to your
# own bucket.
# export AWS_SECRET_ACCESS_KEY=yyy
# export AWS_ACCESS_KEY_ID=xxx
# buildbox-run "buildbox-artifact upload \"$BUILDBOX_ARTIFACT_PATHS\" "s3://name-of-your-s3-bucket/$BUILDBOX_JOB_ID" --url $BUILDBOX_AGENT_API_URL > /dev/null 2>&1"
# By default we silence the buildbox-artifact build output. However, if you'd like to see
# it in your logs, remove the: > /dev/null 2>&1 from the end of the line.
buildbox-run "buildbox-artifact upload \"$BUILDBOX_ARTIFACT_PATHS\" --url $BUILDBOX_AGENT_API_URL > /dev/null 2>&1"
else
echo >&2 "ERROR: buildbox-artifact could not be found in $BUILDBOX_PATH"
exit 1
fi
fi
exit $EXIT_STATUS