Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config:cache for default Laravel configuration creates absolute paths #12263

Closed
progmars opened this issue Feb 12, 2016 · 5 comments
Closed

Config:cache for default Laravel configuration creates absolute paths #12263

progmars opened this issue Feb 12, 2016 · 5 comments

Comments

@progmars
Copy link

I noticed that when I run php artisan config:cache the resulting cache has absolute paths because by default there are some storage_path() calls in Laravel config files.

I have read the documentation:

You should typically run the php artisan config:cache command as part of your production deployment routine.

The documentation does not say that I should run the command on the server and not on my automated CI build agent. But not everyone has access to php artisan config:cache command on their servers for executing it remotely from CI build agent.

I think the documentation should be updated to reflect the requirement to run the command on the server.
However, it would be even better to implement some way to generate config cache with relative paths.

Currently I could add a workaround for the issue and create a PHP controller which executes artisan command, and call it during deployment. But the problem is that on our server deployment is set up to work through Git push and only after a few minutes the web site gets synced with current version in the repository. I don't think it would be a good idea to just sit inside my CI deploy script and wait for unknown time just to be able to call URL to my custom controller which in turn executes php artisan config:cache.

So, we are back to the question - how to make the paths in config files relative to the app root and generate cached config which will work with relative paths?

@GrahamCampbell
Copy link
Member

You need to run composer and our optimization commands on your server, not before deployment.

@progmars
Copy link
Author

In the Laravel docs quote I mentioned above, "deployment routine" does not explicitly mean that the cache command has to be run on the server and not in the folder which is about to be deployed. Therefore I think it is worth to expand this sentence, so people do not make false assumptions (as I did).

Anyway, it's a shame that we cannot use relative paths instead of storage_path() in config files. This would make things much easier for automatic CI deployments.

@shehi
Copy link
Contributor

shehi commented Jun 17, 2018

Yes, that's why documentation says "deployment routine", not "build routine"...

@simonbuehler
Copy link

simonbuehler commented Feb 25, 2022

i had the same problem when deploying to a server that mounts the codebase into a docker container and so the path inside the docker container is different from the one php artisan config:cache generates.

for the ones stranding here from googleling this, i created a small deployer receipt that replaces the absolute paths in config.php with the path as seen from inside the container:

<?php

namespace Deployer;

desc('Run sed to replace absolute paths with docker mappings.');
task('config:fix', function () {
    cd('{{release_or_current_path}}');
    run("sed -i -- 's/\/absolute\/path\/from\/outside\//\/absolute\/inside\/path\//g' ./bootstrap/cache/*");
});

@hilnius
Copy link

hilnius commented Sep 12, 2022

thanks @simonbuehler for the script. For any other person coming there with the same issue, if you use the views:cache build commmand in a build that is not run on the server, you will also have the problem that the views won't have the right filenames. Indeed, laravel seems to generate the filename from the sha1 of the full path of the file, so I wrote a script that renames views so after deployment, the app doesn't need to re-generate the views, they're already generated with the right filenames. This is especially helpful if - like me - your app is deployed on a read-only file system and it's not possible to compile the views at runtime.

Here, i'm building the files in the compiled-views folder, but by default in laravel it will be storage/framework/views

#!/bin/bash

FILES=$(find compiled-views/ -type f)

for file in $FILES; do
  newsha=$(grep PATH ${file} \
    | sed -e 's/^.*PATH \(.*\) ENDPATH.*$/\1/g' \
    | sed -e 's/\/your\/folder\/during\/build\/time\//\/deployed\/folder\//g' \
    | tr -d '\n' \
    | sha1sum - \
    | head -c 40).php
  echo "moving $file -> compiled-views/$newsha"
  mv $file compiled-views/$newsha
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants