# 从配置文件自动设置 GitHub 仓库 # 用途:读取 .github-config 文件并配置 Git 远程仓库 param( [string]$ConfigFile = ".github-config" ) Write-Host "=== 从配置文件设置 GitHub 仓库 ===" -ForegroundColor Cyan Write-Host "" # 检查配置文件是否存在 if (-not (Test-Path $ConfigFile)) { Write-Host "错误: 配置文件 $ConfigFile 不存在" -ForegroundColor Red Write-Host "请先复制 .github-config.example 为 .github-config 并填写配置" -ForegroundColor Yellow exit 1 } # 读取配置文件 Write-Host "读取配置文件: $ConfigFile" -ForegroundColor Yellow $config = @{} Get-Content $ConfigFile | ForEach-Object { if ($_ -match '^\s*([^#=]+?)\s*=\s*(.+?)\s*$') { $key = $matches[1].Trim() $value = $matches[2].Trim() $config[$key] = $value } } # 验证必需配置 $requiredKeys = @('GITHUB_USERNAME', 'GITHUB_TOKEN', 'GITHUB_REPO_NAME', 'GITHUB_REPO_URL') foreach ($key in $requiredKeys) { if (-not $config.ContainsKey($key) -or [string]::IsNullOrWhiteSpace($config[$key])) { Write-Host "错误: 配置文件缺少必需的键: $key" -ForegroundColor Red exit 1 } } # 显示配置信息(隐藏 token) Write-Host "配置信息:" -ForegroundColor Cyan Write-Host " GitHub 用户名: $($config['GITHUB_USERNAME'])" -ForegroundColor Gray Write-Host " GitHub Token: $($config['GITHUB_TOKEN'].Substring(0, [Math]::Min(10, $config['GITHUB_TOKEN'].Length)))..." -ForegroundColor Gray Write-Host " 仓库名称: $($config['GITHUB_REPO_NAME'])" -ForegroundColor Gray Write-Host " 仓库 URL: $($config['GITHUB_REPO_URL'])" -ForegroundColor Gray Write-Host "" # 检查 Git 是否已初始化 if (-not (Test-Path .git)) { Write-Host "初始化 Git 仓库..." -ForegroundColor Yellow git init Write-Host "✓ Git 仓库已初始化" -ForegroundColor Green } else { Write-Host "✓ Git 仓库已存在" -ForegroundColor Green } # 配置 Git 用户信息(如果还没有) $gitUser = git config user.name 2>$null $gitEmail = git config user.email 2>$null if (-not $gitUser) { Write-Host "" Write-Host "配置 Git 用户信息..." -ForegroundColor Yellow $inputUser = Read-Host "请输入 Git 用户名 (或按 Enter 使用 GitHub 用户名: $($config['GITHUB_USERNAME']))" if ([string]::IsNullOrWhiteSpace($inputUser)) { $inputUser = $config['GITHUB_USERNAME'] } git config user.name $inputUser Write-Host "✓ Git 用户名已设置: $inputUser" -ForegroundColor Green } if (-not $gitEmail) { $inputEmail = Read-Host "请输入 Git 邮箱" if (-not [string]::IsNullOrWhiteSpace($inputEmail)) { git config user.email $inputEmail Write-Host "✓ Git 邮箱已设置: $inputEmail" -ForegroundColor Green } } # 配置远程仓库 Write-Host "" Write-Host "配置远程仓库..." -ForegroundColor Yellow $currentRemote = git remote get-url origin 2>$null if ($currentRemote) { Write-Host "当前远程仓库: $currentRemote" -ForegroundColor Cyan if ($currentRemote -ne $config['GITHUB_REPO_URL']) { $update = Read-Host "是否更新远程仓库地址? (y/n)" if ($update -eq "y" -or $update -eq "Y") { git remote set-url origin $config['GITHUB_REPO_URL'] Write-Host "✓ 远程仓库已更新" -ForegroundColor Green } } else { Write-Host "✓ 远程仓库地址已正确" -ForegroundColor Green } } else { git remote add origin $config['GITHUB_REPO_URL'] Write-Host "✓ 远程仓库已添加" -ForegroundColor Green } # 配置 Git 凭据(使用 token) Write-Host "" Write-Host "配置 Git 凭据..." -ForegroundColor Yellow # 设置 Git 凭据助手(Windows) $credentialHelper = git config credential.helper 2>$null if (-not $credentialHelper) { git config credential.helper manager-core Write-Host "✓ Git 凭据助手已配置" -ForegroundColor Green } # 提取仓库 URL 的用户名和密码部分 $repoUrl = $config['GITHUB_REPO_URL'] if ($repoUrl -match 'https://(.+?)@') { Write-Host "检测到 URL 中已包含凭据" -ForegroundColor Gray } else { # 将 token 添加到 URL 中(用于推送) $urlWithToken = $repoUrl -replace 'https://', "https://$($config['GITHUB_USERNAME']):$($config['GITHUB_TOKEN'])@" git remote set-url origin $urlWithToken Write-Host "✓ Git 凭据已配置到远程 URL" -ForegroundColor Green } # 设置默认分支 Write-Host "" Write-Host "设置默认分支..." -ForegroundColor Yellow $defaultBranch = $config['GITHUB_DEFAULT_BRANCH'] if (-not $defaultBranch) { $defaultBranch = "main" } $currentBranch = git branch --show-current 2>$null if (-not $currentBranch) { # 创建初始提交(如果还没有) Write-Host "创建初始提交..." -ForegroundColor Yellow git add . git commit -m "Initial commit: 企业微信 AI 助手" 2>$null git branch -M $defaultBranch Write-Host "✓ 已创建初始提交并设置分支为 $defaultBranch" -ForegroundColor Green } else { if ($currentBranch -ne $defaultBranch) { git branch -M $defaultBranch Write-Host "✓ 分支已重命名为 $defaultBranch" -ForegroundColor Green } } # 显示下一步操作 Write-Host "" Write-Host "=== 设置完成 ===" -ForegroundColor Green Write-Host "" Write-Host "下一步操作:" -ForegroundColor Cyan Write-Host "" Write-Host "1. 检查远程仓库配置:" -ForegroundColor Yellow Write-Host " git remote -v" -ForegroundColor Gray Write-Host "" Write-Host "2. 推送代码到 GitHub:" -ForegroundColor Yellow Write-Host " git push -u origin $defaultBranch" -ForegroundColor Gray Write-Host "" Write-Host "3. 配置 GitHub Secrets (用于 GitHub Actions):" -ForegroundColor Yellow Write-Host " 进入仓库: https://github.com/$($config['GITHUB_USERNAME'])/$($config['GITHUB_REPO_NAME'])/settings/secrets/actions" -ForegroundColor Gray Write-Host " 添加 Secrets: PROD_HOST, PROD_USER, PROD_SSH_KEY, PROD_DOMAIN" -ForegroundColor Gray Write-Host "" Write-Host "4. 查看详细文档:" -ForegroundColor Yellow Write-Host " docs/github-quickstart.md" -ForegroundColor Gray Write-Host ""