Public
GitHubSync
Webhook to commit vals to GitHub and sync code from GitHub back to Val Town
This example val is synced to this GitHub repo
This val exposes two webhook endpoints:
/push
will copy the contents from a list of vals specified inconfig.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
- Fork this val
- Edit
config.json
to point to the repo and vals you plan to sync - Create a new repo on GitHub to sync to
- 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
- 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
- Add a new Val Town API token with read/write permissions. Add that token to the val's env vars as
VALTOWN_TOKEN
- 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.
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();
GITHUB_TOKEN
: Read/write GitHub personal access token for reading and writing repo contentsVALTOWN_TOKEN
: ValTown API token (with read/write Vals permissions) for writing updates from GitHubGITHUB_WEBHOOK_SECRET
: secret for verifying webhooks from GitHubVAL_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.
Script
HTTP
Script