|
| 1 | +#!/bin/bash |
| 2 | +set -eo pipefail |
| 3 | + |
| 4 | +slug_file=/tmp/slug.tgz |
| 5 | +app_dir=/app |
| 6 | +build_root=/tmp/build |
| 7 | +cache_root=/tmp/cache |
| 8 | +buildpack_root=/tmp/buildpacks |
| 9 | + |
| 10 | +mkdir -p $app_dir |
| 11 | +mkdir -p $cache_root |
| 12 | +mkdir -p $buildpack_root |
| 13 | +mkdir -p $build_root/.profile.d |
| 14 | + |
| 15 | +function output_redirect() { |
| 16 | + if [[ "$slug_file" == "-" ]]; then |
| 17 | + cat - 1>&2 |
| 18 | + else |
| 19 | + cat - |
| 20 | + fi |
| 21 | +} |
| 22 | + |
| 23 | +function echo_title() { |
| 24 | + echo $'\e[1G----->' $* | output_redirect |
| 25 | +} |
| 26 | + |
| 27 | +function echo_normal() { |
| 28 | + echo $'\e[1G ' $* | output_redirect |
| 29 | +} |
| 30 | + |
| 31 | +function ensure_indent() { |
| 32 | + while read line; do |
| 33 | + if [[ "$line" == --* ]]; then |
| 34 | + echo $'\e[1G'$line | output_redirect |
| 35 | + else |
| 36 | + echo $'\e[1G ' "$line" | output_redirect |
| 37 | + fi |
| 38 | + |
| 39 | + done |
| 40 | +} |
| 41 | + |
| 42 | +## Copy application code over |
| 43 | +if [ -d "/tmp/app" ]; then |
| 44 | + cp -rf /tmp/app/. $app_dir |
| 45 | +else |
| 46 | + cat | tar -xmC $app_dir |
| 47 | +fi |
| 48 | + |
| 49 | +# In heroku, there are two separate directories, and some |
| 50 | +# buildpacks expect that. |
| 51 | +cp -r $app_dir/. $build_root |
| 52 | + |
| 53 | +# Grant the slugbuilder user access to all relevant |
| 54 | +# directories, then run the rest as slugbuilder |
| 55 | +chown -R slugbuilder:slugbuilder $app_dir \ |
| 56 | + $cache_root \ |
| 57 | + $buildpack_root \ |
| 58 | + $build_root |
| 59 | +su slugbuilder |
| 60 | + |
| 61 | +## Buildpack fixes |
| 62 | + |
| 63 | +export APP_DIR="$app_dir" |
| 64 | +export HOME="$app_dir" |
| 65 | +export REQUEST_ID=$(openssl rand -base64 32) |
| 66 | + |
| 67 | +## Buildpack detection |
| 68 | + |
| 69 | +buildpacks=($buildpack_root/*) |
| 70 | +selected_buildpack= |
| 71 | + |
| 72 | +if [[ -n "$BUILDPACK_URL" ]]; then |
| 73 | + echo_title "Fetching custom buildpack" |
| 74 | + |
| 75 | + buildpack="$buildpack_root/custom" |
| 76 | + rm -fr "$buildpack" |
| 77 | + git clone --quiet --depth=1 "$BUILDPACK_URL" "$buildpack" |
| 78 | + selected_buildpack="$buildpack" |
| 79 | + buildpack_name=$($buildpack/bin/detect "$build_root") && selected_buildpack=$buildpack |
| 80 | +else |
| 81 | + for buildpack in "${buildpacks[@]}"; do |
| 82 | + buildpack_name=$($buildpack/bin/detect "$build_root") && selected_buildpack=$buildpack && break |
| 83 | + done |
| 84 | +fi |
| 85 | + |
| 86 | +if [[ -n "$selected_buildpack" ]]; then |
| 87 | + echo_title "$buildpack_name app detected" |
| 88 | + else |
| 89 | + echo_title "Unable to select a buildpack" |
| 90 | + exit 1 |
| 91 | +fi |
| 92 | + |
| 93 | +## Buildpack compile |
| 94 | + |
| 95 | +$selected_buildpack/bin/compile "$build_root" "$cache_root" | ensure_indent |
| 96 | + |
| 97 | +$selected_buildpack/bin/release "$build_root" "$cache_root" > $build_root/.release |
| 98 | + |
| 99 | +## Display process types |
| 100 | + |
| 101 | +echo_title "Discovering process types" |
| 102 | +if [[ -f "$build_root/Procfile" ]]; then |
| 103 | + types=$(ruby -e "require 'yaml';puts YAML.load_file('$build_root/Procfile').keys().join(', ')") |
| 104 | + echo_normal "Procfile declares types -> $types" |
| 105 | +fi |
| 106 | +default_types="" |
| 107 | +if [[ -s "$build_root/.release" ]]; then |
| 108 | + default_types=$(ruby -e "require 'yaml';puts (YAML.load_file('$build_root/.release')['default_process_types'] || {}).keys().join(', ')") |
| 109 | + [[ $default_types ]] && echo_normal "Default process types for $buildpack_name -> $default_types" |
| 110 | +fi |
| 111 | + |
| 112 | + |
| 113 | +## Produce slug |
| 114 | + |
| 115 | +if [[ -f "$build_root/.slugignore" ]]; then |
| 116 | + tar --exclude='.git' --use-compress-program=pigz -X "$build_root/.slugignore" -C $build_root -cf $slug_file . | cat |
| 117 | +else |
| 118 | + tar --exclude='.git' --use-compress-program=pigz -C $build_root -cf $slug_file . | cat |
| 119 | +fi |
| 120 | + |
| 121 | +if [[ "$slug_file" != "-" ]]; then |
| 122 | + slug_size=$(du -Sh "$slug_file" | cut -f1) |
| 123 | + echo_title "Compiled slug size is $slug_size" |
| 124 | +fi |
0 commit comments