Allow .json stories to be generated without bootstraping Drupal in CICD environment

Created on 9 May 2024, 7 months ago
Updated 15 July 2024, 4 months ago

Problem/Motivation

Since *story.json files are generated files, we do not want to commit them to the repo but instead, while running our CICD process and building a site for deployment, we would like to generate .json stories. However, `storybook:generate-all-stories` requires a full Drupal environment. (We do not want to require constructing a full Drupal environment for each code change just to generate stories.)

Proposed resolution

Make the `storybook:generate-all-stories` drush command available globally and remove full-Drupal-environment dependencies from that command so that we could call `vendor/bin/drush storybook:generate-all-stories` directly during our build process.

✨ Feature request
Status

Active

Version

1.0

Component

Storybook

Created by

πŸ‡ΊπŸ‡ΈUnited States bradwade

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @bradwade
  • πŸ‡ΊπŸ‡ΈUnited States devanbicher

    I worked on this issue on the backend with @bradwade.
    I am commenting here to suggest closing this issue and to warn anyone about going down this development path.

    Our intended outcome was to have changes to Single Directory components to be automatically compiled into the appropriate storybook stories for use on one our of development environments. Ideally we didn't want to have to commit the storybook story files into our repository for cleanliness and consistency. Additionally these files are not used in our production environment, just in a development environment that is setup to have storybook available.

    I spent time abstracting the associated drush command, storybook:generate-all-stories, in this module into a custom, globally available drush command so that we could run this command in our github workflow that builds the storbook stories, without bootstrapping drupal. In theory nothing in the drush command file StorybookCommands.php requires drupal after some tweaking to the code.
    For example you can change Drupal\Core\Url::fromUri to a static url if you know what it is ahead of time, which you would if you already have this setup for an environment. Similarly \Drupal::root() can be converted to Drush::bootstrapManager()->getRoot().

    However after much trial and error it turns out that somewhere along the dependency injection pipeline the StoryRenderer object needs Drupal and this storybook module enabled to setup the twig environment that is also used to construct the storybook files. So ultimately drupal and this module is needed to run the storybook:generate-all-stories command even if you move the code for the command into a custom drush command outside the drupal root.

    We still chose to not include the storybook story files in our repository. Instead we created a github workflow that is triggered on adding a specific, storybook, label to a Pull Request. In this way any pull request that updates components used in storybook will trigger a workflow that, amongst other standard tasks in for our PR's:

    1. spins up drupal with ddev
    2. installs a new site with a standard installation
    3. enables this storybook module
    4. runs the storybook:generate-all-stories drush comand

    In this way we avoid boostrapping drupal unnecessarily in a workflow specifically needed only for storybook updates, on every PR. While also still minimizing the overhead for generating these files in a github workflow, and not commiting the story files into the repo.

    Hopefully this helps any developer or team who might want to have a similar outcome to ours.

Production build 0.71.5 2024