Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

andywhit's avatar
Level 10

CI/CD Pipeline

Hello all,

I'm deploying to an on-prem sever and am having some trouble with my build pipeline. How do you deal with persisting .env and storage files through deployments?

0 likes
5 replies
LaryAI's avatar
Level 58

When dealing with CI/CD pipelines and deploying to an on-prem server, it's important to ensure that certain files, like your .env and storage files, persist across deployments. Here are some strategies you can use:

  1. Environment Variables: Instead of using a .env file, consider using environment variables. Most CI/CD tools allow you to set environment variables that can be accessed by your application. This way, you don't need to persist the .env file itself.

  2. Separate Configuration Management: Use a configuration management tool or service to manage your environment-specific configurations. Tools like Ansible, Chef, or Puppet can help you manage and deploy configuration files separately from your application code.

  3. Persistent Storage: For storage files, ensure that your storage directory is outside of your deployment directory. This way, when you deploy new code, the storage files remain untouched. You can achieve this by creating a symbolic link from your application to the storage directory.

  4. Deployment Scripts: Use deployment scripts to handle the copying of necessary files. For example, you can write a script that runs after your code is deployed to copy the .env file and any necessary storage files from a secure location.

  5. Version Control: Keep a version-controlled copy of your .env file template (without sensitive data) in your repository. During deployment, you can use this template to generate the actual .env file by filling in the sensitive data from a secure source.

Here's a simple example of how you might handle the .env file using a deployment script:

#!/bin/bash

# Path to the secure location of your .env file
SECURE_ENV_PATH="/path/to/secure/location/.env"

# Path to your application directory
APP_PATH="/path/to/your/application"

# Copy the .env file to the application directory
cp $SECURE_ENV_PATH $APP_PATH/.env

echo ".env file has been copied to the application directory."

For storage files, you might set up a symbolic link like this:

# Path to the persistent storage directory
PERSISTENT_STORAGE="/path/to/persistent/storage"

# Path to the application storage directory
APP_STORAGE="$APP_PATH/storage"

# Remove the existing storage directory if it exists
rm -rf $APP_STORAGE

# Create a symbolic link
ln -s $PERSISTENT_STORAGE $APP_STORAGE

echo "Storage directory has been linked to persistent storage."

By using these strategies, you can ensure that your environment configurations and storage files persist across deployments, making your CI/CD pipeline more robust and reliable.

rdelorier's avatar

What env are you using to ci/cd? There are so many options out there. AWS, GPC, OCI other smaller cloud providers. Each have their own way of doing things.

rdelorier's avatar

@andywhit Hard to say without seeing the script and what deployment strategy you're using.

martinbean's avatar

How do you deal with persisting .env and storage files through deployments?

@andywhit They should be set on the server as environment variables.

For storage, you either need to symlink to your storage folder if you’re deploying versions to different directories, or store files in a storage solution such as S3.

Please or to participate in this conversation.