Skip to main content
See our tech talk on Databases + CI/CD to see pscale + GitHub Actions used in a real application. With GitHub Actions, you can automate the creation of branches and deploy requests all within your CI workflow.

Getting Started

Authentication

Convert GitHub branch name to PlanetScale branch name

Create a PlanetScale branch

Create a password for a branch

Open a deploy request

Get deploy request by branch name

Get deploy request diff and comment on pull request

Check for dropped columns

Submit a deploy request by branch name

Getting started

The best way to use PlanetScale within GitHub Actions is via the pscale CLI. Use planetscale/setup-pscale-action to make pscale available within your GitHub Actions.
The action works with Linux, Windows, and Mac runners. Once installed it will be added to your tool cache for subsequent runs.

Authentication

Authentication for pscale is via service token environment variables. You will need to create a service token. Make sure to give your service token the proper permissions to the database you’ll be using in your workflow. Add your PLANETSCALE_SERVICE_TOKEN_ID and PLANETSCALE_SERVICE_TOKEN to your Actions secrets. In your Actions workflow, you will need to make the secrets available as environment variables.

Examples

The following examples show how to accomplish common Actions workflows with PlanetScale. In each example, notice that we use secrets for the service token, database and organization names. You will need to set them in your GitHub repository to target your own database.

Convert GitHub branch name to PlanetScale branch name

PlanetScale branch names must be lowercase, alphanumeric characters and hyphens are allowed. Since git branch names allow more possibilities, you can use the following code to transform a git branch name into an acceptable PlanetScale branch name.
This makes ${{ env.PSCALE_BRANCH_NAME }} available for use in the rest of the workflow. This is useful to run in any scenario where you are creating a PlanetScale branch to correspond with a git branch.

Create a PlanetScale branch

You can use pscale branch create to create a branch that matches your GitHub branch name.
Notice that we first check if the branch exists. If it does, we do nothing. Otherwise we create it and pass the --wait flag. This is useful when running in CI, as the workflow may run multiple times and you’ll want the branch ready if you are running schema migrations immediately after creating the branch.

Create a password for a branch

You can use pscale password create to generate credentials for your database branch.
This example shows creating the password and getting back a response in json. The json is then parsed to create a DATABASE_URL which can be used in later steps.
pscale password create can also accept a ttl flag which lets you limit the number of minutes the password is valid for.

Open a deploy request

You can use pscale deploy-request create to open a new deploy request from GitHub Actions. This can be useful after running migrations against a branch.

Get deploy request by branch name

You can use pscale deploy-requests show to grab the latest deploy request by branch name. This can be useful when deploying your application. You can first check if there are any deploy requests open for the branch being deployed. If there are, you can trigger the deploy request to run before you deploy your application.
This example also makes the deploy request number available as an env var so that it can be used in later steps.

Get deploy request diff and comment on pull request

We can use pscale deploy-request diff to see the full schema diff of a deploy request. This example is useful when combined with opening a deploy request for a git branch. You can then automatically comment the diff back to the GitHub pull request.
This writes the diff to the migration-message.txt file. And then creates a comment on the pull request that triggered the workflow.

Check for dropped columns

PlanetScale sets a can_drop_data boolean for any schema change that drop a column or table. We can make use of this to emit a warning into our pull requests. In this example, we first wait for the deployment check to be ready. During this time, PlanetScale is examining the schema change, verifying that it is safe and generating the DDL statements to make the change. Once it’s done, we then use this information to put a comment on the deploy request with tips on how to deploy it safely.

Submit a deploy request by branch name

To trigger a deploy, we can use pscale deploy-request deploy. This command will accept either the deploy request number, or the name of the branch that the deploy request was created from. When using with GitHub Actions, it’s often easier to use the branch name. The --wait flag will let the command run until the deployment is complete. This is important if you want your schema change to run before the next step in your workflow.

Need help?

Get help from the PlanetScale Support team, or join our Discord community to see how others are using PlanetScale.