As I went deep into how to use DevOps with PlanetScale, I really wanted to demonstrate how PlanetScale can be used in CI environments so I got the idea to create and publish the first wave of official Actions that our customers could use.
After researching how to create Actions that can be published to the marketplace, I opted to use Docker since already have the PlanetScale CLI published as a Docker image on Docker Hub. I used some previously created helper scripts built in Bash that have some helpful utilities and essentially merged the two together.
The dockerfile
is built using the planetscale/pscale
image as its base, and uses curl
to pull in the pinned version of the helper scripts mentioned above.
# Container image that runs your code
FROM planetscale/pscale:v0.129.0
RUN apk --no-cache add bash jq curl
# Copies your code file from your action repository to the filesystem path `/` of the container
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x entrypoint.sh
# Install the PlanetScale Actions helpers
COPY install-helpers.sh /install-helpers.sh
RUN chmod +x /install-helpers.sh
RUN /install-helpers.sh
# Code file to execute when the docker container starts up (`entrypoint.sh`)
ENTRYPOINT ["/entrypoint.sh"]
A service token and parameters are passed into the container for the Action step to perform whatever operation its intended to do. For instance, this will create a new branch of a PlanetScale database:
#!/bin/bash
create_branch=true
command="pscale branch create $1 $2 --org $3"
if [ -n "$4" ];then
command="$command --from $4"
fi
if [ -n "$5" ];then
command="$command --restore $5"
fi
if [ -n "$6" ];then
command="$command --region $6"
fi
# Check if branch already exists
if [ "true" == "$8" ];then
output=$(eval "pscale branch show $1 $2 --org $3" 2>&1)
exit_status=$?
if [ $exit_status -ne 0 ]; then
create_branch=true
else
create_branch=false
fi
fi
if $create_branch; then
eval $command
ret=$?
if [ $ret -ne 0 ]; then
exit $ret
fi
if [ "true" == "$7" ];then
. /.pscale/cli-helper-scripts/wait-for-branch-readiness.sh
wait_for_branch_readiness 40 "$1" "$2" "$3" 5
fi
else
echo "Branch $2 already exists"
fi
Additional resources: