关于 github-auth
github-auth 是为 GitHub 操作提供凭据访问说明的技能,适用于需要使用 API、管理仓库或执行 Git 命令的场景。它涵盖 .env 中的 PAT、gh CLI、SSH 密钥及凭据安全做法。
主要功能
- 01从 .env 读取 GitHub 凭据
- 02使用 gh CLI 执行已认证操作
- 03通过 API 访问 GitHub
- 04建议使用 SSH 密钥或凭据助手
- 05提供令牌安全与故障排查指导
适用场景
- 01创建仓库或管理 Issue、PR 和 Release
- 02通过 API 列出仓库
- 03克隆私有仓库并执行 Git 操作
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ jackspace/ claudeskillz --skill github-auth安装后在 Claude Code 中输入/github-auth确认已加载。
附带文件SKILL.json
详细介绍
GitHub 身份验证
此技能可安全访问 GitHub 凭据,以执行 API 操作、仓库管理和 git 命令。
说明
在协助执行需要身份验证的 GitHub 操作时:
凭据位置
-
凭据存储在项目根目录的
.env文件中 -
跨平台路径示例:
- Linux/macOS:
~/apps/your_claude_skills/.env,或使用相对路径:./.env - Windows:
%USERPROFILE%\apps\your_claude_skills\.env,或使用相对路径:.\.env
- Linux/macOS:
-
加载凭据:
# Linux/macOS: source ./.env # Windows PowerShell: # Get-Content .\.env | ForEach-Object { if ($_ -match '^([^=]+)=(.*)$') { [Environment]::SetEnvironmentVariable($matches[1], $matches[2]) } } -
在脚本中访问:
# Linux/macOS: GITHUB_USERNAME=$(grep GITHUB_USERNAME ./.env | cut -d= -f2) GITHUB_PAT=$(grep GITHUB_PAT ./.env | cut -d= -f2) # Windows PowerShell: # $GITHUB_USERNAME = (Get-Content .\.env | Select-String "GITHUB_USERNAME").Line.Split("=")[1] # $GITHUB_PAT = (Get-Content .\.env | Select-String "GITHUB_PAT").Line.Split("=")[1]
GitHub API 操作
使用 GitHub CLI(gh)执行需要身份验证的操作:
# Authenticate gh with stored PAT
echo "$GITHUB_PAT" | gh auth login --with-token
# Or use API directly with curl
curl -H "Authorization: token $GITHUB_PAT" https://api.github.com/user/repos
使用身份验证执行 Git 操作
⚠️ 安全警告:将凭据嵌入 URL 存在安全风险。请改用 SSH 密钥或 git 凭据助手。
推荐:使用 SSH 密钥
# Setup SSH key for GitHub (one-time setup)
ssh-keygen -t ed25519 -C "[email protected]"
cat ~/.ssh/id_ed25519.pub # Add this to GitHub Settings > SSH Keys
# Clone with SSH (RECOMMENDED)
git clone [email protected]:owner/repo.git
# Add SSH remote
git remote add origin [email protected]:owner/repo.git
替代方案:使用 Git 凭据助手
# Configure git credential helper (stores credentials securely)
git config --global credential.helper store
# First time will prompt for credentials, then stores them securely
git clone https://github.com/owner/repo.git
不推荐:在 URL 中使用凭据(仅用于自动化/CI)
# WARNING: Credentials in URLs can leak in logs/history
# Only use in secure, automated environments
git clone https://$GITHUB_USERNAME:[email protected]/owner/repo.git
常见 GitHub 操作
-
创建仓库
gh repo create owner/repo --private --description "Description" -
列出仓库
gh repo list -
创建拉取请求
gh pr create --title "Title" --body "Description" -
管理议题
gh issue create --title "Issue" --body "Description" gh issue list -
发布管理
gh release create v1.0.0 --title "Release 1.0.0" --notes "Release notes"
安全最佳实践
-
切勿回显或显示 PAT
- 切勿使用
echo $GITHUB_PAT或显示令牌 - 在命令中直接使用,或将其通过管道传递到 stdin
- 限制
.env文件的权限(chmod 600)
- 切勿使用
-
尽可能使用 gh CLI
- 优先使用
gh命令,而非直接调用 API - gh 会安全地存储凭据
- 错误处理更完善,输出也更易于用户理解
- 优先使用
-
切勿将凭据放入 Git URL
- URL 中的凭据可能会泄露在 git 历史记录、日志和错误消息中
- 请改用 SSH 密钥或 git 凭据助手
- 仅在安全的 CI/CD 环境中使用 URL 凭据
-
确认 .env 已加入 Git 忽略列表
- 始终检查 .gitignore 是否包含 .env
- 切勿将凭据提交到 git
- 使用 .env.example 进行说明
-
定期轮换令牌
- 应定期轮换 GitHub PAT
- 轮换后撤销旧令牌
- 使用新令牌更新 .env 文件
错误处理
如果身份验证失败:
- 确认 .env 文件中的 PAT 有效
- 检查 PAT 是否具有所需的范围(repo、workflow 等)
- 确认 PAT 尚未过期
- 使用
gh auth status进行测试
示例
示例 1:创建新仓库并推送代码
# Load credentials (Linux/macOS):
source ./.env
# Load credentials (Windows PowerShell):
# Get-Content .\.env | ForEach-Object { if ($_ -match '^([^=]+)=(.*)$') { [Environment]::SetEnvironmentVariable($matches[1], $matches[2]) } }
# Create private repository
gh repo create yourusername/my-new-repo --private --description "My new project"
# Initialize local repo and push
git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin https://github.com/yourusername/my-new-repo.git
git push -u origin main
示例 2:克隆私有仓库(SSH - 推荐)
# Clone with SSH (most secure)
git clone [email protected]:yourusername/private-repo.git
示例 2b:使用凭据助手克隆
# First time setup (one-time)
git config --global credential.helper store
# Clone - will prompt for credentials first time, then cache
git clone https://github.com/yourusername/private-repo.git
示例 3:API 请求
# Load credentials (Linux/macOS):
source ./.env
# Load credentials (Windows PowerShell):
# Get-Content .\.env | ForEach-Object { if ($_ -match '^([^=]+)=(.*)$') { [Environment]::SetEnvironmentVariable($matches[1], $matches[2]) } }
# List user's repositories (Linux/macOS):
curl -s -H "Authorization: token $GITHUB_PAT" \
https://api.github.com/user/repos | jq -r '.[].full_name'
# Windows PowerShell:
# $headers = @{ Authorization = "token $env:GITHUB_PAT" }
# (Invoke-RestMethod -Uri "https://api.github.com/user/repos" -Headers $headers).full_name
备注
- GitHub CLI(gh)是执行 GitHub 操作的推荐方式
- PAT 应根据所需操作具备适当的范围
- 凭据文件受 .gitignore 保护
- 对于 CI/CD,请改用 GitHub Actions secrets,而非 .env 文件
- 可考虑使用 SSH 密钥执行 git 操作,以替代通过 HTTPS 使用 PAT
评论
支持 Markdown