Laravel Tutorials

Speed up your CI builds with Airdrop

Published
Speed up your CI builds with Airdrop image

I'm on a mission to use NodeJS the least amount possible. Why? Because it slows down people's builds on Chipper CI!

Luckily, Airdrop exists.

Laravel Airdrop is a tool that stores your built static assets. When you do CI runs, Airdrop checks to see if your static assets have changed.

If the assets have not changed, Airdrop downloads them from storage and places in the right place - allowing you to skip building your assets.

If they have changed, then you use Node to build your static assets as usual.

This is great for saving time in CI and deployment.

Here's how to use Airdrop.

Install

Installing Airdrop is easy:

composer require hammerstone/airdrop
 
# Add config/airdrop.php to your project
php artisan airdrop:install

Configuring Airdrop

There's only a few things to configure in Airdrop - and a bunch of them can be left to their defaults.

Triggers

You can tell Airdrop when to decide to re-build static assets.

Triggers are specific to the environment (APP_ENV) - each environment gets its own set of files. This is called the Configuration trigger.

The other trigger is the FileTrigger. This tracks configured files and rebuilds assets if the files have changed.

The FileTrigger will check:

  1. Files in the resources (CSS, JS, etc)
  2. If the Webpack/Vite configuration file changes

I also add the package-lock.json file that NPM produces.

<?php
 
use Hammerstone\Airdrop\Drivers\FilesystemDriver;
use Hammerstone\Airdrop\Drivers\GithubActionsDriver;
use Hammerstone\Airdrop\Triggers\ConfigTrigger;
use Hammerstone\Airdrop\Triggers\FileTrigger;
 
return [
'driver' => env('AIRDROP_DRIVER', 'default'),
'drivers' => [
'default' => [...],
'github' => [...],
],
'triggers' => [
ConfigTrigger::class => [
'env' => env('APP_ENV')
],
FileTrigger::class => [
'include' => [
resource_path(), // default
base_path('webpack.mix.js'), // mix default
base_path('vite.config.js'), // vite default
base_path('package-lock.json'), // my addition here
],
],
],
'outputs' => [...],
];

Drivers

You can decide where Airdrop will store static assets. Usually you just use the FilesystemDriver here, and use Laravel's Storage mechanism to tell Airdrop where to put the files.

The disk setting related to the Laravel “disk” storage used. Using some remote storage, such as s3, is recommended.

The GitHub Actions Driver lets you save files to the GH Actions cache, which is pretty handy!

return [
// The driver you wish to use to stash and restore your files.
'driver' => env('AIRDROP_DRIVER', 'default'),
 
'drivers' => [
'default' => [
// The class responsible for implementing the stash and restore
// logic. Must extend BaseDriver.
'class' => FilesystemDriver::class,
 
// The disk on which to store the built files.
'disk' => env('AIRDROP_REMOTE_DISK', 's3'),
 
// The folder (if any) where you'd like your stashed assets to reside.
'remote_directory' => env('AIRDROP_REMOTE_DIR', 'airdrop'),
 
// A writeable directory on the machine that builds the assets.
// Used to build up the ZIP file before stashing it.
'local_tmp_directory' => env('AIRDROP_LOCAL_TMP_DIR', storage_path('framework')),
 
// The skip file is an empty file that will be created to
// indicate that asset building can be skipped.
'skip_file' => env('AIRDROP_SKIP_FILE', base_path('.airdrop_skip')),
],
],
// ...
];

Outputs

Outputs are the files that Airdrop will store and retrieve for you. The files you “watch” via Triggers don't necessarily need to be the same files you have Airdrop save/store for you!

The defaults for Airdrop are pretty good, but of course you can configure these as needed.

return [
// ...
'outputs' => [
/*
* Files or folders that should be included.
*/
'include' => [
// Mix/Webpack
public_path('mix-manifest.json'),
public_path('css'),
public_path('js'),
 
// Vite
public_path('build/manifest.json'),
public_path('build/assets'),
],
 
// ...
],
];

Note: You likely don't want to commit static assets to your repository when using Airdrop. To avoid that, and to finish configuring Airdrop, add the following to your .gitignore file:

/.airdrop_skip
 
# Mix/Webpack
public/css/*
public/js/**
 
# Vite
public/build/*

Integrating Airdrop

When you build your app Airdrop in CI, or in a deployment script (this works great for Forge quick deploys!), you can run the following:

# Download files, only if needed
php artisan airdrop:download
 
# Airdrop creates .airdrop_skip if
# it downloaded files
if [ ! -f ".airdrop_skip" ]; then
npm ci --no-audit
npm run dev
fi
 
# Upload the files if needed
php artisan airdrop:upload
Chris Fidao photo

Teaching coding and servers at CloudCasts and Servers for Hackers. Co-founder of Chipper CI.

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article