Host Storybook with CircleCI and GitHub Deployments
Lately at company, we had a lot of work with developing UI components using React. It went smooth thanks to Storybook. We hosted files generated by Storybook for each push to branch which was really helpful for quality assurance process.
Storybook is a tool for developing UI components in isolation. While this tool is useful for local development, it’s also possible to build a static version of Storybook and host it. I’ll show how to configure a deploy for each push made to repository.
You will learn how to build Storybook on CircleCI and use it as a hosting. You will also learn how to use GitHub Deployments. Deployments are requests to deploy a specific branch, commit, tag. External services can listen for those requests and act.
This guide assumes that you have initialized Storybook using @storybook/cli
. If not, go here to learn how to do it.
TL;DR: Here is a repository with whole process configured. List of deployments can be viewed here and deployment assigned to pull request can be viewed here.
Whole process looks like this:
- Make a push to repository
- CircleCI build is triggered
- GitHub Deployment is created
- Install dependencies
- Build storybook
- Save generated files as CircleCI artifacts
- If whole process was successful, add success deployment status
- If whole process was not successful, add error deployment status
- We can see link to generated files on deployments page
- We can see link to generated files in related pull request
Setting up CircleCI
Go to CircleCI Dashboard and add your project. Start the build process - it will fail at first but we will fix it in next steps.
Create CircleCI config file
In your git repository, create .circleci/config.yml
:
1 | version: 2.1 |
There are 3 parts that are related to creating and adding status updates of GitHub Deployments. This command will create a Deployment and save it’s id to deployment
file. Deployment will be visible in related pull request as pending.
1
2
3- run:
name: Create GitHub Deployment
command: ./tasks/deployment/start.sh > deployment
Only one of other two commands will execute. Execution is based on the status of whole build.
1
2
3
4
5
6
7
8- run:
name: Add GitHub Deployment success status
command: ./tasks/deployment/end.sh success
when: on_success
- run:
name: Add GitHub Deployment error status
command: ./tasks/deployment/end.sh error
when: on_fail
Create deployment scripts
Now create 2 files:tasks/deployment/start.sh
- this will create a GitHub Deployment.
1 |
|
tasks/deployment/end.sh
- this will update Deployment status to success or error.
1 |
|
It might be necessary to update scripts file mode to executable:
1 | git update-index --add --chmod=+x ./tasks/deployment/start.sh |
Configure GitHub access token
Go to https://github.com/settings/tokens and create a new access token. Required scopes:
repo:status
repo_deployment
public_repo
Copy new token and go to Environment Variables
configuration section in CircleCI project. If you can’t find it, use this url, just replace GITHUB_USERNAME and REPOSITORY_NAME with valid values:
1 | https://circleci.com/gh/GITHUB_USERNAME/REPOSITORY_NAME/edit#env-vars |
On CircleCI add variable:
1 | name: GITHUB_DEPLOYMENTS_TOKEN |
Result
Now whenever you push a new commits to your repository, you will get a storybook hosted on CircleCI. The link to storybook will be added to repository deployments page and to the related pull request.
Bonus
Are you working in company? Create a company github bot account and use it’s personal access token to deploy. Customize it’s name and avatar.