-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·165 lines (129 loc) · 3.94 KB
/
build.sh
File metadata and controls
executable file
·165 lines (129 loc) · 3.94 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
#!/bin/bash
set -eo pipefail
if [[ -f /etc/environment_proxy ]]; then
source /etc/environment_proxy
fi
if [[ "$1" == "-" ]]; then
slug_file="$1"
else
slug_file=/tmp/slug.tgz
if [[ "$1" ]]; then
put_url="$1"
fi
fi
app_dir=/app
build_root=/tmp/build
cache_root=/tmp/cache
buildpack_root=/tmp/buildpacks
mkdir -p $app_dir
mkdir -p $cache_root
mkdir -p $buildpack_root
mkdir -p $build_root/.profile.d
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 line; do
if [[ "$line" == --* ]]; then
echo $'\e[1G'$line | output_redirect
else
echo $'\e[1G ' "$line" | output_redirect
fi
done
}
## Copy application code over
if [ -d "/tmp/app" ]; then
cp -rf /tmp/app/. $app_dir
else
cat | tar -xmC $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"
export REQUEST_ID=$(openssl rand -base64 32)
export STACK=cedar-14
## 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
if [[ -n "$SSH_KEY" ]]; then
mkdir -p ~/.ssh/
chmod 700 ~/.ssh/
echo $SSH_KEY | base64 -d > ~/.ssh/id_rsa
chmod 400 ~/.ssh/id_rsa
echo 'StrictHostKeyChecking=no' > ~/.ssh/config
chmod 600 ~/.ssh/config
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"
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
## Buildpack compile
$selected_buildpack/bin/compile "$build_root" "$cache_root" | ensure_indent
$selected_buildpack/bin/release "$build_root" "$cache_root" > $build_root/.release
## Display process types
echo_title "Discovering process types"
if [[ -f "$build_root/Procfile" ]]; then
types=$(ruby -e "require 'yaml';puts YAML.load_file('$build_root/Procfile').keys().join(', ')")
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
## 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 [[ "$slug_file" != "-" ]]; then
slug_size=$(du -Sh "$slug_file" | cut -f1)
echo_title "Compiled slug size is $slug_size"
if [[ $put_url ]]; then
curl -0 -s -o /dev/null -X PUT -T $slug_file "$put_url"
fi
fi