2
  1. I installed a Laravel project on AWS Elastic Beanstalk
  2. I created an env on the server
  3. I run php artisan key:generate command and filled all my env variables

This works fine and my application starts. But then I push some other work on my server through my CI pipeline, but it automatically deletes the env file.

Also note that I have already added my env in .gitignore so it's not pushing my env.

I think it's because Elastic Beanstalk automatically deletes old files and create new files whenever new build is released.

following is my script section of composer.json:

    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true,
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    }
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
  • Are you using Laravel with `composer`? Are you receiving the following error: `RuntimeException No application encryption key has been specified.`? If you are using composer key generation is likely being taken care of automatically for you via `post-create-project-cmd` or similar. [See here for more details](https://stackoverflow.com/a/58056361/1720873) – dusthaines May 27 '21 at 18:23
  • yes this error is shown but because env gets deleted so naturally app key is deleted as well. can you tell me how i can stop env from being deleted? – Muhammad Mubashir May 28 '21 at 05:22
  • Are you using `composer`? If so, can you update your question to include an example of the `scripts` section of your `composer.json` file? It should include an entry similar to this: `"post-create-project-cmd": ["php artisan key:generate"]`. [You can learn more about composer scripts here](https://getcomposer.org/doc/articles/scripts.md) – dusthaines May 28 '21 at 12:39
  • yes i am including my composer file script section in my question @dusthaines – Muhammad Mubashir Jun 01 '21 at 07:54

1 Answers1

1

I found that I need to either create the env every time I make a new release or I can set env variables in ebextension config settings given by AWS, so I ended up setting env variables to a config file inside ebextension.

So it is normal that, whenever you send a new release through a pipeline, the env is deleted since it's included in git ignore.

TylerH
  • 20,799
  • 66
  • 75
  • 101