Supercharge Laravel Workflow with Git Hooks: Automate Migrations and Caching Only When You Need It

itxshakil

Shakil Alam

Posted on November 10, 2024

Supercharge Laravel Workflow with Git Hooks: Automate Migrations and Caching Only When You Need It

If you’re tired of manually running migrations, clearing caches, and recaching configurations every time you git pull or git merge, Git hooks are here to make your life easier. But rather than running every command every time, let’s make this hook smarter, so it only performs these actions when necessary.

Automate Laravel Workflow with Git Hooks

This post will walk you through setting up a post-merge hook that checks for changes in specific files or folders and only runs migrations, and recaches configuration, views, and routes if something’s been updated. Let’s dive in!

What Are Git Hooks?

Git hooks are scripts that Git runs before or after specific events, like committing or merging code. We’ll use the post-merge hook for our setup, which runs every time you pull or merge new changes into your local repository. This way, your development environment will always be ready to go without you lifting a finger.

Setting Up a Smart Post-Merge Hook

  1. Locate or Create the post-merge File. Navigate to the .git/hooks directory in your project. If you don’t already see a post-merge file, create one there.

  2. Write the Script with Conditions for Each Action. Here’s a smart, optimized script that checks for changes before running commands:

   #!/bin/bash

   echo "Running optimized post-merge hook..."

   # Check for new migration files
   if git diff --name-only HEAD@{1} HEAD | grep -qE '^database/migrations/.*\.php'; then
       echo "New migrations found. Running database migrations..."
       php artisan migrate --force
   else
       echo "No new migrations found. Skipping migration step."
   fi

   # Check for changes in config files
   if git diff --name-only HEAD@{1} HEAD | grep -qE '^config/.*\.php'; then
       echo "Config changes detected. Recaching config..."
       php artisan config:cache
   else
       echo "No config changes detected. Skipping config cache."
   fi

   # Check for changes in view files
   if git diff --name-only HEAD@{1} HEAD | grep -qE '^resources/views/.*\.(php|blade\.php)'; then
       echo "View changes detected. Recaching views..."
       php artisan view:cache
   else
       echo "No view changes detected. Skipping view cache."
   fi

   # Check for changes in route files
   if git diff --name-only HEAD@{1} HEAD | grep -qE '^routes/.*\.php'; then
       echo "Route changes detected. Recaching routes..."
       php artisan route:cache
   else
       echo "No route changes detected. Skipping route cache."
   fi

   echo "Optimized post-merge tasks completed!"
Enter fullscreen mode Exit fullscreen mode

Explanation

  • Database Migrations: This part of the script checks if there are new migration files located in the database/migrations directory. If it finds any, it runs php artisan migrate --force to automatically apply the changes.

  • Config Caching: The script checks for any modifications in the config files. If changes are detected, it runs the command to recache the configuration.

  • View Caching: It examines the resources/views directory for any updates. If there are changes, it will recache the views to ensure the latest versions are used.

  • Route Caching: Lastly, the script looks for modifications in the routes directory. If any route files have been altered, it executes the command to recache the routes.

Make Your Script Executable

To allow Git to execute the post-merge hook script, you need to run the following command:

chmod +x .git/hooks/post-merge
Enter fullscreen mode Exit fullscreen mode

You can even find the code at https://gist.github.com/itxshakil/54845e124959bfb70d040964ba0a0eef

Test It Out!

After setting up your post-merge hook, it's time to see it in action! Whenever you execute a git pull or git merge that includes updates to migration, config, view, or route files, the script will intelligently decide which tasks to run. This means your development environment will remain synchronized without unnecessary processing, enhancing your workflow efficiency.

Key Takeaways

  • *Automate Smartly: * By adding conditions to your Git hook, it only executes migrations, config caching, view caching, or route caching when needed, which saves you valuable time and system resources.

  • Easy to Implement: A few lines of Bash scripting can significantly streamline your setup tasks.

  • Keep Your Environment in Sync with Minimal Effort: This hook ensures your local environment is always up-to-date with the latest changes, all without requiring manual intervention.

Conclusion

Implementing this optimized Git hook can transform your development experience, particularly for projects that receive frequent updates. With just a few lines of code, you establish a workflow that is efficient, automated, and consistently aligned with your latest changes—no more missed migrations or forgotten caches. Give it a try, and let Git take care of the repetitive tasks for you!

Happy coding, and here's to a smoother, smarter development experience!

💖 💪 🙅 🚩
itxshakil
Shakil Alam

Posted on November 10, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related