Laravel Envoyer部署:能否将当前版本公共路径文件复制到新版本?
Absolutely! You've hit on a common scenario with Laravel + Envoyer deployments, and there are two solid ways to handle preserving user-uploaded files like those in public/uploads:
1. Use Envoyer's Shared Directories (Recommended Approach)
Envoyer has a built-in feature specifically for this kind of persistent content—shared directories. Instead of copying files between releases, this method creates a symlink from each deployment's public/uploads to a single shared directory that lives outside the release folders. Here's how to set it up:
- Head to your project in Envoyer, then navigate to Settings > Shared Folders & Files
- Add
public/uploadsto the list of shared paths - Save the settings. On your next deployment, Envoyer will automatically:
- Create
shared/public/uploadsif it doesn't exist - Symlink your new release's
public/uploadsto the shared directory
- Create
- From then on, all releases will use the same shared
uploadsdirectory—no copying needed, and files persist across deployments automatically.
2. Custom Deployment Script to Copy Files
If you specifically need to copy files (rather than use symlinks), you can add a custom hook to your deployment workflow. Here's how:
- Go to your project in Envoyer, then go to Deployment Hooks
- Choose the stage where you want the copy to run—Before Activate New Release is a good spot (so files are in place before the new version goes live)
- Add a bash script like this:
# Copy uploads from the current live release to the new release cp -R {{ release }}/../current/public/uploads/* {{ release }}/public/uploads/ # Optional: Ensure correct file permissions for the web server chown -R www-data:www-data {{ release }}/public/uploads
A quick breakdown of the variables:
{{ release }}is an Envoyer variable that points to the directory of the new release being deployed../currentnavigates to the symlink pointing to your currently active live release
A Quick Note on Which to Choose
The shared directory method is almost always better—it's faster (no file copying overhead), cleaner, and aligns with how Envoyer is designed to handle persistent assets. Copying files works, but can slow down deployments if you have a lot of uploads, and introduces more points where permissions or file conflicts could pop up.
内容的提问来源于stack exchange,提问作者Felix

