Skip to content

Commit 673b201

Browse files
feat: enhance GitHub authentication with direct token support and improved fallback methods
1 parent ab1e663 commit 673b201

File tree

4 files changed

+61
-27
lines changed

4 files changed

+61
-27
lines changed

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

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ module "copilot_cli" {
2727
- **Node.js v22+** and **npm v10+**
2828
- **Active Copilot subscription** (GitHub Copilot Pro, Pro+, Business, or Enterprise)
2929
- **GitHub authentication** via one of:
30+
- Direct token via `github_token` variable (highest priority)
3031
- Coder external authentication (recommended)
3132
- GitHub CLI (`gh auth login`)
32-
- Environment token (`GITHUB_TOKEN`)
3333
- Or use interactive login in Copilot CLI
3434

3535
## Examples
@@ -80,6 +80,20 @@ module "copilot_cli" {
8080
}
8181
```
8282

83+
### Direct Token Authentication
84+
85+
Use a GitHub token directly (OAuth token or Personal Access Token):
86+
87+
```tf
88+
module "copilot_cli" {
89+
source = "registry.coder.com/coder-labs/copilot-cli/coder"
90+
version = "1.0.0"
91+
agent_id = coder_agent.example.id
92+
workdir = "/home/coder/project"
93+
github_token = "your_github_token_here" # Or use data.coder_external_auth.github.access_token
94+
}
95+
```
96+
8397
### Standalone Mode
8498

8599
Run and configure Copilot CLI as a standalone tool in your workspace.
@@ -144,17 +158,24 @@ module "copilot_cli" {
144158

145159
## Authentication
146160

147-
This module works with multiple GitHub authentication methods:
161+
This module works with multiple GitHub authentication methods in priority order:
162+
163+
**1. Direct Token (highest priority):**
164+
165+
- **`github_token` variable**: Provide a GitHub OAuth token or Personal Access Token directly to the module
148166

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
167+
**2. Automatic detection:**
168+
169+
- **Coder External Auth**: OAuth tokens from GitHub external authentication configured in Coder
170+
- **GitHub CLI**: OAuth tokens from `gh auth login` in the workspace
171+
172+
**3. Interactive fallback:**
152173

153-
**Automatic fallback:**
154-
- **Environment tokens**: Uses existing `GITHUB_TOKEN` if available (note: Personal Access Tokens may not work with all Copilot CLI features)
155174
- **Interactive login**: If no authentication is found, Copilot CLI will prompt users to login via the `/login` slash command
156175

157-
**No setup required** - the module automatically detects and uses whatever authentication is available.
176+
**No setup required** for automatic methods - the module detects and uses whatever authentication is available.
177+
178+
> **Note**: OAuth tokens work best with Copilot CLI. Personal Access Tokens may have limited functionality.
158179
159180
## Troubleshooting
160181

registry/coder-labs/modules/copilot-cli/main.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ variable "external_auth_id" {
2424
default = "github"
2525
}
2626

27+
variable "github_token" {
28+
type = string
29+
description = "GitHub OAuth token or Personal Access Token. If provided, this will be used instead of auto-detecting authentication."
30+
default = ""
31+
sensitive = true
32+
}
33+
2734
variable "copilot_model" {
2835
type = string
2936
description = "Model to use. Supported values: claude-sonnet-4 (default), claude-sonnet-4.5, gpt-5."
@@ -188,6 +195,13 @@ resource "coder_env" "copilot_model" {
188195
value = var.copilot_model
189196
}
190197

198+
resource "coder_env" "github_token" {
199+
count = var.github_token != "" ? 1 : 0
200+
agent_id = var.agent_id
201+
name = "GITHUB_TOKEN"
202+
value = var.github_token
203+
}
204+
191205

192206

193207
module "agentapi" {

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ install_copilot_cli() {
5151
check_github_authentication() {
5252
echo "Checking GitHub authentication..."
5353

54+
if [ -n "$GITHUB_TOKEN" ]; then
55+
echo "✓ GitHub token provided via module configuration"
56+
return 0
57+
fi
58+
5459
if command_exists coder; then
5560
if coder external-auth access-token "${ARG_EXTERNAL_AUTH_ID:-github}" > /dev/null 2>&1; then
5661
echo "✓ GitHub OAuth authentication via Coder external auth"
@@ -63,12 +68,6 @@ check_github_authentication() {
6368
return 0
6469
fi
6570

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-
7271
echo "⚠ No GitHub authentication detected"
7372
echo " Copilot CLI will prompt for authentication when started"
7473
echo " For seamless experience, configure GitHub external auth in Coder or run 'gh auth login'"

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,18 @@ configure_copilot_model() {
8484

8585
setup_github_authentication() {
8686
echo "Setting up GitHub authentication..."
87-
87+
88+
# Check for provided token first (highest priority)
89+
if [ -n "$GITHUB_TOKEN" ]; then
90+
export GH_TOKEN="$GITHUB_TOKEN"
91+
echo "✓ Using GitHub token from module configuration"
92+
return 0
93+
fi
94+
95+
# Try external auth
8896
if command_exists coder; then
8997
local github_token
90-
if github_token=$(coder external-auth access-token "${ARG_EXTERNAL_AUTH_ID:-github}" 2>/dev/null); then
98+
if github_token=$(coder external-auth access-token "${ARG_EXTERNAL_AUTH_ID:-github}" 2> /dev/null); then
9199
if [ -n "$github_token" ] && [ "$github_token" != "null" ]; then
92100
export GITHUB_TOKEN="$github_token"
93101
export GH_TOKEN="$github_token"
@@ -96,25 +104,17 @@ setup_github_authentication() {
96104
fi
97105
fi
98106
fi
99-
107+
100108
# Try GitHub CLI as fallback
101109
if command_exists gh && gh auth status > /dev/null 2>&1; then
102110
echo "✓ Using GitHub CLI OAuth authentication"
103111
return 0
104112
fi
105-
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-
113+
114114
echo "⚠ No GitHub authentication available"
115115
echo " Copilot CLI will prompt for login during first use"
116116
echo " Use the '/login' command in Copilot CLI to authenticate"
117-
return 0 # Don't fail - let Copilot CLI handle authentication
117+
return 0 # Don't fail - let Copilot CLI handle authentication
118118
}
119119

120120
start_agentapi() {

0 commit comments

Comments
 (0)