Skip to content

Instantly share code, notes, and snippets.

@tormjens
Created February 22, 2019 09:25
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tormjens/3209d40aaa99242ba097abddcb38ec42 to your computer and use it in GitHub Desktop.
Save tormjens/3209d40aaa99242ba097abddcb38ec42 to your computer and use it in GitHub Desktop.
Laravel Envoyer – conditional webpack/npm run

Laravel Envoyer - Diffed conditional webpack run/npm install

At work we use Envoyer to build our assets as part of our deployment. This has removed a lot of the headaches related to merge conflicts.

However, due to this, deployment takes a long time. Even when you just deploy a update to a controller or some other things.

We use these deployment hooks to run npm install and npm run production only if there's been changes to the source files.

The hooks should work as long as your assets are in resources/assets (which was the default up to Laravel 5.7).

diff -q {{ project }}/current/resources/assets {{ release }}/resources/assets 1>/dev/null
if [[ $? == "0" ]]
then
echo "Skipping 'npm install' since resources is equal to current release."
else
echo "Running 'npm install'"
cd {{ release }}
npm install
fi
diff -q {{ project }}/current/resources/assets {{ release }}/resources/assets 1>/dev/null
if [[ $? == "0" ]]
then
echo "Skipping 'npm run production' since resources is equal to current release. Copying public folder."
/bin/cp -rf {{ project }}/current/public {{ release }}
else
echo "Running 'npm run production'"
cd {{ release }}
npm run production
fi
@heikokrebs
Copy link

heikokrebs commented May 12, 2020

Hey! Thanks for the idea!

But shouldn't you check the package-lock.json for diff before the npm install?

@tormjens
Copy link
Author

The thing is that you'd need to do both an npm install and a npm run production when assets have changed. Envoyer does not preserve the node_modules folder between deployments.

@heikokrebs
Copy link

@tormjens You're right! Wouldn't it make sense then to check at the npm-install.sh if the package-lock.json has changes – if not we copy the node_modules folder from the (still) current release. If there are any changes, we will run an npm install as usual.

The step for npm run production can then remain as described above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment