Related blogs
Now all we need to do is create the JavaScript file you see mentioned on line 15 to extract the branch name from the string passed to it. For example, we can pass the entire branch name such as feature-db/example-branch and this script would extract the example-branch portion so we can then use it as the branch name on PlanetScale.
To set this script up, create a new file called extract-branch-name.js inside a scripts folder in the root of your repository and add the below code to it.
With this JavaScript file created, we’ve configured all of the scripts required for the pipeline to run so all we need to do is configure our Bitbucket Pipeline step and the environment variables for it to function so let’s do that next.
Bitbucket Pipeline & ENVs
To add the step to our Bitbucket pipeline, we can add the below code to our bitbucket-pipelines.yml file under the branches trigger.
This configuration will trigger the create branch script every time a branch is created or pushed to that starts with feature-db, this is why earlier we needed to update the create-db-branch function to not fail if we try to create an existing branch which would cause excess failures in our pipeline logs.
With our pipelines now configured, all we need to do is add in our environment variables on Bitbucket for the scripts to function correctly. To do that, head to “Repository Settings” and then “Repository Variables” for your repository. Then you’ll want to add in the below variables.
You’ll need to create a PlanetScale Service Token to perform the actions we’ve configured and run the CI scripts. To do this, you can follow their guide here.
- DB_NAME: Name of your database on PlanetScale.
- ORG_NAME: Name of your oragnization on PlanetScale.
- PLANETSCALE_SERVICE_TOKEN: The value of your PlanetScale Service Token.
- PLANETSCALE_SERVICE_TOKEN_ID: The ID of your PlanetScale Service Token.
After adding these variables, everything should be configured and we can test that everything works!
Testing
To test if everything has been setup correctly, create a new branch for your repository by using the Bitbucket UI or by creating one locally and pushing it. Ensure your new branch is using the pre-defined naming structure, in our case this is feature-db/YOUR_BRANCH_NAME.
Then after your branch has been created, watch your pipeline run and hopefully if all goes to plan, a new branch should have been created on PlanetScale using the name of your git branch. If this is the case then everything is working as expected!
Closing Thoughts
In this post, we’ve covered how to configure a Bitbucket repository to use Bitbucket pipelines to create dedicated PlanetScale database branches based on if the source git branch uses a pre-defined naming structure. This aids us in not going above the 5 development branches and needing to upgrade to a more expensive plan just for a higher allowance.
I hope you found this post helpful.
Tech Blog #4: How to version all packages synchronously, in a monorepo using Lerna
Today we are setting up automatic versioning of packages inside of a monorepo using Lerna. There are many ways in which you can do this with different technologies...
Tech Blog #4: How to version all packages synchronously, in a monorepo using Lerna
Today we are setting up automatic versioning of packages inside of a monorepo using Lerna. There are many ways in which you can do this with different technologies...