jogeshwar01 / native-github-app

0 stars 0 forks source link

hono-cloudflare-worker-github-app

A Cloudflare Worker (Hono) + GitHub App built with @octokit/app POST requests: handle webhook request from GitHub Reference - gr2m/cloudflare-worker-github-app-example

Setup

Note that you require access to the new GitHub Actions for the automated deployment to work.

  1. Create a GitHub App

  2. Create a Cloudflare account (it's free!) if you don't have one yet.

  3. Install the wrangler CLI and login with your account

    npm install --global wrangler
    wrangler login
  4. Add the following secrets to your Cloudflare worker:

    • APP_ID: In your GitHub App registration's settings page, find App ID

      wrangler secret put APP_ID
    • WEBHOOK_SECRET: In your GitHub App registration's settings page, find Webhook secret

      wrangler secret put WEBHOOK_SECRET
    • PRIVATE_KEY: Generate a private key (see the button at the bottom of your GitHub App registration's settings page).

      1. You will be prompted to download a *.pem file. After download, rename it to private-key.pem.
      2. Convert the key from the PKCS#1 format to PKCS#8 (The WebCrypto API only supports PKCS#8):
      openssl pkcs8 -topk8 -inform PEM -outform PEM -nocrypt -in private-key.pem -out private-key-pkcs8.pem
      1. Write the contents of the new file into the secret PRIVATE_KEY:
      cat private-key-pkcs8.pem | wrangler secret put PRIVATE_KEY
    • Instead of using wrangler secret put, you can also add the variables in the wrangler.toml file under the [vars] section.

  5. For local development

To setup bot for local development, you would need a webhook url for your github app to hit. You can use ngrok to create a tunnel to your localhost. Suppose your bot runs on localhost:8787, then you can run:

ngrok http 8787

After executing that command you should see something like this:

Forwarding      https://9add-49-43-98-101.ngrok-free.app -> http://localhost:8787
  1. To deploy your code to Hono -
yarn deploy
  1. Add the Webhook URL (ngrok for local dev. or cloudflare worker url) in the Github App.

License

ISC © 2024 Jogeshwar Singh