Deploy Laravel 5 App on heroku with mysql using bitbucket CI pipeline

ABIMBOLA JEREMIAH
4 min readJul 10, 2018

--

In this post, I’ll show you how to deploy your existing laravel 5 app from local to heroku using the CI pipeline. It’s quite easy. We will use mysql in this tutorial.

Things needed:

  • Php and Laravel knowledge
  • Heroku user account
  • Heroku toolbet (Heroku CLI)
  • Git knowledge
  • Bitbucket

This tutorial assumes you already have your laravel app created on your local server, so I won’t go through those steps anymore. If you don’t, quickly spin up a new laravel project by checking this link.

Step 1. Create a git repository on bitbucket, call it smallapp for example

Step 2. Clone git clone <repo name>the repository from bitbucket, and copy your laravel application into the folder created by git.

Now your app has been initialized on git. Open your project with any great IDE of your choice. I will recommend PhpStorm for this purpose. To check this, type git init on your terminal.

Step 3. commit your code to your repository on bitbucket with git add .

git commit -m "added something new to this repo just now"

git push origin master Once this is successful, you are ready to deploy your app on heroku

Step 4. Creating a Procfile

By default, Heroku will launch an Apache web server together with PHP to serve applications from the root directory of the project.

Type this on your terminal in PhpStorm. This step will create a Procfile in your project and then commit the changes.

echo web: vendor/bin/heroku-php-apache2 public/ > Procfile
$ git add .
$ git commit -m "Procfile for Heroku"

Step 5: Create Heroku user account; go to heroku and sign up if you don’t have a user account on heroku. Then you can login to that account from your project terminal using heroku login This will prompt you for username and password (password might not be revealed as you type).

Step 6: Create an app on heroku heroku create simpleapp Heroku automatically knows the language of the app you create.

Step 7: Set the APP_KEY variable withheroku config:set APP_KEY=… you can first run php artisan key:generate --show to see the key. Put that key in the first command.

Instead of pushing our project to heroku directly as seen on heroku’s documentation, we will setup a CI pipeline on bitbucket that will do that for us.

Step 8: Click on the Pipeline on the sidebar menu on bitbucket to setup a build pipeline. This is require you to create a .yml file in your project with name “bitbucket-pipelines.yml”.

Once you have done that and you commit the changes to the repo, bitbucket should automatically recognise that file. Paste the following code in there

# This is a sample build configuration for PHP.
# Check our guides at https://confluence.atlassian.com/x/e8YWN for more examples.
# Only use spaces to indent your .yml configuration.
# -----
# You can specify a custom docker image from Docker Hub as your build environment.
image: php:7.1
pipelines:
default:
- step:
caches:
- composer
script:
- php -v
- apt-get update
- apt-get install -y unzip git
- git push https://heroku:$HEROKU_API_KEY@git.heroku.com/$HEROKU_APP_NAME.git HEAD
- export CACHE_DRIVER=redis
- export SESSION_DRIVER=redis
- export DB_CONNECTION=mysql
- php artisan migrate

Step 9: Setup your variables on bitbucket for $HEROKU_API_KEY and $HEROKU_APP_NAME in the pipeline

Step 10: On your heroku dashboard (overview tab), click on Configure-Add-ons and search for Cleardb, choose the Ignite(free) or any other Pricing plan you wish. NOTE: YOU WILL BE REQUIRED TO ADD YOUR DEBIT CARD DETAILS BEFORE THIS CAN BE APPROVED.

All your db credentials will be created including database name, username and password.

For example, from this url: mysql://b6de34rj38adfad3:a10cd36db@us-cdbr-iron-east-05.cleardb.net/heroku_c40fa7a488382ef?reconnect=true

the string between mysql:// and : is your DB_USERNAME i.e b6de34rj38adfad3

the string between : and @ is your DB_PASSWORD i.e a10cd36db

the string between @ and the next / is your DB_HOST i.e us-cdbr-iron-east-05.cleardb.net

and the string between / and ? is your DB_DATABASE i.e heroku_c40fa7a488382ef

You can set the above in your env variable, and your laravel app should be connected to the database just fine.

You can use a tool like HeidiSQL or MySQL Workbench to view and manage your database.

You can use a tool like HeidiSQL or MySQL Workbench to view and manage your database.

That’s it. Now, anytime you make changes to your app locally, updating your heroku app through the pipeline is as easy as:

git add .

git commit -m "commit message"

git push origin master

and voila!!, your online app is updated.

Note: On heroku, you won’t have access to your *larave.log* file, so in order to be able to see your app-generated logs, you’ll need to direct your app’s logs to heroku’s logs streams. You can do that by setting the .env variable APP_LOG to errorlog. i.e

heroku config:set APP_LOG=errorlog

Then you’d be able to view your logs by running:

heroku logs

Friendly warning: the heroku logs can be sometimes overwhelming, so you just have to take your time to sieve through and find what you’re looking for 😁

Update: To run your regular artisan commands on heroku, precede all statements with heroku run e.g heroku run php artisan migrate . You can also do this in heroku dashboard of your app.

Congratulations. You have just deployed a laravel app on heroku using bitbucket pipline.

--

--