|
| 1 | +#!/bin/bash |
| 2 | +set -eo pipefail |
| 3 | +source $(dirname $0)/build-helpers.sh |
| 4 | + |
| 5 | +app_dir=/app |
| 6 | +build_root=/tmp/build |
| 7 | +cache_root=/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 | +cd $app_dir |
| 16 | + |
| 17 | +# In heroku, there are two separate directories, and some |
| 18 | +# buildpacks expect that. |
| 19 | +cp -r . $build_root |
| 20 | + |
| 21 | +## Get custom buildpack |
| 22 | + |
| 23 | +if [[ -f ".env" ]]; then |
| 24 | + source ".env" |
| 25 | +fi |
| 26 | + |
| 27 | +## Buildpack fixes |
| 28 | + |
| 29 | +export APP_DIR="$app_dir" |
| 30 | +export HOME="$app_dir" |
| 31 | +export REQUEST_ID=$(openssl rand -base64 32) |
| 32 | +export STACK=cedar-14 |
| 33 | +export CURL_CONNECT_TIMEOUT=30 |
| 34 | + |
| 35 | +## Buildpack detection |
| 36 | + |
| 37 | +buildpacks=($buildpack_root/*) |
| 38 | +selected_buildpack= |
| 39 | + |
| 40 | +if [[ -n "$BUILDPACK_URL" ]]; then |
| 41 | + echo_title "Fetching custom buildpack" |
| 42 | + |
| 43 | + buildpack="$buildpack_root/custom" |
| 44 | + rm -rf "$buildpack" |
| 45 | + /tmp/builder/install-buildpack "$buildpack_root" "$BUILDPACK_URL" custom &> /dev/null |
| 46 | + selected_buildpack="$buildpack" |
| 47 | + buildpack_name=$($buildpack/bin/detect "$build_root") && selected_buildpack=$buildpack |
| 48 | +else |
| 49 | + for buildpack in "${buildpacks[@]}"; do |
| 50 | + buildpack_name=$($buildpack/bin/detect "$build_root") && selected_buildpack=$buildpack && break |
| 51 | + done |
| 52 | +fi |
| 53 | + |
| 54 | +if [[ -n "$selected_buildpack" ]]; then |
| 55 | + echo_title "$buildpack_name app detected" |
| 56 | +else |
| 57 | + echo_title "Unable to select a buildpack" |
| 58 | + exit 1 |
| 59 | +fi |
| 60 | + |
| 61 | +## Buildpack compile |
| 62 | + |
| 63 | +$selected_buildpack/bin/compile "$build_root" "$cache_root" | ensure_indent |
| 64 | +$selected_buildpack/bin/release "$build_root" "$cache_root" > $build_root/.release |
| 65 | + |
| 66 | +## Display process types |
| 67 | + |
| 68 | +echo_title "Discovering process types" |
| 69 | +if [[ -f "$build_root/Procfile" ]]; then |
| 70 | + types=$(ruby -e "require 'yaml';puts YAML.load_file('$build_root/Procfile').keys().join(', ')") |
| 71 | + echo_normal "Procfile declares types -> $types" |
| 72 | +fi |
| 73 | +default_types="" |
| 74 | +if [[ -s "$build_root/.release" ]]; then |
| 75 | + default_types=$(ruby -e "require 'yaml';puts (YAML.load_file('$build_root/.release')['default_process_types'] || {}).keys().join(', ')") |
| 76 | + [[ $default_types ]] && echo_normal "Default process types for $buildpack_name -> $default_types" |
| 77 | +fi |
| 78 | + |
| 79 | +## Load profile.d and release config |
| 80 | + |
| 81 | +if [[ -s .release ]]; then |
| 82 | + ruby -e "require 'yaml';(YAML.load_file('.release')['config_vars'] || {}).each{|k,v| puts \"#{k}=\${#{k}:-'#{v}'}\"}" > .profile.d/config_vars |
| 83 | +fi |
| 84 | + |
| 85 | +## Generate start commands |
| 86 | +cat > /exec <<EOF |
| 87 | +#!/bin/bash |
| 88 | +export HOME=$app_dir |
| 89 | +cd $app_dir |
| 90 | +for file in .profile.d/*; do |
| 91 | + source \$file |
| 92 | +done |
| 93 | +hash -r |
| 94 | +"\$@" |
| 95 | +EOF |
| 96 | + |
| 97 | +cat > /start <<EOF |
| 98 | +#!/bin/bash |
| 99 | +export HOME=$app_dir |
| 100 | +cd $app_dir |
| 101 | +for file in .profile.d/*; do |
| 102 | + source \$file |
| 103 | +done |
| 104 | +hash -r |
| 105 | +if [[ -f Procfile ]]; then |
| 106 | + ruby -e "require 'yaml';puts YAML.load_file('Procfile')['\$1']" | bash |
| 107 | +else |
| 108 | + ruby -e "require 'yaml';puts (YAML.load_file('.release')['default_process_types'] || {})['\$1']" | bash |
| 109 | +fi |
| 110 | +EOF |
| 111 | + |
| 112 | +## Finalize build |
| 113 | +chmod +x /exec |
| 114 | +chmod +x /start |
| 115 | +rm -rf $app_dir |
| 116 | +mv $build_root $app_dir |
| 117 | + |
| 118 | +# Clean up |
| 119 | +rm -rf /build/ |
| 120 | +rm -rf /tmp/* |
0 commit comments