1

I am deploying my Laravel application to Heroku. I set up a pipeline for my application connecting to GitHub. I configured it to deploy again automatically whenever a new commit found on the develop branch. The thing is I need to configure to run some other commands on each deployment, for example to migrate my database.

I cannot find a option to set up those commands in Heroku UI. How can I do that?

enter image description here

I found Heroku's release phase, but I would like to configure this through the Heroku dashboard. With a release phase I have to put the commands in the Procfile. It is hard to run and manage the commands based on the environment.

For example, in my testing environment I might want to run php artisan migrate --seed, but in my production environment I might run my migrations without seeding the database.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372
  • What do you mean by Migration? Database Migration or something else. – ParthS007 Dec 22 '18 at 22:42
  • php artisan migrate – Wai Yan Hein Dec 22 '18 at 22:45
  • Maybe other custom commands – Wai Yan Hein Dec 22 '18 at 22:45
  • Possible duplicate of [How to use php artisan migrate command of Laravel4 in Heroku?](https://stackoverflow.com/questions/20243373/how-to-use-php-artisan-migrate-command-of-laravel4-in-heroku) – ParthS007 Dec 22 '18 at 22:46
  • U do not get my question. I want to run a set of configured commands on each new deployment. I am not asking how to run artisan on Heroku. – Wai Yan Hein Dec 22 '18 at 22:52
  • You may prefer to do this through the UI, but I don't think that's an option. The `Procfile` release phase method works very well, and I've added an answer accordingly. Why would you want to change the release command based on environments? Don't you want to migrate your database in each one? – ChrisGPT was on strike Dec 23 '18 at 02:00

1 Answers1

3

You may prefer to do this through the web dashboard, but I don't think that's an option. The Procfile release phase method works quite well.

Add a release process to your Procfile, e.g.

web: vendor/bin/heroku-php-apache2 public/
release: php artisan migrate

Release phase enables you to run certain tasks before a new release of your app is deployed. Release phase can be useful for tasks such as:

  • Sending CSS, JS, and other assets from your app’s slug to a CDN or S3 bucket
  • Priming or invalidating cache stores
  • Running database schema migrations

If a release phase task fails, the new release is not deployed, leaving your current release unaffected.

Edit: A release command isn't a good fit for continuous integration. A better solution would be to use a proper CI tool. Heroku does provide one, but you can use a third-party CI tool if you'd prefer.

To use Heroku's CI tool you need to enable Heroku CI in the pipeline's settings and add or update a file called app.json in your repository root. Within the app.json file you can configure the testing environment, which can be a lot more complex than simply seeding your database. For example, you might include Heroku addons to provide a Redis node or set custom environment variables.

For PHP your require-dev dependencies will automatically be installed in your test environment. You can seed your database in the test-setup script and define your test command in the test script.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • Hi Chris thanks for the help. I edited my question because I found Heroku release. But I am still looking for a way to do it from the Heroku dashboard. Is it possible? – Wai Yan Hein Dec 23 '18 at 02:20
  • @WaiYanHein, as I said in my answer and in a comment, I don't believe it's possible to do through the dashboard. I ask again: why would you want to change the release command based on environments? Don't you want to run your database migrations in each environment? – ChrisGPT was on strike Dec 23 '18 at 02:30
  • But for testing environment, I might want to run this, php artisan migrate --seed. With seed. For production, I might run migration without seed – Wai Yan Hein Dec 23 '18 at 12:07
  • @WaiYanHein, ah, well testing is different. I wouldn't use a release command for that, I'd use a CI tool. Heroku does provide one, and I'll update my answer accordingly. – ChrisGPT was on strike Dec 23 '18 at 13:22
  • Why is a release command not a good fit for CI? – Eskay Amadeus Jan 30 '22 at 21:12