GitHubSync

Webhook to commit vals to GitHub and sync code from GitHub back to Val Town

Demo

This example val is synced to this GitHub repo

How to set up

This val exposes two webhook endpoints:

  • /push will copy the contents from a list of vals specified in config.json and push them to a GitHub repo
  • /deploy is a GitHub webhook URL that will fetch contents from GitHub and update the code on Val Town
  1. Fork this val
  2. Edit config.json to point to the repo and vals you plan to sync
  3. Create a new repo on GitHub to sync to
  4. Add a webhook to your repo that points to this val's /deploy route, e.g. https://username-<ID>.web.val.run/deploy
    • Create a secret for your webhook
    • In this val's Environment Variables, add that secret as GITHUB_WEBHOOK_SECRET
  5. On GitHub, create a new personal access token
    • Ensure the token has read/write access to Contents for the repo
    • Copy the access token and add that as the GITHUB_TOKEN env var in this val
  6. Add a new Val Town API token with read/write permissions. Add that token to the val's env vars as VALTOWN_TOKEN
  7. Add a VAL_SECRET env var to the val. Use this secret to sign the webhook POST request to the /push endpoint. Use this endpoint to commit vals from Val Town to your GitHub repo.

Example push to GitHub fetch

You can use this example to POST to the /push endpoint to copy vals to GitHub. You should keep the code for this private so that only you can push to GitHub. This example can run manually or use a Cron val.

NOTE: this can overwrite changes made outside of Val Town, on GitHub/etc.

// This can post to the GitHubSync `/push` webhook; keep this private import sign from "https://esm.town/v/jxnblk/GitHubSync/sign-webhook"; const secret = Deno.env.get("SECRET") as string; const url = Deno.env.get("PUSH_URL") as string; async function pushValsToGitHub() { const body = "hello"; const signature = await sign(body, secret); const res = await fetch(url, { method: "POST", body, headers: { "x-val-signature": signature, } }) if (!res.ok) { console.log(res); return; } const json = await res.json(); console.log(json); } pushValsToGitHub();

Env vars

  • GITHUB_TOKEN: Read/write GitHub personal access token for reading and writing repo contents
  • VALTOWN_TOKEN: ValTown API token (with read/write Vals permissions) for writing updates from GitHub
  • GITHUB_WEBHOOK_SECRET: secret for verifying webhooks from GitHub
  • VAL_SECRET: secret for verifying requests to the /push endpoint

This is the latest version of https://www.val.town/v/jxnblk/gitHubSync. Note: This will currently only work with non-project vals until the SDK supports projects.