Skip to content

Instantly share code, notes, and snippets.

@flybayer
Last active June 6, 2023 14:48
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save flybayer/ff537d4d2a555c95ec60ee3c11e8e541 to your computer and use it in GitHub Desktop.
Save flybayer/ff537d4d2a555c95ec60ee3c11e8e541 to your computer and use it in GitHub Desktop.
name: PlanetScale Migration
on:
pull_request:
types: [opened, synchronize]
paths:
- packages/shared/adapters/prisma/schema.prisma
env:
PLANETSCALE_ORG: flightcontrol
PLANETSCALE_DB: fc-production
PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }}
PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }}
jobs:
branch_and_migration:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v3
# This is used to ensure the create branch & create deploy request actions only run once
- name: Inject slug/short variables
uses: rlespinasse/github-slug-action@v4
with:
prefix: SLUG_
- name: PS creation state
id: ps_created
uses: actions/cache@v3
with:
path: .planetscale-db-created
key: ${{ runner.os }}-ps-created-${{ env.SLUG_GITHUB_HEAD_REF_SLUG }}
- name: Create planetscale branch
uses: planetscale/create-branch-action@v1
id: create_branch
if: ${{ steps.ps_created.outputs.cache-hit != 'true' }}
with:
org_name: ${{ env.PLANETSCALE_ORG }}
database_name: ${{ env.PLANETSCALE_DB }}
branch_name: ${{ env.SLUG_GITHUB_HEAD_REF_SLUG }}
wait: true
- name: Create planetscale deploy request
uses: planetscale/create-deploy-request-action@v1
id: create_deploy_request
if: ${{ steps.ps_created.outputs.cache-hit != 'true' }}
with:
org_name: ${{ env.PLANETSCALE_ORG }}
database_name: ${{ env.PLANETSCALE_DB }}
branch_name: ${{ env.SLUG_GITHUB_HEAD_REF_SLUG }}
# This is used so migrations are only pushed to the branch if schema changes
- name: Save schema hash
id: schema_hash
uses: actions/cache@v3
with:
path: packages/shared/adapters/prisma/schema.prisma
key: ${{ runner.os }}-schema-${{ hashFiles('packages/shared/adapters/prisma/schema.prisma') }}
- name: Create planetscale password
uses: planetscale/create-branch-password-action@v1
id: password
if: ${{ steps.schema_hash.outputs.cache-hit != 'true' }}
with:
org_name: ${{ env.PLANETSCALE_ORG }}
database_name: ${{ env.PLANETSCALE_DB }}
branch_name: ${{ env.SLUG_GITHUB_HEAD_REF_SLUG }}
name: ${{ github.sha }}
env:
PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }}
PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }}
- run: touch .planetscale-db-created
if: ${{ steps.schema_hash.outputs.cache-hit != 'true' }}
- name: Comment PR
uses: thollander/actions-comment-pull-request@v2
if: ${{ steps.ps_created.outputs.cache-hit != 'true' }}
with:
message: |
## PlanetScale
Branch: https://app.planetscale.com/${{ env.PLANETSCALE_ORG }}/${{ env.PLANETSCALE_DB }}/${{ env.SLUG_GITHUB_HEAD_REF }}
Deploy request: https://app.planetscale.com/${{ env.PLANETSCALE_ORG }}/${{ env.PLANETSCALE_DB }}/deploy-requests/${{ steps.create_deploy_request.outputs.number }}
- uses: pnpm/action-setup@v2
if: ${{ steps.schema_hash.outputs.cache-hit != 'true' }}
- name: Set up Node
if: ${{ steps.schema_hash.outputs.cache-hit != 'true' }}
env:
NODE_OPTIONS: "--max_old_space_size=5120"
uses: actions/setup-node@v3
with:
node-version: 16.x
cache: "pnpm"
- name: Get pnpm store directory
if: ${{ steps.schema_hash.outputs.cache-hit != 'true' }}
id: pnpm-cache
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT
- uses: actions/cache@v3
if: ${{ steps.schema_hash.outputs.cache-hit != 'true' }}
with:
path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
key: ${{ runner.os }}-node-v3-${{ hashFiles('pnpm-lock.yaml') }}
restore-keys: ${{ runner.os }}-node-v3-
- run: pnpm install --no-optional
if: ${{ steps.schema_hash.outputs.cache-hit != 'true' }}
- name: Sleep for 30 seconds to let database boot
run: sleep 30s
if: ${{ steps.ps_created.outputs.cache-hit != 'true' }}
- name: prisma migrate
run: |
export DATABASE_URL=mysql://${{ steps.password.outputs.username }}:${{ steps.password.outputs.password }}@${{ steps.password.outputs.hostname }}/${{ env.PLANETSCALE_DB }}?sslaccept=strict
cd packages/flightdeck && pnpm blitz prisma db push
if: ${{ steps.schema_hash.outputs.cache-hit != 'true' }}
@CodeWithShreyans
Copy link

Should probably add a note to change the file path according to one's project

@jrysana
Copy link

jrysana commented Apr 6, 2023

This is awesome, thanks Brandon for sharing!

@b-bot
Copy link

b-bot commented Apr 9, 2023

I think you need to prepend outputs to all your cache-hit statements.
ie. steps.schema_hash.outputs.cache-hit != 'true'

https://github.com/actions/cache#skipping-steps-based-on-cache-hit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment