📜 ⬆️ ⬇️

Deploy Ratpack app on Heroku

Deploying a simple Ratpack application to Heroku is much more complicated than it should be. After a lot of trial and error, I finally managed to get a working configuration for a successful deployment. This article is a manual on how to deploy an application step by step. In the next article I will explain how to use the Gradle plugin to simplify the process.

Artifacts

For successful deployment, certain artifacts must be presented in your Heroku project in order to successfully discover your application. I recommend placing these files in a separate branch in your Git, because Heroku uses Git for deployment.
First we need to create the following folder structure:
bin
Comp── compile
Detect── detect
And files with the following content:
bin / detect
#!/usr/bin/env bash # bin/use <build-dir> if [ -f $1/src/ratpack/ratpack.groovy ]; then echo "Ratpack" && exit 0 else echo "no" && exit 1 fi 

bin / compile
 #!/usr/bin/env bash # bin/compile <build-dir> <cache-dir> # fail fast set -e BIN_DIR=$(cd $(dirname $0); pwd) # absolute path # parse args BUILD_DIR=$1 CACHE_DIR=$2 #env variables JAVA_DIST="openjdk1.7.0_21" JDK7_URL="https://s3.amazonaws.com/heroku-jdk/${JAVA_DIST}.tar.gz" #create the cache dir if it doesn't exist mkdir -p $CACHE_DIR cd "$BUILD_DIR" #jdk7 if [ -d .jdk7 ]; then echo "-----> .jdk7 folder found, moving along." else echo -n "-----> .jdk7 folder not found! " if [[ -d "$CACHE_DIR/.jdk7" ]]; then echo -n "Copying jdk from cache to app... " cp -r "$CACHE_DIR/.jdk7" "$BUILD_DIR" echo "Done!" else echo -n "-----> Installing ${JAVA_DIST} build (to .jdk7)....." mkdir "$BUILD_DIR/.jdk7" cd "$BUILD_DIR/.jdk7" curl --max-time 180 --location "$JDK7_URL" | tar xz cd "$BUILD_DIR" echo "Done!" fi fi cd $BUILD_DIR export JAVA_HOME="$BUILD_DIR/.jdk7" export PATH="$JAVA_HOME/bin:$PATH" echo "" echo "-----> Building project with Gradle wrapper:" echo " ./gradlew clean installApp" export GRADLE_OPTS="-Dfile.encoding=UTF-8 -server -Xmx512m -XX:+UseCompressedOops" export GRADLE_USER_HOME="$CACHE_DIR/.gradle" ./gradlew clean installApp rm -rf "$CACHE_DIR/.jdk7" cp -r .jdk7 "$CACHE_DIR/.jdk7" 


Next we need a Procfile, leave it in the root directory of our Ratpack project.
Procfile
 --- addons: config_vars: JAVA_OPTS: -Dfile.encoding=UTF-8 -server -Xmx512m -XX:+UseCompressedOops default_process_types: web: build/install/ratpack/bin/ratpack 


The build / install / ratpack / bin / ratpack file , as well as the build / install / ratpack folder, appear in the web process. This file and folder will be named the root of your project. It is possible (and necessary) to override this value for deployments in Heroku, since the name of the root project comes from the generated hash. In order to fix this, we need to set the name of the project root to something known (in our case ratpack). This can be configured in the settings file in the project root:
')
settings.gradle
rootProject.name = 'ratpack'
Now we need to set the RATPACK_OPTS environment variable pointing to the $ PORT variable provided to us during the deployment process. It should be written in a script hidden in the depths of our project in the profile.d folder.

.profile.d / setenv.sh

 #!/bin/bash echo "Setting environment varialbes..." export RATPACK_OPTS="-Dratpack.port=$PORT" export JAVA_HOME="${BUILD_DIR}/.jdk7" 


Now we have the following folder structure:
Bin── bin
│ ├── compile
│ └── detect
Pro── Procfile
├── .profile.d
└── setenv.sh
Settings── settings.gradle

Deployment process

Now we need to create our new application on Heroku using the buildpack command. This simple command transfers all the commands to the artifacts we created in our project. The team also creates our application, like Cedar Stack based on Ubuntu 10.04 on Heroku.
heroku apps: create myapplication - buildpack github.com/kr/heroku-buildpack-inline.git

Next, we switch to our new branch, which we created for deployment and commit all our artifacts there.

 git checkout -b deploy git add bin Procfile settings.gradle .profile.d git commit -m 'Heroku deployment artifacts.' git push heroku deploy:master 


Now we need to see our application successfully deployed:
-----> Fetching custom git buildpack ... done
-----> Ratpack app detected
-----> .jdk7 folder not found! Copying jdk from cache to app ... Done!

-----> Building project with Gradle wrapper:
./gradlew clean installApp
<snip />
-----> Discovering process types
Procfile declares types -> JAVA_HOME, JAVA_OPTS, web

-----> Compiled slug size: 64.8MB
-----> Launching ... done, v38
myapplication.herokuapp.com deployed to Heroku
And we can look at the logs to confirm this:
heroku logs --tail --num 100 --app myapplication

In the next article I will explain how to do the same, using the Gradle plugin, which is proposed here . The source can be found here . The original article is here . Thanks for attention.

Source: https://habr.com/ru/post/220341/


All Articles