Upload fixture files to Cloudcube on every review app creation

Upload fixture files to Cloudcube on every review app creation

The automatic creation of review apps is one of the biggest advantages of using Heroku. But what about using files in your S3 bucket? I've build a Symfony console command to upload files automatically to Cloudcube through flysystem.

The command receives a path to a directory with fixture files and copies them over to the S3 storage. Although the command is structured to not only work with S3 but every flysystem storage configuration you supply to it.

<?php

declare(strict_types=1);

namespace App\Command\Review;

use League\Flysystem\FilesystemInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;

final class CopyFixturesCommand extends Command
{
    protected static $defaultName = 'app:review:copy-fixtures';

    private string $fixturesDirectory;
    private FilesystemInterface $storage;

    public function __construct(
        string $fixturesDirectory,
        FilesystemInterface $defaultStorage
    ) {
        parent::__construct();

        $this->fixturesDirectory = $fixturesDirectory;
        $this->storage = $defaultStorage;
    }

    protected function configure(): void
    {
        $this->setDescription('Copies fixture files from review folder to S3 on heroku for review apps');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $finder = new Finder();
        $finder->files()->in($this->fixturesDirectory);

        foreach ($finder as $file) {
            $fileIncludingPath = sprintf('%s/%s', $file->getRelativePath(), $file->getFilename());
            $this->storage->put($fileIncludingPath, $file->getContents());
        }

        $output->writeln('Fixture files transferred');

        return 0;
    }
}
CopyFixturesCommand.php

I've set the directory to /heroku/review/storage and added the following line to /heroku/review/setup-review.sh:

php bin/console app:review:copy-fixtures
setup-review.sh

Within my app.json file I trigger the setup-review.sh script in the postdeploy process.

{
  "environments": {
    "review": {
      "scripts": {
        "postdeploy": "heroku/review/setup-review.sh"
      }
      ...
    }
  }
}
app.json

This process is run after the release phase of the review app but only ones after the initial build of the review app.