Initial commit: 浼佷笟寰俊 AI 鏈哄櫒浜哄姪鐞?MVP
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
90
scripts/push-to-github.ps1
Normal file
90
scripts/push-to-github.ps1
Normal file
@@ -0,0 +1,90 @@
|
||||
# 快速推送代码到 GitHub
|
||||
# 用途:使用配置文件中的信息自动推送代码
|
||||
|
||||
param(
|
||||
[string]$CommitMessage = "Update: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')",
|
||||
[string]$ConfigFile = ".github-config",
|
||||
[switch]$Force
|
||||
)
|
||||
|
||||
Write-Host "=== 推送代码到 GitHub ===" -ForegroundColor Cyan
|
||||
Write-Host ""
|
||||
|
||||
# 检查配置文件
|
||||
if (-not (Test-Path $ConfigFile)) {
|
||||
Write-Host "错误: 配置文件 $ConfigFile 不存在" -ForegroundColor Red
|
||||
Write-Host "请先运行: .\scripts\setup-github-from-config.ps1" -ForegroundColor Yellow
|
||||
exit 1
|
||||
}
|
||||
|
||||
# 读取配置
|
||||
$config = @{}
|
||||
Get-Content $ConfigFile | ForEach-Object {
|
||||
if ($_ -match '^\s*([^#=]+?)\s*=\s*(.+?)\s*$') {
|
||||
$key = $matches[1].Trim()
|
||||
$value = $matches[2].Trim()
|
||||
$config[$key] = $value
|
||||
}
|
||||
}
|
||||
|
||||
$repoUrl = $config['GITHUB_REPO_URL']
|
||||
$defaultBranch = $config['GITHUB_DEFAULT_BRANCH']
|
||||
if (-not $defaultBranch) {
|
||||
$defaultBranch = "main"
|
||||
}
|
||||
|
||||
# 检查 Git 状态
|
||||
Write-Host "检查 Git 状态..." -ForegroundColor Yellow
|
||||
$status = git status --porcelain 2>$null
|
||||
if ($status) {
|
||||
Write-Host "发现更改的文件:" -ForegroundColor Cyan
|
||||
git status --short
|
||||
Write-Host ""
|
||||
|
||||
# 添加所有更改
|
||||
Write-Host "添加所有更改..." -ForegroundColor Yellow
|
||||
git add .
|
||||
Write-Host "✓ 文件已添加到暂存区" -ForegroundColor Green
|
||||
|
||||
# 提交
|
||||
Write-Host "提交更改..." -ForegroundColor Yellow
|
||||
Write-Host "提交信息: $CommitMessage" -ForegroundColor Gray
|
||||
git commit -m $CommitMessage
|
||||
Write-Host "✓ 更改已提交" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "✓ 没有需要提交的更改" -ForegroundColor Green
|
||||
}
|
||||
|
||||
# 检查远程仓库
|
||||
$currentRemote = git remote get-url origin 2>$null
|
||||
if (-not $currentRemote) {
|
||||
Write-Host "错误: 未配置远程仓库" -ForegroundColor Red
|
||||
Write-Host "请先运行: .\scripts\setup-github-from-config.ps1" -ForegroundColor Yellow
|
||||
exit 1
|
||||
}
|
||||
|
||||
# 推送
|
||||
Write-Host ""
|
||||
Write-Host "推送到 GitHub..." -ForegroundColor Yellow
|
||||
Write-Host "远程仓库: $currentRemote" -ForegroundColor Gray
|
||||
Write-Host "分支: $defaultBranch" -ForegroundColor Gray
|
||||
Write-Host ""
|
||||
|
||||
if ($Force) {
|
||||
Write-Host "⚠ 使用 --force 推送(危险操作)" -ForegroundColor Red
|
||||
git push -u origin $defaultBranch --force
|
||||
} else {
|
||||
git push -u origin $defaultBranch
|
||||
}
|
||||
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
Write-Host ""
|
||||
Write-Host "✓ 代码已成功推送到 GitHub" -ForegroundColor Green
|
||||
Write-Host ""
|
||||
Write-Host "查看仓库: $repoUrl" -ForegroundColor Cyan
|
||||
Write-Host "GitHub Actions: $repoUrl/actions" -ForegroundColor Cyan
|
||||
} else {
|
||||
Write-Host ""
|
||||
Write-Host "✗ 推送失败,请检查错误信息" -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
162
scripts/setup-github-from-config.ps1
Normal file
162
scripts/setup-github-from-config.ps1
Normal file
@@ -0,0 +1,162 @@
|
||||
# 从配置文件自动设置 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 ""
|
||||
162
scripts/setup-github.ps1
Normal file
162
scripts/setup-github.ps1
Normal file
@@ -0,0 +1,162 @@
|
||||
# GitHub Actions 部署快速设置脚本
|
||||
# 用途:生成 SSH 密钥、准备 GitHub 推送
|
||||
|
||||
Write-Host "=== GitHub Actions 部署快速设置 ===" -ForegroundColor Cyan
|
||||
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
|
||||
}
|
||||
|
||||
# 检查是否已配置远程仓库
|
||||
$remoteUrl = git remote get-url origin 2>$null
|
||||
if ($remoteUrl) {
|
||||
Write-Host ""
|
||||
Write-Host "当前远程仓库: $remoteUrl" -ForegroundColor Cyan
|
||||
$changeRemote = Read-Host "是否更改远程仓库地址? (y/n)"
|
||||
if ($changeRemote -eq "y" -or $changeRemote -eq "Y") {
|
||||
$newUrl = Read-Host "请输入新的 GitHub 仓库 URL"
|
||||
git remote set-url origin $newUrl
|
||||
Write-Host "✓ 远程仓库已更新" -ForegroundColor Green
|
||||
}
|
||||
} else {
|
||||
Write-Host ""
|
||||
Write-Host "未配置远程仓库" -ForegroundColor Yellow
|
||||
$setupRemote = Read-Host "是否现在配置? (y/n)"
|
||||
if ($setupRemote -eq "y" -or $setupRemote -eq "Y") {
|
||||
$githubUrl = Read-Host "请输入 GitHub 仓库 URL (例如: https://github.com/username/repo.git)"
|
||||
git remote add origin $githubUrl
|
||||
Write-Host "✓ 远程仓库已添加" -ForegroundColor Green
|
||||
}
|
||||
}
|
||||
|
||||
# 生成 SSH 密钥
|
||||
Write-Host ""
|
||||
Write-Host "=== 生成 SSH 密钥 ===" -ForegroundColor Cyan
|
||||
$sshKeyPath = "$env:USERPROFILE\.ssh\github-actions"
|
||||
$sshKeyPubPath = "$sshKeyPath.pub"
|
||||
|
||||
if (Test-Path $sshKeyPath) {
|
||||
Write-Host "SSH 密钥已存在: $sshKeyPath" -ForegroundColor Yellow
|
||||
$regenerate = Read-Host "是否重新生成? (y/n)"
|
||||
if ($regenerate -ne "y" -and $regenerate -ne "Y") {
|
||||
Write-Host "跳过 SSH 密钥生成" -ForegroundColor Gray
|
||||
} else {
|
||||
Remove-Item $sshKeyPath -Force -ErrorAction SilentlyContinue
|
||||
Remove-Item $sshKeyPubPath -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
|
||||
if (-not (Test-Path $sshKeyPath)) {
|
||||
Write-Host "正在生成 SSH 密钥..." -ForegroundColor Yellow
|
||||
ssh-keygen -t ed25519 -C "github-actions-deploy" -f $sshKeyPath -N '""' | Out-Null
|
||||
Write-Host "✓ SSH 密钥已生成" -ForegroundColor Green
|
||||
}
|
||||
|
||||
# 显示公钥和私钥
|
||||
Write-Host ""
|
||||
Write-Host "=== SSH 密钥信息 ===" -ForegroundColor Cyan
|
||||
Write-Host ""
|
||||
Write-Host "公钥(需要添加到服务器的 ~/.ssh/authorized_keys):" -ForegroundColor Yellow
|
||||
Write-Host "---"
|
||||
Get-Content $sshKeyPubPath
|
||||
Write-Host "---"
|
||||
Write-Host ""
|
||||
|
||||
Write-Host "私钥(需要添加到 GitHub Secrets 的 PROD_SSH_KEY):" -ForegroundColor Yellow
|
||||
Write-Host "---"
|
||||
Get-Content $sshKeyPath
|
||||
Write-Host "---"
|
||||
Write-Host ""
|
||||
|
||||
# 保存到文件
|
||||
$pubKeyFile = "github-actions.pub"
|
||||
$privKeyFile = "github-actions.key"
|
||||
Copy-Item $sshKeyPubPath $pubKeyFile -Force
|
||||
Copy-Item $sshKeyPath $privKeyFile -Force
|
||||
Write-Host "✓ 密钥已保存到项目根目录:" -ForegroundColor Green
|
||||
Write-Host " - $pubKeyFile (公钥)" -ForegroundColor Gray
|
||||
Write-Host " - $privKeyFile (私钥)" -ForegroundColor Gray
|
||||
Write-Host ""
|
||||
Write-Host "⚠ 注意: 请妥善保管私钥文件,不要提交到 Git!" -ForegroundColor Red
|
||||
|
||||
# 检查 .gitignore
|
||||
Write-Host ""
|
||||
Write-Host "=== 检查 .gitignore ===" -ForegroundColor Cyan
|
||||
if (Test-Path .gitignore) {
|
||||
$gitignoreContent = Get-Content .gitignore -Raw
|
||||
if ($gitignoreContent -notmatch "github-actions\.key") {
|
||||
Add-Content .gitignore "`n# GitHub Actions SSH Key`ngithub-actions.key`n"
|
||||
Write-Host "✓ 已添加 github-actions.key 到 .gitignore" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "✓ .gitignore 已包含 github-actions.key" -ForegroundColor Green
|
||||
}
|
||||
} else {
|
||||
Write-Host "创建 .gitignore..." -ForegroundColor Yellow
|
||||
@"
|
||||
# GitHub Actions SSH Key
|
||||
github-actions.key
|
||||
"@ | Out-File .gitignore -Encoding UTF8
|
||||
Write-Host "✓ .gitignore 已创建" -ForegroundColor Green
|
||||
}
|
||||
|
||||
# 生成 GitHub Secrets 配置模板
|
||||
Write-Host ""
|
||||
Write-Host "=== GitHub Secrets 配置模板 ===" -ForegroundColor Cyan
|
||||
$secretsTemplate = @"
|
||||
# GitHub Secrets 配置清单
|
||||
# 进入仓库: Settings → Secrets and variables → Actions → New repository secret
|
||||
|
||||
## 必需 Secrets
|
||||
|
||||
PROD_HOST=你的服务器IP
|
||||
PROD_USER=你的SSH用户名(通常是 root 或 ubuntu)
|
||||
PROD_SSH_KEY=上面的私钥内容(github-actions.key 文件内容)
|
||||
PROD_DOMAIN=你的生产域名(例如: api.yourdomain.com)
|
||||
|
||||
## 可选 Secrets
|
||||
|
||||
PROD_SSH_PORT=22
|
||||
PROD_APP_PATH=/opt/wecom-ai-assistant
|
||||
GHCR_TOKEN=(可选,默认使用 GITHUB_TOKEN)
|
||||
|
||||
## 配置步骤
|
||||
|
||||
1. 复制上面的私钥内容
|
||||
2. 进入 GitHub 仓库 → Settings → Secrets and variables → Actions
|
||||
3. 点击 New repository secret
|
||||
4. 依次添加上述 Secrets(名称和值)
|
||||
5. 确保 Workflow permissions 设置为 "Read and write permissions"
|
||||
"@
|
||||
|
||||
$secretsFile = "GITHUB_SECRETS_TEMPLATE.md"
|
||||
$secretsTemplate | Out-File $secretsFile -Encoding UTF8
|
||||
Write-Host "✓ 配置模板已保存到: $secretsFile" -ForegroundColor Green
|
||||
|
||||
# 显示下一步操作
|
||||
Write-Host ""
|
||||
Write-Host "=== 下一步操作 ===" -ForegroundColor Cyan
|
||||
Write-Host ""
|
||||
Write-Host "1. 将公钥添加到服务器:" -ForegroundColor Yellow
|
||||
Write-Host " ssh user@your-server" -ForegroundColor Gray
|
||||
Write-Host " mkdir -p ~/.ssh" -ForegroundColor Gray
|
||||
Write-Host " echo '$(Get-Content $pubKeyFile)' >> ~/.ssh/authorized_keys" -ForegroundColor Gray
|
||||
Write-Host " chmod 600 ~/.ssh/authorized_keys" -ForegroundColor Gray
|
||||
Write-Host ""
|
||||
Write-Host "2. 配置 GitHub Secrets:" -ForegroundColor Yellow
|
||||
Write-Host " 查看文件: $secretsFile" -ForegroundColor Gray
|
||||
Write-Host ""
|
||||
Write-Host "3. 推送代码到 GitHub:" -ForegroundColor Yellow
|
||||
Write-Host " git add ." -ForegroundColor Gray
|
||||
Write-Host " git commit -m 'Initial commit'" -ForegroundColor Gray
|
||||
Write-Host " git push -u origin main" -ForegroundColor Gray
|
||||
Write-Host ""
|
||||
Write-Host "4. 在生产服务器上准备:" -ForegroundColor Yellow
|
||||
Write-Host " 参见: docs/github-quickstart.md" -ForegroundColor Gray
|
||||
Write-Host ""
|
||||
Write-Host "✓ 设置完成!" -ForegroundColor Green
|
||||
Reference in New Issue
Block a user