Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
GitHub Docs

Quickstart for building GitHub Apps

Quickly build a GitHub App that comments on pull requests.

Introduction

GitHub Apps let you automate processes or integrate other platforms with GitHub. For more info, seeAbout creating GitHub Apps.

This quickstart describes how to quickly create a GitHub App. When a pull request is opened in a repository that the app was granted access to, the app will add a comment to the pull request.

This quickstart uses pre-written code to help you get started quickly. For a more detailed tutorial that helps you write the code, seeBuilding a GitHub App that responds to webhook events.

Prerequisites

Your computer or codespace should use Node.js version 12 or greater. For more information, seeNode.js.

Step 1: Clone the app code

To help you get started quickly, we wrote code that you can use. If you want to learn how to write the code yourself, seeBuilding a GitHub App that responds to webhook events.

  1. Clone thegithub/github-app-js-sample repository. For more information, seeCloning a repository. You may use a local clone or GitHub Codespaces.
  2. In a terminal window, navigate to the directory where your clone is stored.
  3. Runnpm install to install the dependencies.

Step 2: Get a webhook proxy URL

In order to develop your app locally, you can use a webhook proxy URL to forward webhooks from GitHub to your computer or codespace. This quickstart uses Smee.io to provide a webhook proxy URL and forward webhooks.

  1. In your browser, navigate tohttps://smee.io/.
  2. ClickStart a new channel.
  3. Copy the full URL under "Webhook Proxy URL". You will use this URL in a later step.

Step 3: Register a GitHub App

The following steps will guide you through configuring the app settings that are required for this quickstart. For more information about the settings, seeRegistering a GitHub App.

  1. In the upper-right corner of any page on GitHub, click your profile picture.
  2. Navigate to your account settings.
    • For an app owned by a personal account, clickSettings.
    • For an app owned by an organization:
      1. ClickYour organizations.
      2. To the right of the organization, clickSettings.
  3. In the left sidebar, click Developer settings.
  4. In the left sidebar, clickGitHub Apps.
  5. ClickNew GitHub App.
  6. Under "GitHub App name", enter a name for your app. For example,USERNAME-quickstart-app whereUSERNAME is your GitHub username.
  7. Under "Homepage URL", enterhttps://github.com/github/github-app-js-sample#readme.
  8. Skip the "Identifying and authorizing users" and "Post installation" sections for this quickstart. For more information about these settings, seeRegistering a GitHub App.
  9. Make sure thatActive is selected under "Webhooks."
  10. Under "Webhook URL", enter your webhook proxy URL from earlier. For more information, seeStep 2: Get a webhook proxy URL.
  11. Under "Webhook secret", enter a random string. You will use this string later.
  12. Under "Repository permissions", next to "Pull requests," selectRead & write.
  13. Under "Subscribe to events", selectPull request.
  14. Under "Where can this GitHub App be installed?", selectOnly on this account.
  15. ClickCreate GitHub App.

Step 4: Store identifying information and credentials

In this quickstart, you will store your app's credentials and identifying information as environment variables in a.env file. When you deploy your app, you will want to change how you store the credentials. For more information, seeDeploy your app.

Make sure that you are on a secure machine before performing these steps since you will store your credentials locally.

Create a.env file

Your cloned repository includes.env in the.gitignore file. This will prevent you from accidentally committing your app's credentials. For more information about.gitignore files, seeIgnoring files.

  1. Navigate to the directory where your clone ofgithub/github-app-js-sample is stored.

  2. Create a file called.env at the top level of this directory.

  3. Add the following contents to your.env file. You will update the values in a later step.

    Text
    APP_ID="YOUR_APP_ID"WEBHOOK_SECRET="YOUR_WEBHOOK_SECRET"PRIVATE_KEY_PATH="YOUR_PRIVATE_KEY_PATH"

Navigate to your app settings

If you navigated away from your app settings after creating your app, navigate to the settings page for your app:

  1. In the upper-right corner of any page on GitHub, click your profile picture.
  2. Navigate to your account settings.
    • For an app owned by a personal account, clickSettings.
    • For an app owned by an organization:
      1. ClickYour organizations.
      2. To the right of the organization, clickSettings.
  3. In the left sidebar, click Developer settings.
  4. In the left sidebar, clickGitHub Apps.
  5. Next to your app's name, clickEdit.

