Skip to content

Commit

Permalink
Use jvm buildpack to install JRuby dependencies (#1119)
Browse files Browse the repository at this point in the history
* Use jvm buildpack to install JRuby dependencies

Currently, the jvm logic is duplicated in this buildpack and the jvm buildpack. By using the JVM buildpack we can reduce duplication and increase consistency across the Heroku platform.

* Use named variables instead of bash args

Previously the function compile_buildpack_v2 directly uses $1, $2, and $3. This change updates this function to instead use named local variables.

In addition, there was a typo in the output to the user.
  • Loading branch information
schneems committed Feb 11, 2021
1 parent bcae21a commit 648a149
Show file tree
Hide file tree
Showing 17 changed files with 419 additions and 391 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Main (unreleased)

* Ruby buildpack now relies on the JVM buildpack to install java for Jruby apps (https://github.com/heroku/heroku-buildpack-ruby/pull/1119)

## v223 (1/22/2021)

* Fix Gemfile.lock read bug from preventing propper removal of BUNDLED WITH declaration (https://github.com/heroku/heroku-buildpack-ruby/pull/1108)
Expand Down
20 changes: 20 additions & 0 deletions bin/compile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,30 @@
# The actual compilation code lives in `bin/support/ruby_compile`. This file instead
# bootstraps the ruby needed and then executes `bin/support/ruby_compile`

BUILD_DIR=$1
CACHE_DIR=$2
ENV_DIR=$3
BIN_DIR=$(cd $(dirname $0); pwd)
BUILDPACK_DIR=$(dirname $BIN_DIR)

source "$BIN_DIR/support/bash_functions.sh"
heroku_buildpack_ruby_install_ruby "$BIN_DIR" "$BUILDPACK_DIR"

if detect_needs_java "$BUILD_DIR"; then
cat <<EOM
## Warning: Your app needs java
The Ruby buildpack determined your app needs java installed
we recommend you add the jvm buildpack to your application:
$ heroku buildpacks:add heroku/jvm --index=1
-----> Installing Java
EOM

compile_buildpack_v2 "$BUILD_DIR" "$CACHE_DIR" "$ENV_DIR" "https://buildpack-registry.s3.amazonaws.com/buildpacks/heroku/jvm.tgz" "heroku/jvm"
fi

$heroku_buildpack_ruby_dir/bin/ruby $BIN_DIR/support/ruby_compile $@
109 changes: 109 additions & 0 deletions bin/support/bash_functions.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
#!/usr/bin/env bash

curl_retry_on_18() {
local ec=18;
local attempts=0;
while (( ec == 18 && attempts++ < 3 )); do
curl "$@" # -C - would return code 33 if unsupported by server
ec=$?
done
return $ec
}

# This function will install a version of Ruby onto the
# system for the buidlpack to use. It coordinates download
# and setting appropriate env vars for execution
Expand Down Expand Up @@ -44,3 +56,100 @@ heroku_buildpack_ruby_install_ruby()
export PATH=$heroku_buildpack_ruby_dir/bin/:$PATH
unset GEM_PATH
}

which_java()
{
which java > /dev/null
}

# Detects if a given Gemfile.lock has jruby in it
# $ cat Gemfile.lock | grep jruby # => ruby 2.5.7p001 (jruby 9.2.13.0)
detect_needs_java()
{
local app_dir=$1
local gemfile_lock="$app_dir/Gemfile.lock"
# local needs_jruby=0
local skip_java_install=1

if which_java; then
return $skip_java_install
fi
grep "(jruby " "$gemfile_lock" --quiet
}

# Runs another buildpack against the build dir
#
# Example:
#
# compile_buildpack_v2 "$build_dir" "$cache_dir" "$env_dir" "https://buildpack-registry.s3.amazonaws.com/buildpacks/heroku/nodejs.tgz" "heroku/nodejs"
#
compile_buildpack_v2()
{
local build_dir=$1
local cache_dir=$2
local env_dir=$3
local buildpack=$4
local name=$5

local dir
local url
local branch

dir=$(mktemp -t buildpackXXXXX)
rm -rf "$dir"

url=${buildpack%#*}
branch=${buildpack#*#}

if [ "$branch" == "$url" ]; then
branch=""
fi

if [ "$url" != "" ]; then
echo "-----> Downloading Buildpack: ${name}"

if [[ "$url" =~ \.tgz$ ]] || [[ "$url" =~ \.tgz\? ]]; then
mkdir -p "$dir"
curl_retry_on_18 -s "$url" | tar xvz -C "$dir" >/dev/null 2>&1
else
git clone "$url" "$dir" >/dev/null 2>&1
fi
cd "$dir" || return

if [ "$branch" != "" ]; then
git checkout "$branch" >/dev/null 2>&1
fi

# we'll get errors later if these are needed and don't exist
chmod -f +x "$dir/bin/{detect,compile,release}" || true

framework=$("$dir"/bin/detect "$build_dir")

# shellcheck disable=SC2181
if [ $? == 0 ]; then
echo "-----> Detected Framework: $framework"
"$dir"/bin/compile "$build_dir" "$cache_dir" "$env_dir"

# shellcheck disable=SC2181
if [ $? != 0 ]; then
exit 1
fi

# check if the buildpack left behind an environment for subsequent ones
if [ -e "$dir/export" ]; then
set +u # http://redsymbol.net/articles/unofficial-bash-strict-mode/#sourcing-nonconforming-document
# shellcheck disable=SC1090
source "$dir/export"
set -u # http://redsymbol.net/articles/unofficial-bash-strict-mode/#sourcing-nonconforming-document
fi

if [ -x "$dir/bin/release" ]; then
"$dir"/bin/release "$build_dir" > "$1"/last_pack_release.out
fi
else
echo "Couldn't detect any framework for this buildpack. Exiting."
exit 1
fi
fi
}

20 changes: 20 additions & 0 deletions bin/test-compile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,30 @@
# The actual `bin/test-compile` code lives in `bin/ruby_test-compile`. This file instead
# bootstraps the ruby needed and then executes `bin/ruby_test-compile`

BUILD_DIR=$1
CACHE_DIR=$2
ENV_DIR=$3
BIN_DIR=$(cd $(dirname $0); pwd)
BUILDPACK_DIR=$(dirname $BIN_DIR)

source "$BIN_DIR/support/bash_functions.sh"
heroku_buildpack_ruby_install_ruby "$BIN_DIR" "$BUILDPACK_DIR"

if detect_needs_java "$BUILD_DIR"; then
cat <<EOM
## Warning: Your app needs java
The Ruby buildpack determined your app needs java installed
we recommend you add the jvm buildpack to your application:
$ heroku buildpacks:add heroku/jvm --index=1
-----> Installing Java
EOM

compile_buildpack_v2 "$BUILD_DIR" "$CACHE_DIR" "$ENV_DIR" "https://buildpack-registry.s3.amazonaws.com/buildpacks/heroku/jvm.tgz" "heroku/jvm"
fi

$heroku_buildpack_ruby_dir/bin/ruby $BIN_DIR/support/ruby_test-compile $@
13 changes: 13 additions & 0 deletions changelogs/unreleased/jvm_installer_default.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## JVM support for JRuby apps is now provided by the `heroku/jvm` buildpack

JRuby applications require the JVM to run. Before this change, the JVM for JRuby apps was provided by the `heroku/ruby` buildpack for JRuby applications.

Now the custom JVM installation logic in the `heroku/ruby` buildpack has been removed, and instead, the `heroku/jvm` buildpack will be called directly by the Ruby buildpack.

Instead of relying on this functionality, it is recommended to manually require the `heroku/jvm` buildpack for your application:

```
$ heroku buildpacks:add heroku/jvm --index=1
```

For more information on JRuby support, see the [Heroku Ruby Support page](https://devcenter.heroku.com/articles/ruby-support).
1 change: 0 additions & 1 deletion hatchet.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"sharpstone/cd_ruby",
"sharpstone/ruby_193_jruby_173",
"sharpstone/ruby_193_jruby_176",
"sharpstone/ruby_193_jruby_1_7_27",
"sharpstone/ruby_193_jruby_17161_jdk7",
"sharpstone/ruby_193_bad_patch_cedar_14",
"sharpstone/ruby_25",
Expand Down
2 changes: 0 additions & 2 deletions hatchet.lock
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@
- a08e8b2cc61d08bd611accaa455dbcbfff80f831
- - "./repos/ruby/ruby_193_jruby_176"
- 9fcbc184cb386abc8784e9935d52d403e35c532c
- - "./repos/ruby/ruby_193_jruby_1_7_27"
- 06d5e86180ff0e5b6894f823736cd649dcfebed4
- - "./repos/ruby/ruby_25"
- 0cb3df80d55b61e9417f2ac00adb06e15ae37982
- - "./repos/ruby/ruby_version_does_not_exist"
Expand Down
120 changes: 0 additions & 120 deletions lib/language_pack/helpers/jvm_installer.rb

This file was deleted.

Loading

0 comments on commit 648a149

Please sign in to comment.