-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·257 lines (210 loc) · 6.68 KB
/
build.sh
File metadata and controls
executable file
·257 lines (210 loc) · 6.68 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env bash
set -eo pipefail
function sleep_before_exit {
# delay before exiting, so stdout/stderr flushes through the logging system
sleep 3
}
trap sleep_before_exit EXIT
[[ $DEIS_DEBUG ]] && set -x
unset DEIS_DEBUG
app_dir=/app
build_root=/tmp/build
cache_root=/tmp/cache
cache_file=/tmp/cache.tgz
secret_dir=/tmp/env
env_root=/tmp/environment
buildpack_root=/tmp/buildpacks
mkdir -p $app_dir
mkdir -p $cache_root
mkdir -p $env_root
mkdir -p $secret_dir
mkdir -p $buildpack_root
mkdir -p $build_root/.profile.d
if ! [[ -z "${TAR_PATH}" ]]; then
get_object
tar -xzf /tmp/slug.tgz -C /app/
unset TAR_PATH
fi
if [[ "$1" == "-" ]]; then
slug_file="$1"
else
slug_file=/tmp/slug.tgz
fi
function output_redirect() {
if [[ "$slug_file" == "-" ]]; then
cat - 1>&2
else
cat -
fi
}
function echo_title() {
echo $'\e[1G----->' "$*" | output_redirect
}
function echo_normal() {
echo $'\e[1G ' "$*" | output_redirect
}
function ensure_indent() {
while read -r line; do
if [[ "$line" == --* ]]; then
echo $'\e[1G'"$line" | output_redirect
else
echo $'\e[1G ' "$line" | output_redirect
fi
done
}
function cache_fingerprint() {
md5deep -r ${cache_root} | sort | uniq | md5sum
}
# Restore cache when a $CACHE_PATH was supplied
if ! [[ -z "${CACHE_PATH}" ]]; then
echo_title "Restoring cache..."
restore_cache
if [[ -f ${cache_file} ]]; then
tar -xzf ${cache_file} -C ${cache_root}
echo_normal "Done!"
else
echo_normal "No cache file found. If this is the first deploy, it will be created now."
fi
original_cache_fingerprint=$(cache_fingerprint)
fi
## Copy application code over
if [ -d "/tmp/app" ]; then
cp -rf /tmp/app/. $app_dir
chown -R slug:slug $app_dir
fi
# In heroku, there are two separate directories, and some
# buildpacks expect that.
cp -r $app_dir/. $build_root
## Buildpack fixes
export APP_DIR="$app_dir"
export HOME="$app_dir"
REQUEST_ID=$(openssl rand -base64 32)
export REQUEST_ID
export STACK=heroku-18
## copy the environment dir excluding the ephemeral ..data/ dir and other symlinks created by Kubernetes.
secret_dir_file_list="$(ls -A $secret_dir)"
for subpath in $secret_dir_file_list; do
full_path="$secret_dir/$subpath"
if [[ -f "$full_path" ]]; then
cp "$full_path" "$env_root/"
fi
done
## SSH key configuration
if [[ -f "$env_root/SSH_KEY" ]]; then
mkdir -p ~/.ssh/
chmod 700 ~/.ssh/
base64 -d "$env_root/SSH_KEY" > ~/.ssh/id_rsa
chmod 400 ~/.ssh/id_rsa
echo 'StrictHostKeyChecking=no' > ~/.ssh/config
chmod 600 ~/.ssh/config
fi
## Buildpack detection
buildpacks=($buildpack_root/*)
selected_buildpack=
if [[ -n "$BUILDPACK_URL" ]]; then
echo_title "Fetching custom buildpack"
buildpack="$buildpack_root/custom"
rm -fr "$buildpack"
url=${BUILDPACK_URL%#*}
committish=${BUILDPACK_URL#*#}
if [ "$committish" == "$url" ]; then
committish="master"
fi
set +e
git clone --branch "$committish" --depth=1 "$url" "$buildpack" &> /dev/null
SHALLOW_CLONED=$?
set -e
if [ $SHALLOW_CLONED -ne 0 ]; then
# if the shallow clone failed partway through, clean up and try a full clone
rm -rf "$buildpack"
git clone --quiet "$url" "$buildpack"
pushd "$buildpack" &>/dev/null
git checkout --quiet "$committish"
git submodule init --quiet
git submodule update --quiet --recursive
popd &>/dev/null
fi
selected_buildpack="$buildpack"
buildpack_name=$("$buildpack/bin/detect" "$build_root") && selected_buildpack=$buildpack
else
for buildpack in "${buildpacks[@]}"; do
buildpack_name=$("$buildpack/bin/detect" "$build_root") && selected_buildpack=$buildpack && break
done
fi
if [[ -n "$selected_buildpack" ]]; then
echo_title "$buildpack_name app detected"
else
echo_title "Unable to select a buildpack"
exit 1
fi
## Run pre-compile hook
if [[ -f "$build_root/bin/pre-compile" ]]; then
pushd "$build_root" &> /dev/null
"$build_root/bin/pre-compile"
popd &>/dev/null
fi
## Buildpack compile
"$selected_buildpack/bin/compile" "$build_root" "$cache_root" "$env_root" | ensure_indent
"$selected_buildpack/bin/release" "$build_root" > $build_root/.release
## Run post-compile hook
if [[ -f "$build_root/bin/post-compile" ]]; then
pushd "$build_root" &> /dev/null
"$build_root/bin/post-compile"
popd &>/dev/null
fi
## Display process types
echo_title "Discovering process types"
if [[ -f "$build_root/Procfile" ]]; then
types=$(read_procfile_keys "$build_root/Procfile")
echo_normal "Procfile declares types -> $types"
fi
default_types=""
if [[ -s "$build_root/.release" ]]; then
default_types=$(ruby -e "require 'yaml';puts ((YAML.load_file('$build_root/.release') || {})['default_process_types'] || {}).keys().join(', ')")
[[ $default_types ]] && echo_normal "Default process types for $buildpack_name -> $default_types"
fi
# Fix any wayward permissions. We want everything in app to be owned
# by slug.
chown -R slug:slug $build_root/*
## Produce slug
if [[ -f "$build_root/.slugignore" ]]; then
tar -z --exclude='.git' -X "$build_root/.slugignore" -C $build_root -cf $slug_file . | cat
else
tar -z --exclude='.git' -C $build_root -cf $slug_file . | cat
fi
if [[ ! -f "$build_root/Procfile" ]]; then
if [[ -s "$build_root/.release" ]] && [[ $default_types ]]; then
ruby -e "require 'yaml';procTypes = (YAML.load_file('$build_root/.release')['default_process_types']);open('$build_root/Procfile','w') {|f| YAML.dump(procTypes,f)}"
else
echo "{}" > $build_root/Procfile
fi
fi
# Compress and save cache
if ! [[ -z "${CACHE_PATH}" ]]; then
echo_title "Checking for changes inside the cache directory..."
# If there's any files in the cache_root folder, we'll create a tar and upload
# it for future use
if [ "$(ls -A ${cache_root})" ]; then
# Let's check if the fingerprint changed, if it did, we'll be updating
# the cache
if [[ "$original_cache_fingerprint" != "$(cache_fingerprint)" ]]; then
echo_normal "Files inside cache folder changed, uploading new cache..."
# Create a new cache file and check if it requires to be updated
tar -z -C ${cache_root} -cf ${cache_file} .
cache_size=$(du -Sh "$cache_file" | cut -f1)
store_cache
echo_normal "Done: Uploaded cache (${cache_size})"
else
echo_normal "Cache unchanged, not updating"
fi
else
echo_normal "No files were added to the cache folder, cache wasn't updated"
fi
fi
if [[ "$slug_file" != "-" ]]; then
slug_size=$(du -Sh "$slug_file" | cut -f1)
echo_title "Compiled slug size is $slug_size"
if [[ $PUT_PATH ]]; then
put_object
fi
fi