Title: A post-autoload-dump script terminated with an error
Tags: php;laravel;heroku
Question: I'm creating an API with laravel. I have used laravel passport for api authentication.
I am getting this error while deploying my code to heroku.
I used git for depploy in heroku. When I run git push heroku master I am getting this error.
remote:        Generating optimized autoload files
remote:        &gt; Illuminate\Foundation\ComposerScripts75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0postAutoloadDump
remote:        &gt; @php artisan package:discover --ansi
remote:
remote:        In CryptKey.php line 52:
remote:
remote:          Key path &quot;file:///tmp/build_a8bd6864/storage/oauth-private.key&quot; does not ex
remote:          ist or is not readable
remote:
remote:
remote:        Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1
remote:  !     WARNING: A post-autoload-dump script terminated with an error
remote:
remote:  !     ERROR: Dependency installation failed!
remote:  !
remote:  !     The 'composer install' process failed with an error. The cause
remote:  !     may be the download or installation of packages, or a pre- or
remote:  !     post-install hook (e.g. a 'post-install-cmd' item in 'scripts')
remote:  !     in your 'composer.json'.
remote:  !
remote:  !     Typical error cases are out-of-date or missing parts of code,
remote:  !     timeouts when making external connections, or memory limits.
remote:  !
remote:  !     Check the above error output closely to determine the cause of
remote:  !     the problem, ensure the code you're pushing is functioning
remote:  !     properly, and that all local changes are committed correctly.
remote:  !
remote:  !     For more information on builds for PHP on Heroku, refer to
remote:  !     https://devcenter.heroku.com/articles/php-support
remote:  !
remote:  !     REMINDER: the following warnings were emitted during the build;
remote:  !     check the details above, as they may be related to this error:
remote:  !     - A post-autoload-dump script terminated with an error
remote:
remote:  !     Push rejected, failed to compile PHP app.
remote:
remote:  !     Push failed
remote: !
remote:  ! ## Warning - The same version of this code has already been built: 2084aa011f3c0e5b395af053a556d46d7d1e3a1f
remote:  !
remote:  ! We have detected that you have triggered a build from source code with version 2084aa011f3c0e5b395af053a556d46d7d1e3a1f
remote:  ! at least twice. One common cause of this behavior is attempting to deploy code from a different branch.
remote:  !
remote:  ! If you are developing on a branch and deploying via git you must run:
remote:  !
remote:  !     git push heroku <branchname&gt;:main
remote:  !
remote:  ! This article goes into details on the behavior:
remote:  !   https://devcenter.heroku.com/articles/duplicate-build-version
remote:
remote: Verifying deploy...
remote:
remote: !   Push rejected to minute-man-api.
remote:
To https://git.heroku.com/minute-man-api.git
! [remote rejected] master -&gt; master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/minute-api.git'
I am getting this error while deploying my code to heroku.
I used git for depploy in heroku. When I run git push heroku master I am getting this error.
Here is composer.json file.
&quot;name&quot;: &quot;laravel/laravel&quot;,
&quot;type&quot;: &quot;project&quot;,
&quot;description&quot;: &quot;The Laravel Framework.&quot;,
&quot;keywords&quot;: [
&quot;framework&quot;,
&quot;laravel&quot;
],
&quot;license&quot;: &quot;MIT&quot;,
&quot;require&quot;: {
&quot;php&quot;: &quot;^7.3&quot;,
&quot;fideloper/proxy&quot;: &quot;^4.2&quot;,
&quot;fruitcake/laravel-cors&quot;: &quot;^2.0&quot;,
&quot;guzzlehttp/guzzle&quot;: &quot;^7.0&quot;,
&quot;laravel/framework&quot;: &quot;^8.0&quot;,
&quot;laravel/nexmo-notification-channel&quot;: &quot;^2.4&quot;,
&quot;laravel/passport&quot;: &quot;^10.0&quot;,
&quot;laravel/tinker&quot;: &quot;^2.0&quot;
},
&quot;require-dev&quot;: {
&quot;facade/ignition&quot;: &quot;^2.3.6&quot;,
&quot;fzaninotto/faker&quot;: &quot;^1.9.1&quot;,
&quot;mockery/mockery&quot;: &quot;^1.3.1&quot;,
&quot;nunomaduro/collision&quot;: &quot;^5.0&quot;,
&quot;phpunit/phpunit&quot;: &quot;^9.3&quot;
},
&quot;config&quot;: {
&quot;optimize-autoloader&quot;: true,
&quot;preferred-install&quot;: &quot;dist&quot;,
&quot;sort-packages&quot;: true
},
&quot;extra&quot;: {
&quot;laravel&quot;: {
&quot;dont-discover&quot;: []
}
},
&quot;autoload&quot;: {
&quot;psr-4&quot;: {
&quot;App\\&quot;: &quot;app/&quot;,
&quot;Database\\Factories\\&quot;: &quot;database/factories/&quot;,
&quot;Database\\Seeders\\&quot;: &quot;database/seeders/&quot;
}
},
&quot;autoload-dev&quot;: {
&quot;psr-4&quot;: {
&quot;Tests\\&quot;: &quot;tests/&quot;
}
},
&quot;minimum-stability&quot;: &quot;dev&quot;,
&quot;prefer-stable&quot;: true,
&quot;scripts&quot;: {
&quot;post-autoload-dump&quot;: [
&quot;Illuminate\\Foundation\\ComposerScripts75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0postAutoloadDump&quot;,
&quot;@php artisan package:discover --ansi&quot;
],
&quot;post-root-package-install&quot;: [
&quot;@php -r \&quot;file_exists('.env') || copy('.env.example', '.env');\&quot;&quot;
],
&quot;post-create-project-cmd&quot;: [
&quot;@php artisan key:generate --ansi&quot;
]
How solve this?
Thanks for your time.
Comment: can you put your `composer.json` file ?

Comment: I'm having the same issue. Did you resolve it?

Comment: @KamleshPaul I have added composer.json file. Please check.

Here is the accepted answer: In AuthServiceProvider.php you should not check if the key exists or not in storage.
And in .gitinnore file comment /storage/*.key line like this # /storage/*.key
Then after deploying to heroku generate passpot key.
php artisan passport:keys

Here is another answer: This answer is based on another I posted on another question. To reiterate what I posted there, the following is a hack.
Since I don't think you're able to alter the heroku initialisation without creating your own custom buildpack you'll need to rely on the scripts. Add the following in your scripts section of composer.json:
&quot;scripts&quot;: {
&quot;post-autoload-dump&quot;: [
&quot;Illuminate\\Foundation\\ComposerScripts75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0postAutoloadDump&quot;,
&quot;if test -f 'storage/oauth-public.key'; then @php artisan package:discover --ansi; fi&quot;
],
&quot;post-root-package-install&quot;: [
&quot;@php -r \&quot;file_exists('.env') || copy('.env.example', '.env');\&quot;&quot;
],
&quot;post-create-project-cmd&quot;: [
&quot;@php artisan key:generate --ansi&quot;
],
&quot;post-install-cmd&quot;: [
&quot;Illuminate\\Foundation\\ComposerScripts75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0postInstall&quot;,
&quot;ssh-keygen -t rsa -b 4096 -f ./storage/id_rsa &amp;&amp; mv ./storage/id_rsa ./storage/oauth-private.key &amp;&amp; mv ./storage/id_rsa.pub ./storage/oauth-public.key&quot;,
&quot;@php artisan clear-compiled&quot;,
&quot;@php artisan optimize&quot;,
&quot;@php artisan migrate --force&quot;,
&quot;@php artisan passport:install --force&quot;
],
To explain: post-autoload-dump runs first so there's a check to make sure the key files exist before running package:discover. Next post-install-cmd will run and this creates some temporary key files to get everything running before then running everything to install passport.
I experienced a second issue where it couldn't run because there wasn't a valid APP_KEY so you might need to add an .env file to your repository containing a key generated on another machine. You should change that once everything is up and running as having an app key off the machine is a security risk.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:10/21/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><laravel><heroku>', 'Title': 'A post-autoload-dump script terminated with an error', 'ViewCount': 3313, 'id': 64456175, 'user_AboutMe': "<p>Hi, I'm Niloy Quazi, professional web application developer.</p>\n", 'user_AccountId': 8705225.0, 'user_DisplayName': 'Niloy Quazi', 'user_Reputation': 87.0, 'user_Views': 29.0, 'user_id': 7860085.0}}