|
| 1 | +# GitHub App Setup |
| 2 | + |
| 3 | +This guide walks through creating and installing a GitHub App for github-ops-app. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- GitHub organization with admin access |
| 8 | + |
| 9 | +## Step 1: Create the GitHub App |
| 10 | + |
| 11 | +1. Navigate to your organization's settings: |
| 12 | + - Go to `https://github.com/organizations/YOUR_ORG/settings/apps` |
| 13 | + - Or: **Organization** → **Settings** → **Developer settings** → **GitHub Apps** |
| 14 | + |
| 15 | +2. Click **New GitHub App** |
| 16 | + |
| 17 | +3. Fill in the basic information: |
| 18 | + |
| 19 | + | Field | Value | |
| 20 | + |-----------------------|-------------------------------------------------| |
| 21 | + | GitHub App name | `github-ops-app` (must be unique across GitHub) | |
| 22 | + | Homepage URL | Your organization's URL or repo URL | |
| 23 | + | Webhook > Webhook URL | Leave blank for now | |
| 24 | + | Webhook > Secret | Generate a strong secret (save this for later) | |
| 25 | + | Webhook > Active | **Uncheck** to disable webhooks initially | |
| 26 | + |
| 27 | + > **Note**: Disable webhooks during creation since you may not know your |
| 28 | + > endpoint URL until after deployment. You'll configure webhooks and |
| 29 | + > subscribe to events in [Step 7](#step-7-configure-webhook-and-events). |
| 30 | +
|
| 31 | +4. Under **Permissions**, set the following: |
| 32 | + - Repository Permissions |
| 33 | + - Contents: Read |
| 34 | + - Read branch protection rules |
| 35 | + - Pull requests: Read |
| 36 | + - Access PR details for compliance |
| 37 | + - Organization Permissions |
| 38 | + - Administration: Read |
| 39 | + - Read organization settings |
| 40 | + - Members: Read/Write |
| 41 | + - Manage team membership |
| 42 | + |
| 43 | +4. Under Set installation scope: |
| 44 | + - Where can this GitHub App be installed?: Only on this account |
| 45 | + |
| 46 | +5. Click **Create GitHub App** |
| 47 | + |
| 48 | +## Step 2: Generate Private Key |
| 49 | + |
| 50 | +After creating the app: |
| 51 | + |
| 52 | +1. Scroll to **Private keys** section |
| 53 | +2. Click **Generate a private key** |
| 54 | +3. Save the downloaded `.pem` file securely |
| 55 | +4. This file is used for `APP_GITHUB_APP_PRIVATE_KEY` or |
| 56 | + `APP_GITHUB_APP_PRIVATE_KEY_PATH` |
| 57 | + |
| 58 | +## Step 3: Note Your App ID |
| 59 | + |
| 60 | +On the app's settings page, find and save: |
| 61 | + |
| 62 | +- **App ID** - numeric ID displayed near the top (e.g., `123456`) |
| 63 | + |
| 64 | +## Step 4: Install the App |
| 65 | + |
| 66 | +1. In the left sidebar, click **Install App** |
| 67 | +2. Select your organization |
| 68 | +3. Choose repository access: |
| 69 | + - **All repositories** - recommended for org-wide PR compliance |
| 70 | + - **Only select repositories** - if limiting scope |
| 71 | +4. Click **Install** |
| 72 | + |
| 73 | +## Step 5: Get Installation ID |
| 74 | + |
| 75 | +After installation, you'll be redirected to a URL like: |
| 76 | +``` |
| 77 | +https://github.com/organizations/YOUR_ORG/settings/installations/12345678 |
| 78 | +``` |
| 79 | + |
| 80 | +The number at the end (`12345678`) is your **Installation ID**. |
| 81 | + |
| 82 | +Alternatively, use the GitHub API: |
| 83 | +```bash |
| 84 | +# List installations (requires app JWT authentication) |
| 85 | +curl -H "Authorization: Bearer YOUR_JWT" \ |
| 86 | + https://api.github.com/app/installations |
| 87 | +``` |
| 88 | + |
| 89 | +## Step 6: Configure Environment Variables |
| 90 | + |
| 91 | +Set these environment variables in your deployment: |
| 92 | + |
| 93 | +```bash |
| 94 | +# Required GitHub configuration |
| 95 | +APP_GITHUB_APP_ID=123456 |
| 96 | +APP_GITHUB_INSTALLATION_ID=12345678 |
| 97 | +APP_GITHUB_ORG=your-org-name |
| 98 | +APP_GITHUB_WEBHOOK_SECRET=your-webhook-secret |
| 99 | + |
| 100 | +# Private key (choose one method) |
| 101 | +APP_GITHUB_APP_PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY----- |
| 102 | +... |
| 103 | +-----END RSA PRIVATE KEY-----" |
| 104 | + |
| 105 | +# Or use a file path |
| 106 | +APP_GITHUB_APP_PRIVATE_KEY_PATH=/path/to/private-key.pem |
| 107 | + |
| 108 | +# Or use AWS SSM parameter |
| 109 | +APP_GITHUB_APP_PRIVATE_KEY=arn:aws:ssm:us-east-1:123456789:parameter/github-bot/private-key |
| 110 | +``` |
| 111 | + |
| 112 | +## Step 7: Configure Webhook and Events |
| 113 | + |
| 114 | +After deploying your server, configure and enable webhooks: |
| 115 | + |
| 116 | +1. Go to your GitHub App settings: |
| 117 | + `https://github.com/organizations/YOUR_ORG/settings/apps/YOUR_APP` |
| 118 | +2. On the **General** tab, under **Webhook**: |
| 119 | + - Set **Webhook URL** to your endpoint: |
| 120 | + - Lambda: `https://xxx.execute-api.region.amazonaws.com/webhooks` |
| 121 | + - Server: `https://your-domain.com/webhooks` |
| 122 | + - Check **Active** to enable webhooks |
| 123 | + - Click **Save changes** |
| 124 | +3. Go to the **Permissions & events** tab |
| 125 | +4. Scroll to **Subscribe to events** and check: |
| 126 | + - [x] **Pull request** - PR open, close, merge events |
| 127 | + - [x] **Team** - Team creation, deletion, changes |
| 128 | + - [x] **Membership** - Team membership changes |
| 129 | +5. Click **Save changes** |
| 130 | + |
| 131 | +## Verification |
| 132 | + |
| 133 | +Test your setup: |
| 134 | + |
| 135 | +1. **Webhook delivery**: Check **Settings** → **Developer settings** → |
| 136 | + **GitHub Apps** → your app → **Advanced** → **Recent Deliveries** |
| 137 | + |
| 138 | +2. **Create a test PR**: Open and merge a PR to a monitored branch to verify |
| 139 | + webhook reception |
| 140 | + |
| 141 | +3. **Check logs**: Verify your application receives and processes the webhook |
| 142 | + |
| 143 | +## Troubleshooting |
| 144 | + |
| 145 | +### Webhook signature verification failed |
| 146 | + |
| 147 | +- Verify `APP_GITHUB_WEBHOOK_SECRET` matches the secret in GitHub App settings |
| 148 | +- Check for whitespace or encoding issues in the secret |
| 149 | + |
| 150 | +### 401 Unauthorized from GitHub API |
| 151 | + |
| 152 | +- Verify the private key matches the one generated for this app |
| 153 | +- Check that the app is installed on the target organization |
| 154 | +- Ensure `APP_GITHUB_INSTALLATION_ID` is correct |
| 155 | + |
| 156 | +### Missing permissions error |
| 157 | + |
| 158 | +- Re-check the app's permission settings |
| 159 | +- After changing permissions, organization admins may need to re-approve |
| 160 | + |
| 161 | +### Webhook not received |
| 162 | + |
| 163 | +- Verify the webhook URL is accessible from the internet |
| 164 | +- Check the webhook URL doesn't have a trailing slash mismatch |
| 165 | +- Review recent deliveries in GitHub App settings for error details |
0 commit comments