Home > Mobile >  How to run a custom command unique to a PR on merging a PR?
How to run a custom command unique to a PR on merging a PR?

Time:08-21

I am wondering if there is any way to do the following. Say I have an "open data" repo, which allows people to submit content. The repo saves all the data, and the changes to the structured JSON/YAML is reviewed in a PR. But then because I am in a serverless system (like Vercel), I need to upload the changes to the data to the production database, on merge of the branch. So there should be required a custom data migration in the PR, which runs when the PR is approved and merged.

How can that be accomplished? All I can imagine as a solution is having a special "code block" in markdown with some JSON config explaining what script to run for the data migration, and you add that marked code snippet as a comment to the PR, then parse the PR comments and figure out what script to run from that. But that would be of course (seemingly) a super hack, so is there a right way to do something like this?

The other option is to have to run the script/command manually after you merge the PR, but ideally there would be a more automatic way of doing it.

CodePudding user response:

GitHub itself can run code on various events through actions. Actions are configured through YAML files in the directory .github/workflows in the repository. Some actions relative to a branch use the workflow files from that branch, while “global” actions use the workflow files from the default branch (typically called main, or master for older repositories).

For example, this workflow runs bin/update-production-database whenever the main branch is updated (whether from a pull request merge or by pushing directly):

name: Update database
on:
  push:
    branches:
      - main
jobs:
  update-database:
    runs-on: ubuntu-latest
    steps:
      - run: bin/update-production-database

See more examples in Deploying with GitHub Actions. To pass the credentials needed to access the database, set up an encrypted secret.

To only run the job on a PR merge and not on other pushes, see Running your workflow when a pull request merges.

If you use Vercel (which I know nothing about), it claims it “automatically deploys your GitHub projects” so there may be a built-in solution there (either using actions so that the trigger comes from GitHub, or using some Vercel-owned server which polls GitHub).

  • Related