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