GithubHelp home page GithubHelp logo

jonico / pscale-workflow-helper-scripts Goto Github PK

View Code? Open in Web Editor NEW
77.0 4.0 8.0 160 KB

Workflows and helper scripts around the PlanetScale DB workflow to automate database branch creation, association, update and merge directly out of your pull/merge request or favourite CI/CD. This is just a passion project, no official repository from Planetscale.

Home Page: https://youtu.be/l3oe7srRUZw?t=610

Shell 100.00%
planetscale cli helper synchronous wait credentials actions zerosetup issueops pull-requests

pscale-workflow-helper-scripts's Introduction

PlanetScale Workflow Helper scripts

๐Ÿ“บ DevOpsPro Talk

๐Ÿ“บ Recording from live demo at PlanetScale Happy-Half-Hour

๐Ÿ“บLonger talk at WeAreDevelopers DevOps Day

This is a collection of GitHub Workflows and CI/CD helper scripts around PlanetScale's database branching workflows to:

IssueOps Example

image (example PR)

Installation

To test out the IssueOps commands and helper scripts, either work on a copy of this repo or run the following command in the root directory of your GitHub repo's workspace and follow the suggestions of the script:

curl https://raw.githubusercontent.com/jonico/pscale-workflow-helper-scripts/main/install.sh | bash

Issue-Ops based workflows

(GitHub Actions workflow source)

/ps-create: Create PlanetScale Deploy Request from your PR

You can create a database branch and deploy request in an already existing Pull Request using /ps-create: image

See configuration section for an explanation of the environment variables to set to specify a PlanetScale org and database different from the default settings.

/ps-attach: Attach existing PlanetScale branch or Deploy Request to your PR

If the PlanetScale deploy request or database branch and your pull request both exist already, you can use the /ps-attach command, followed by the PlanetScale URL to your branch/DR:

image

/ps-update: Refresh schema info in your PR from your attached Deploy Request

If you like to refresh the schema information of your branch or add additional DDL statements, use /ps-update:

image

/ps-approve: Approve Deploy Request from your PR

Approvals can be done using /ps-approve:

image

/ps-merge: Merge Deploy Request attached to your PR

Schema changes in attached deploy requests can then be applied to production by adding a /ps-merge comment to the pull request. This IssueOps command will also create GitHub deployment events, so that you could have other integrations and Action based workflows hook into the deployment lifecycle.

image

/ps-delete: Delete PlanetScale branch attached to your PR

Finally, you can delete the database branch associated with a Git branch using /ps-delete:

image

Stay tuned for updates and any feedbacks / PRs are welcome ๐Ÿ˜Š

Zero-setup Action workflows included โœจ

In case you like to see those helper scripts in action without any further manual configuration, just

  1. Create a copy of this repo by clicking on the green 'Use this template' button or just here. Both private and public repo visibility work.

image

  1. Create a PlanetScale account: If you have not done already, sign up here for a PlanetScale account, you can have one database for free. Do not create a database yet though, this step will be automated as part of the first Action workflow.

  2. Switch to the Actions tab in your repo

image

  1. Start the workflows manually, in the displayed order.

image

See configuration section for an explanation of the environment variables to set to specify a PlanetScale org and database different from the default settings.

  1. When you run the workflows, follow the output where it will ask you to authenticate to PlanetScale by clicking the displayed links.

image

If you start the "Create branch/PR for schema change" workflow, a pull request like this one will be created that is associated with a PlanetScale deploy request and the desired schema changes.

image

If you like to install the workflows in your own repository, follow the installation steps.

Configuration

All scripts and IssueOps command in this repo have been designed to run successfully even if zero configuration was provided. If the defaut values do not fit your you, or you do not like to always authorize every action with the Web UI, here are the environment variables you can override in your CI/CD or set as GitHub secrets for your GitHub Action workflows to override default behavior.

DB_NAME

DB_NAME is name of your PlanetScale database. Unless you attach a specific branch or deploy request to your PR, it defaults to example-db-<yourgithubaccount>. If you rather like to change the source for the default DB name, it would be this file.

ORG_NAME

ORG_NAME is name of your PlanetScale organization. Unless you attach a specific branch or deploy request to your PR, it defaults to the first PlanetScale org you joined with your account. If you rather like to change the source for the default DB name, it would be this file.

PLANETSCALE_SERVICE_TOKEN_ID and PLANETSCALE_SERVICE_TOKEN

If you do not like to authorize actions by clicking on authorization links in the browser, you can create a PlanetScale service token. Don't forget to set the proper scopes for the database operations you like to automate.

Note: While service tokens can be used to work with existing databases, there is currently no scope to create new databases. This is why the scripts / workflows to create entirely new databses will always prompt you to authenticate via the Web UI.

CI - Runnning in CI

GitHub Actions and many other CI/CD platforms set the CI variable to true when they run a script. The helper scripts in the .pscale/cli-helper-scripts/ directory will only prompt for interactive user input if the CI variable is not set. In CI mode, it will output additional workflow commands to standard out to enable communication between GitHub Action workflow steps. If you are using a different CI/CD platform than GitHub Actions, you can ignore all lines starting with "::".

Initial table

When you create a bew database from scratch, a default table called pixel_matrix will be created. If you like to change this behavior, modify this file.

pscale-workflow-helper-scripts's People

Contributors

bitoiu avatar fooforge avatar gselva avatar jonico avatar zebulonj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

pscale-workflow-helper-scripts's Issues

Error: both --service-token and --service-token-id are required for service token authentication

Basically its not possible to use pscale CLI within github action. In this example im using create-deploy-request function in ps-create-helper-functions.sh

As you can see below, the user gets authenticated, however requests for authentication anyways
image

I`m able to perform the same action locally
image

There is any new business rule that doest not allow to use CLI in CI/CD?

Job:

jobs:
  deploy-planetscale:
    name: Planetscale deploy
    runs-on: ubuntu-latest
    #container: node:latest
    steps:
      - name: checkout
        uses: actions/checkout@v2

      - name: Apply database migrations in pre-deploy database branch
        timeout-minutes: 3
      # - shell: bash
        env:
          DATABASE_URL: ${{ secrets.DATABASE_URL }}
          PLANETSCALE_SERVICE_TOKEN_ID: ${{secrets.PLANETSCALE_SERVICE_TOKEN_ID}}
          PLANETSCALE_SERVICE_TOKEN_NAME: ${{secrets.PLANETSCALE_SERVICE_TOKEN_NAME}}
          PLANETSCALE_SERVICE_TOKEN: ${{secrets.PLANETSCALE_SERVICE_TOKEN}}
          ORG_NAME: ${{secrets.ORG_NAME}}
          DB_NAME: ${{secrets.DB_NAME}}
          BRANCH_NAME: pre-deploy

        run: |
          ./scripts/deploy-database-staging.sh

deploy-database-staging.sh

#!/bin/bash
. ./scripts/planetscale-docker-image.sh

unset PLANETSCALE_SERVICE_TOKEN
. ./scripts/authenticate-planetscale.sh

#npm i -g prisma >3
#prisma db push --skip-generate

. ./scripts/planetscale-helper-functions.sh
create-deploy-request "$DB_NAME" "$BRANCH_NAME" "$ORG_NAME"
create-deployment "$DB_NAME" "$ORG_NAME" "$DEPLOY_REQUEST_NUMBER"

Obs: All env vars are set propelly

issue running export-db-connection-string.sh and accessing the output

Good Morning,

I am trying to run export-db-connection-string.sh (https://github.com/jonico/pscale-workflow-helper-scripts/blob/main/.pscale/cli-helper-scripts/export-db-connection-string.sh) within my yml file:
image

this is the error I get in GitHub:
image

furthermore, after I have successfully managed to run the script, I would need to store the output (connection string) as a global variable, so that i can use it in the next job (docker build), what is the correct way to access "set-output name=dbconnection_${i}_${j}" within my yml file

Kind Regards
AAACafe

Branch diff is always null

I'm able to successfully attach and update a PR with a Planetscale DR using the issue-ops-ps-commands /ps-attach and /ps-update, however the Branch diff is always null.

Even thought the workflow job passes (green), I also get the following error {"error": "deploy request 'myapp/7' does not exist in organization my-org"}. This DR does indeed exist, and has schema changes, so I'm unsure where the issue stems from. Note that I am only using the issue-ops-ps-commands.yml workflow along with the directory of .pscale shell commands.

Prisma Example

This is fantastic. Thanks for putting this out there! Super helpful. Are you planning on adding an example demoing something like this with Prisma in CI/CD pipeline? If not, I could maybe throw something together and submit a PR.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.