Logo

dev-resources.site

for different kinds of informations.

Automating Laravel Tasks with JSON-Based Task Runner

Published at
8/7/2024
Categories
automation
laravel
taskrunner
devtips
Author
doozieakshay
Author
12 person written this
doozieakshay
open
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!

devtips Article's
30 articles in total
Favicon
𝐓𝐡𝐞 𝐌𝐢𝐬𝐭𝐚𝐤𝐞𝐬 𝐈 𝐌𝐚𝐝𝐞 𝐀𝐬 𝐚 𝐁𝐞𝐠𝐢𝐧𝐧𝐞𝐫 𝐏𝐫𝐨𝐠𝐫𝐚𝐦𝐦𝐞𝐫
Favicon
Debugging Techniques Every Developer Should Know
Favicon
13 Hidden Windows Productivity Tricks You Should Know
Favicon
Tkinter: Python's Secret Weapon for Stunning GUIs
Favicon
Tips and Tricks for Docker Compose: Leveraging the override Feature
Favicon
Essential Security Practices for Web Developers: Keep Your Code Safe and Sound
Favicon
13 Hidden Windows Productivity Tricks You Should Know
Favicon
Automating Laravel Tasks with JSON-Based Task Runner
Favicon
8 Plugins You Should Add To Your IDE And Why
Favicon
How to Safely Edit a Git Commit Message After Pushing *Demystifying one of the tricky aspects of Git*
Favicon
A Guide to Efficient Problem Solving: Techniques for Tackling Coding Challenges
Favicon
How to improve the PageSpeed score of your Nuxt.js website in 6 steps
Favicon
Top 10 Vue.js Resources For Your Project 🚀
Favicon
How is testing different in monolith and microservices architectures?
Favicon
Top 15 Flutter Tools that you should know
Favicon
Top 10 tools for (not only) multilingual Android development
Favicon
Why allow users to switch languages
Favicon
Top 10 Android Libraries to boost your development in 2022
Favicon
Seven reasons why you should use Microservices architecture
Favicon
TypeScript library for Localazy API
Favicon
Cheatsheet: Getting started with Software Localization
Favicon
My paper to-do strategy
Favicon
Measuring productivity with GitHub issues
Favicon
How to set up `git bro` command with git alias
Favicon
Digital resilience: redundancy for websites and communications
Favicon
CodeSandbox init shortcuts
Favicon
How to become a software developer
Favicon
How to write good documentation
Favicon
So you're the family tech support
Favicon
Transforming an object to array in JavaScript

Featured ones: