DEV Community

Cover image for Automating Laravel Tasks with JSON-Based Task Runner
Akshay Joshi
Akshay Joshi

Posted on

Automating Laravel Tasks with JSON-Based Task Runner

As developers, we often perform repetitive tasks that could be automated to save time and reduce the risk of human error.

In this blog post, we'll explore how to automate a series of Laravel tasks using a JSON-based task runner. We'll cover pulling from the main branch, updating Composer dependencies, running migrations, and starting the Laravel application.

Why Automate Tasks?

  • Consistency: Ensures tasks are performed the same way every time.
  • Efficiency: Saves time by reducing manual work.
  • Error Reduction: Minimizes the risk of human error.

Setting Up the Task Runner

To get started, we'll create a tasks.json file that defines the tasks we want to automate. Then, we'll write a shell script to read and execute these tasks.

  1. Define Tasks in tasks.json

Create a tasks.json file in your project directory with the following content:

{
  "version": "2.0.0",
  "tasks": {
    "pull-main": {
      "command": "git pull origin main",
      "args": [],
      "type": "shell",
      "label": "Pull from main branch"
    },
    "composer-update": {
      "command": "composer update",
      "args": [],
      "type": "shell",
      "label": "Update Composer dependencies"
    },
    "migrate": {
      "command": "php artisan migrate",
      "args": [],
      "type": "shell",
      "label": "Run migrations"
    },
    "start-app": {
      "command": "php artisan serve",
      "args": [],
      "type": "shell",
      "label": "Start Laravel application"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

This JSON file defines four tasks: pulling from the main branch, updating Composer dependencies, running migrations, and starting the Laravel application.

  1. Create the Task Runner Script

Next, create a shell script named run-tasks.sh to read and execute the tasks from tasks.json:

#!/bin/bash

# Read tasks.json
TASKS=$(jq -c '.tasks[]' tasks.json)

for TASK in $TASKS; do
  LABEL=$(echo $TASK | jq -r '.label')
  COMMAND=$(echo $TASK | jq -r '.command')

  echo "Running: $LABEL"
  eval $COMMAND

  if [ $? -ne 0 ]; then
    echo "Task failed: $LABEL"
    exit 1
  fi
done

echo "All tasks completed successfully."
Enter fullscreen mode Exit fullscreen mode

This script uses jq, a lightweight and flexible command-line JSON processor, to parse the tasks.json file and execute each task sequentially.

  1. Install jq

Ensure jq is installed on your system. On Ubuntu, you can install it with:

sudo apt-get install jq
Enter fullscreen mode Exit fullscreen mode
  1. Make the Script Executable and Run It

Make the run-tasks.sh script executable and run it:

chmod +x run-tasks.sh
./run-tasks.sh
Enter fullscreen mode Exit fullscreen mode

The script will sequentially execute the tasks defined in the tasks.json file. If any task fails, the script will stop and display an error message.

Benefits of This Approach

  • Modularity: Tasks are defined in a separate JSON file, making it easy to add, remove, or modify tasks.
  • Simplicity: The shell script is straightforward to understand.
  • Reusability: This setup can be reused for different projects with minimal changes.

Conclusion

Automating repetitive tasks in your Laravel project can save time, ensure consistency, and reduce the risk of errors. By defining tasks in a JSON file and using a simple shell script to execute them, you can streamline your workflow and focus on what really matters: building great applications.

Share your thoughts: How do you automate tasks in your development workflow?

Happy coding!

Top comments (0)