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 thepscale
CLI.
Use planetscale/setup-pscale-action
to make pscale available within your GitHub Actions.
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 yourPLANETSCALE_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.${{ 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 usepscale branch create
to create a branch that matches your GitHub branch name.
--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 usepscale password create
to generate credentials for your database branch.
DATABASE_URL
which can be used in later steps.
Note
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 usepscale 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 usepscale 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.
env
var so that it can be used in later steps.
Get deploy request diff and comment on pull request
We can usepscale 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.
migration-message.txt
file. And then creates a comment on the pull request that triggered the workflow.
Check for dropped columns
PlanetScale sets acan_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 usepscale 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.