Deploy Docker environment to Heroku with git push
Since I used Heroku for the first time, I summarized the method
After pushing the change to github, it can be automatically deployed to Heroku. This time it is Laravel, but I think that you can use it in other languages by changing the Buildpack language and environment variables.
- mac OS Catalina 10.15.6
- Docker version 19.03.12
- Laravel 7.22.4
- Heroku CLI 7.42.8
Laravel Docker environment (created in ↓)
The folder structure looks something like this
. ├── README.md ├── docker │ ├── mysql │ ├── nginx │ └── php ├── docker-compose.yml ├── logs └── src (Laravel project folder) ├── Procfile (will be created later) ├── app ├── artisan ├── bootstrap ├── ...
Laravel application has been created (under /src)
Heroku first introduction
1. Register on Heroku
https://jp.heroku.com/ ![Screenshot 2020-08-22 23.11.15.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/649853/2d032ae4-8d88-eb30-d9b5-(9de7203e8e15.png)
2. Heroku CLI installation. Then set it with
Brew tap heroku/brew && brew install heroku
$ heroku auth:login Enter your Heroku credentials: Email: [email address when user registration] Password: [Password for user registration] Logged in as [email address when user registration]
Now you can operate from the command line.
3. Create the app
The name you give here will be part of the URL.
$ heroku create [application name] Creating ⬢ [application name]... done https://git.heroku.com/[application name].git
4. Link with github
From the screen of the created app, specify Github in the
Deployment method of Deply to link.
Automatic Deploy to set automatic deployment with git push.
![Screenshot 2020-08-22 23.14.45.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/649853/19bf4a85-54ec-87b5-c042-(8602cfed6f5e.png)
5. Set environment variables
.env files are usually not included in version control with gitignore.
Therefore, register the environment variables included in
.env to Heroku.
-Two registration methods Reference: https://devcenter.heroku.com/articles/config-vars
heroku config:set DB_CONNECTION=pgsql
Register with the command line as follows.
2. Heroku site
![Screenshot 2020-08-22 13.59.26.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/649853/717ca64c-2f96-8a1b-9cf8-(4ea73da8bb20.png)
Config Vars here to register.
In case of Laravel, be sure to register
6. DB linkage
I used MySQL for local development, but I recommend PostgreSQL on Heroku.
-MySQL 5MB (megabyte) -PostgreSQL 1GB
Since it was a cloud difference, I will use Postgre.
-Add Heroku Postgres
Reference: [https://devcenter.heroku.com/articles/heroku-postgresql](https://devcenter.heroku.com/articles/heroku-postgresql) ```:terminal heroku addons:create heroku-postgresql:hobby-dev ``` Link PostgreSQL with. I have selected a free plan with `hobby-dev`. If you get the following error, ```:terminal $ heroku addons:create heroku-postgresql:hobby-dev ›Error: Missing required flag: ›-A, --app APP app to run command against ›See more help with --help ``` Let's add the name of the app with `-a`. ```:terminal heroku addons:create heroku-postgresql:hobby-dev -a app name ``` When the creation is completed, the URL of this database is automatically registered in the environment variable. The registered environment variables can be checked with the command below. ```:terminal heroku config -a app name ``` Put your DB settings in environment variables. ```:terminal $ heroku config:set DB_CONNECTION=pgsql $ heroku config:set DB_HOST=[hostname] $ heroku config:set DB_PORT=[port] $ heroku config:set DB_DATABASE=[database name] $ heroku config:set DB_USERNAME=[username] $ heroku config:set DB_PASSWORD=[password] ```
7. Create Procfile
Create a file called
Procfile with the contents of ↓ at the root of the project.
web: vendor/bin/heroku-php-apache2 public/
The apache server will now start when heroku starts.
8. git push and deploy!
It seems that you can use the method of the reference site of ↓, but I did not want to do
git subtree push, so I searched for another method.
$ cd projectname $ git add. $ git commit -m'commit message' $ git subtree push --prefix src/ heroku master
I will refer to this article this time.
Use this to make heroku recognize only Laravel apps that you put in a subdirectory (/src). This allows you to deploy ignoring other files in the Docker environment.
Method: First, install this buildpack.
heroku buildpacks:set https://github.com/timanovsky/subdir-heroku-buildpack
Next, add the Heroku official buildpack (PHP this time) so that it will be installed.
heroku buildpacks:add heroku/php
It looks like this on the site.
Specify the folder (src) you want to deploy with environment variables.
heroku config:set PROJECT_PATH=src/
Finally it will be deployed with git push!
Check by accessing
9. Migration & seeding```:terminal
heroku run "php artisan migrate" -a app name heroku run "php artisan db:seed" -a app name ```
This completes the deployment. Thank you.
reference Let’s publish on Heroku The following three points have been changed.
- Cooperation with github
- How to register environment variables
- How to deploy by git push