Search code examples
amazon-web-servicesamazon-elastic-beanstalkaws-codepipeline

My env gets deleted whenever new release is pushed in a Laravel AWS pipeline


  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"
        ]
    }
}

Solution

  • 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.