Skip to content

Commit ab1e663

Browse files
feat(copilot-cli): improve GitHub authentication with multiple fallback methods
1 parent 3cef733 commit ab1e663

File tree

3 files changed

+44
-21
lines changed

3 files changed

+44
-21
lines changed

registry/coder-labs/modules/copilot-cli/README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ module "copilot_cli" {
2626

2727
- **Node.js v22+** and **npm v10+**
2828
- **Active Copilot subscription** (GitHub Copilot Pro, Pro+, Business, or Enterprise)
29-
- **GitHub external authentication** configured in Coder
29+
- **GitHub authentication** via one of:
30+
- Coder external authentication (recommended)
31+
- GitHub CLI (`gh auth login`)
32+
- Environment token (`GITHUB_TOKEN`)
33+
- Or use interactive login in Copilot CLI
3034

3135
## Examples
3236

@@ -140,12 +144,17 @@ module "copilot_cli" {
140144

141145
## Authentication
142146

143-
This module uses Coder's GitHub external authentication:
147+
This module works with multiple GitHub authentication methods:
144148

145-
- Users authenticate via GitHub OAuth in the Coder UI
146-
- Copilot CLI automatically uses the authenticated session
147-
- No manual token management required
148-
- If not authenticated, users will be prompted to login via the `/login` slash command
149+
**Recommended (automatic):**
150+
- **Coder External Auth**: Configure GitHub external authentication in Coder for seamless OAuth token integration
151+
- **GitHub CLI**: Users can run `gh auth login` in their workspace
152+
153+
**Automatic fallback:**
154+
- **Environment tokens**: Uses existing `GITHUB_TOKEN` if available (note: Personal Access Tokens may not work with all Copilot CLI features)
155+
- **Interactive login**: If no authentication is found, Copilot CLI will prompt users to login via the `/login` slash command
156+
157+
**No setup required** - the module automatically detects and uses whatever authentication is available.
149158

150159
## Troubleshooting
151160

registry/coder-labs/modules/copilot-cli/scripts/install.sh

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,30 @@ install_copilot_cli() {
4949
}
5050

5151
check_github_authentication() {
52-
echo "Checking GitHub authentication via Coder external auth..."
52+
echo "Checking GitHub authentication..."
5353

5454
if command_exists coder; then
5555
if coder external-auth access-token "${ARG_EXTERNAL_AUTH_ID:-github}" > /dev/null 2>&1; then
56-
echo "GitHub authentication via Coder external auth: OK"
56+
echo "GitHub OAuth authentication via Coder external auth"
5757
return 0
58-
else
59-
echo "WARNING: GitHub external auth not configured or expired"
60-
echo "Please authenticate with GitHub in the Coder UI"
6158
fi
6259
fi
6360

6461
if command_exists gh && gh auth status > /dev/null 2>&1; then
65-
echo "GitHub CLI authentication detected as fallback"
62+
echo "GitHub OAuth authentication via GitHub CLI"
6663
return 0
6764
fi
6865

69-
echo "WARNING: No GitHub authentication found. Copilot CLI requires:"
70-
echo " - GitHub external authentication configured in Coder (recommended)"
71-
echo " - Or GitHub CLI with 'gh auth login'"
66+
if [ -n "$GITHUB_TOKEN" ]; then
67+
echo "✓ GitHub token found in environment"
68+
echo " Note: Copilot CLI works best with OAuth tokens from Coder external auth or 'gh auth login'"
69+
return 0
70+
fi
71+
72+
echo "⚠ No GitHub authentication detected"
73+
echo " Copilot CLI will prompt for authentication when started"
74+
echo " For seamless experience, configure GitHub external auth in Coder or run 'gh auth login'"
75+
return 0
7276
}
7377

7478
setup_copilot_configurations() {

registry/coder-labs/modules/copilot-cli/scripts/start.sh

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,24 +87,34 @@ setup_github_authentication() {
8787

8888
if command_exists coder; then
8989
local github_token
90-
if github_token=$(coder external-auth access-token "${ARG_EXTERNAL_AUTH_ID}" 2>/dev/null); then
90+
if github_token=$(coder external-auth access-token "${ARG_EXTERNAL_AUTH_ID:-github}" 2>/dev/null); then
9191
if [ -n "$github_token" ] && [ "$github_token" != "null" ]; then
9292
export GITHUB_TOKEN="$github_token"
9393
export GH_TOKEN="$github_token"
94-
echo "GitHub authentication: via Coder external auth"
94+
echo "✓ Using Coder external auth OAuth token"
9595
return 0
9696
fi
9797
fi
9898
fi
9999

100+
# Try GitHub CLI as fallback
100101
if command_exists gh && gh auth status > /dev/null 2>&1; then
101-
echo "GitHub authentication: via GitHub CLI"
102+
echo "✓ Using GitHub CLI OAuth authentication"
102103
return 0
103104
fi
104105

105-
echo "WARNING: No GitHub authentication found. Copilot CLI requires authentication."
106-
echo "Please ensure GitHub external auth is configured in Coder or run 'gh auth login'"
107-
return 1
106+
# Use existing environment variable if present
107+
if [ -n "$GITHUB_TOKEN" ]; then
108+
export GH_TOKEN="$GITHUB_TOKEN"
109+
echo "✓ Using GitHub token from environment"
110+
echo " Note: If this is a Personal Access Token, Copilot CLI may not work properly"
111+
return 0
112+
fi
113+
114+
echo "⚠ No GitHub authentication available"
115+
echo " Copilot CLI will prompt for login during first use"
116+
echo " Use the '/login' command in Copilot CLI to authenticate"
117+
return 0 # Don't fail - let Copilot CLI handle authentication
108118
}
109119

110120
start_agentapi() {

0 commit comments

Comments
 (0)