Get your app credentials and identifying information

  1. On your app's settings page, next to "App ID", find the app ID for your app.
  2. In your.env file, replaceYOUR_APP_ID with the app ID of your app.
  3. On your app's settings page, under "Private keys", clickGenerate a private key. You will see a private key in PEM format downloaded to your computer. For more information, seeManaging private keys for GitHub Apps.
  4. If you are using a codespace, move the downloaded PEM file into your codespace so that your codespace can access the file.
  5. In your.env file, replaceYOUR_PRIVATE_KEY_PATH with the full path to your private key, including the.pem extension.
  6. In your.env file, replaceYOUR_WEBHOOK_SECRET with the webhook secret for your app. If you have forgotten your webhook secret, under "Webhook secret (optional)", clickChange secret. Enter a new secret, then clickSave changes.

Step 5: Install your app

In order for your app to leave a comment on pull requests in a repository, it must be installed on the account that owns the repository and granted access to that repository. Since your app is private, it can only be installed on the account that owns the app.

  1. In the account that owns the app you created, create a new repository to install the app on. For more information, seeCreating a new repository.
  2. If you navigated away from your app settings after creating your app, navigate to the settings page for your app. For more information, seeNavigate to your app settings.
  3. ClickPublic page.
  4. ClickInstall.
  5. SelectOnly select repositories.
  6. Select theSelect repositories dropdown menu and click the repository that you chose at the start of this section.
  7. ClickInstall.

Step 6: Start your server

For testing, you will use your computer or codespace as a server. Your app will only be active when your server is running.

  1. In a terminal window, navigate to the directory where your clone ofgithub/github-app-js-sample is stored.

  2. To receive forwarded webhooks from Smee.io, runnpx smee -u WEBHOOK_PROXY_URL -t http://localhost:3000/api/webhook. ReplaceWEBHOOK_PROXY_URL with your webhook proxy URL. If you forgot your URL, you can find it in the "webhook URL" field on your app's settings page.

    You should see output that looks like this, whereWEBHOOK_PROXY_URL is your webhook proxy URL:

    Forwarding WEBHOOK_PROXY_URL to http://localhost:3000/api/webhookConnected WEBHOOK_PROXY_URL
  3. In a second terminal window, navigate to the directory where your clone ofgithub/github-app-js-sample is stored.

  4. Runnpm run server. Your terminal should say,Server is listening for events at: http://localhost:3000/api/webhook.

Step 7: Test your app

Now that your server is running and receiving forwarded webhooks events, test your app by opening a pull request.

  1. Open a pull request on the repository you created inStep 5: Install your app. For more information, seeCreating a pull request.
  2. Navigate to your webhook proxy URL on smee.io. You should see apull_request event. This indicates that GitHub successfully sent a pull request event when you created a pull request.
  3. In the terminal where you rannpm run server, you should see something like "Received a pull request event for #1" where the integer after the# is the number of the pull request that you opened.
  4. In the timeline of your pull request, you should see a comment from your app. The comment uses the contents of themessage.md file in your cloned repository.
  5. In both terminal windows, enterCtrl+C to stop your server and stop listening for forwarded webhooks.

Next steps

Now that you have an app, you might want to expand your app's code, deploy your app, and make your app public.

Modify the app code

Fork thegithub/github-app-js-sample repository and modify the code to respond to different webhook events or to make different API requests. For more information about the code, seeBuilding a GitHub App that responds to webhook events.

Remember to update your app's permissions if your app needs additional permissions for the API requests that you want to make or the webhook events you want to receive. For more information, seeChoosing permissions for a GitHub App.

Deploy your app

This tutorial used your computer or codespace as a server. Once the app is ready for production use, you should deploy your app to a dedicated server. For example, you can useAzure App Service.

Once you have a server, update the webhook URL in your app settings. You should not use Smee.io to forward your webhooks in production.

You will also need to update theport andhost constants in the code. For more information, seeBuilding a GitHub App that responds to webhook events.

You should never publicize your app's private key or webhook secret. This tutorial stored your app's credentials in a gitignored.env file. When you deploy your app, you should choose a secure way to store the credentials and update your code to get the value accordingly. For example, you can store the credentials in an environment variable on the server where your app is deployed. You can also use a secret management service likeAzure Key Vault.

Share your app

If you want to share your app with other users and organizations, make your app public. For more information, seeMaking a GitHub App public or private.

Follow best practices

You should aim to follow best practices with your GitHub App. For more information, seeBest practices for creating a GitHub App.


[8]ページ先頭

©2009-2025 Movatter.jp