Skip to content

Latest commit

 

History

History
199 lines (168 loc) · 6.38 KB

03-3-B-Heroku.adoc

File metadata and controls

199 lines (168 loc) · 6.38 KB

Heroku

Preparations

  1. Sign up/log in on Heroku by visiting https://www.heroku.com/.

  2. Install the command line client for Heroku: Heroku CLI

    Note
    The Travis client might also be useful at later stages of the course, you can install it from here: Travis CLI
  3. Log in to Heroku CLI by opening a terminal an typing: heroku login.

Creating a Heroku app

We are creating a Heroku application and deploying the Hello world! Spring example. Additionally, the steps below will make it possible to store multiple different applications in the same git repository and deploy them individually to Heroku. Steps will be shown through the example EventRegistration application, and should be adapted in the course project.

Note
All actions described here for configuring Heroku applications using the Heroku CLI could also be done via the web UI.
  1. Once you are logged in with the Heroku-CLI, create a new Heroku application: in the root of the git repository of your repository (assumed to be ~/git/eventregistration), issue the below command to create an application named "eventregistration-backend-<UNIQUE_ID>".

heroku create eventregistration-backend-<UNIQUE_ID> -n

Note
In Heroku, the application name should be unique Heroku-wise, that is, each application in Heroku’s system should have a unique name. If you don’t provide a name parameter for the command, Heroku will randomly generate one.
  1. Add the multi procfile and Gradle buildpacks to the app.

    heroku buildpacks:add -a eventregistration-backend-<UNIQUE_ID> https://github.com/heroku/heroku-buildpack-multi-procfile
    heroku buildpacks:add -a eventregistration-backend-<UNIQUE_ID> heroku/gradle
    Caution
    Order is important.

Adding a database to the application

  1. Open the Heroku applications web page and go to Resources, then add the Heroku Postgres add-on.
    Heroku Postgres add-on

  2. Click the entry for Postgres within the list of add-ons, then go to Settings. You can see the database credentials there. Heroku Postgres add-on

    Note
    The credentials are periodically updated and changed by Heroku, so make sure that you are using the actual credentials when manually connecting to the database. (E.g., during manual testing.)

Extending the build for the Heroku deployment environment

  1. Before deploying, a top level build.gradle and settings.gradle need to be created in the root of the repository (i.e., in ~/git/eventregistration)
    build.gradle:

    task stage () {
        dependsOn ':EventRegistration-Backend:assemble'
    }

    settings.gradle:

    include ':EventRegistration-Backend'
  2. Generate the Gradle wrapper with the newest Gradle version

    gradle wrapper --gradle-version 5.6.2
  3. Create a .gitignore file for the .gradle folder:
    .gitignore:

    .gradle/
  4. Add all new files to git

    git add .
    git status #make sure that files in .gradle/ are not added

    Expected output for git status:

    On branch master
    Your branch is ahead of 'origin/master' by 2 commits.
      (use "git push" to publish your local commits)
    
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
    	new file:   .gitignore
    	new file:   build.gradle
    	new file:   gradle/wrapper/gradle-wrapper.jar
    	new file:   gradle/wrapper/gradle-wrapper.properties
    	new file:   gradlew
    	new file:   gradlew.bat
    	new file:   settings.gradle

    Commit changes:

    git commit -m "Adding Gradle wrapper"

Supply application-specific setting for Heroku

  1. Within the EventRegistration-Backend folder, create a file called Procfile (not Procfile.txt, name it exactly Procfile) with the content:

    web: java -jar EventRegistration-Backend/build/libs/EventRegistration-Backend-0.0.1-SNAPSHOT.jar
  2. Add the Procfile to a new commit

  3. Configure the multi-procfile buildpack to find the Procfile:

    heroku config:add PROCFILE=EventRegistration-Backend/Procfile --app eventregistration-backend-<UNIQUE_ID>

Deploying the app

  1. Obtain and copy the Heroku Git URL

    heroku git:remote --app eventregistration-backend-<UNIQUE_ID> --remote backend-heroku

    Output:

    set git remote backend-heroku to https://git.heroku.com/eventregistration-backend-<UNIQUE_ID>.git
  2. Verify that the backend-heroku remote is successfully added besides origin with git remote -v. Output:

    backend-heroku	https://git.heroku.com/eventregistration-backend-123.git (fetch)
    backend-heroku	https://git.heroku.com/eventregistration-backend-123.git (push)
    origin	git@github.com:imbur/eventregistration.git (fetch)
    origin	git@github.com:imbur/eventregistration.git (push)
  3. Deploy your application with

    git push backend-heroku master
    Note
    If it fails to build, make sure you try understanding the output. Typical issue: buildpacks are not added/are not in the right order.
  4. Visit the link provided in the build output. It may take some time (even 30-60 seconds) for the server to answer the first HTTP request, so be patient!

  5. Save your work to the GitHub repository, too: git push origin master
    Final layout of the files (only two directory levels are shown and hidden items are suppressed):

~/git/eventregistration
├── build.gradle
├── EventRegistration-Backend
│   ├── build
│   │   ├── classes
│   │   ├── libs
│   │   ├── resources
│   │   └── tmp
│   ├── build.gradle
│   ├── gradle
│   │   └── wrapper
│   ├── gradlew
│   ├── gradlew.bat
│   ├── Procfile
│   ├── settings.gradle
│   └── src
│       ├── main
│       └── test
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── README.md
└── settings.gradle