惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
Google DeepMind News
Google DeepMind News
H
Help Net Security
Engineering at Meta
Engineering at Meta
D
DataBreaches.Net
MongoDB | Blog
MongoDB | Blog
Martin Fowler
Martin Fowler
T
Troy Hunt's Blog
Recent Announcements
Recent Announcements
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Security @ Cisco Blogs
S
Secure Thoughts
Y
Y Combinator Blog
D
Docker
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Blog — PlanetScale
Blog — PlanetScale
N
News and Events Feed by Topic
aimingoo的专栏
aimingoo的专栏
I
InfoQ
P
Palo Alto Networks Blog
F
Full Disclosure
C
Cyber Attacks, Cyber Crime and Cyber Security
The Register - Security
The Register - Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
H
Heimdal Security Blog
G
Google Developers Blog
Webroot Blog
Webroot Blog
腾讯CDC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
WordPress大学
WordPress大学
W
WeLiveSecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
Help Net Security
Help Net Security
The Hacker News
The Hacker News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
The Last Watchdog
The Last Watchdog
TaoSecurity Blog
TaoSecurity Blog
博客园 - 三生石上(FineUI控件)
T
Threatpost
V
V2EX
AWS News Blog
AWS News Blog
O
OpenAI News
V
Visual Studio Blog

博客园 - 冯叶青

OpenAI vs Anthropic API 对比:响应体、请求体、消息格式与工具调用 Git分支自动合并脚本:基于时间戳的冲突解决方案 Next.js lingui.js 多语言自动提取翻译键 - ats-node React实现短信验证码输入组件 Next.js 中优雅地使用 Lottie 动画 Next.js 路由参数更新最佳实践:从 replaceState 到 nextReplaceState Jenkins构建时SWR模块导入报错解决方案 解决 Jenkins 环境下 Lingui 构建报错 "btoa is not defined" git 提交 实现大图自动压缩功能 React lingui.js 多语言自动提取翻译键 - ast node html2canvas 解决截图空白问题 react 实现前端发版监测 JS根据文件名获取文件类型 JS获取本机IP地址 适用于react、vue菜单格式化工具函数 git 内容提交 实现大图拦截功能 JS实现视频截图 next.js 利用中间件(middleware.ts)实现PC与移动路由无缝切换 Android生成签名文件及对apk进行签名 JS-SDK 配置,实现微信分享功能
GitHub Actions 自动部署流程
冯叶青 · 2026-04-02 · via 博客园 - 冯叶青

GitHub Actions 自动部署流程

每次向 vpn 分支 push 代码后,GitHub Actions 会自动 SSH 到远程服务器拉取最新代码,无需手动登录操作。


一、服务器准备

1.1 安装 Docker 与 Docker Compose

# 安装 Docker
curl -fsSL https://get.docker.com | sh

# 启动并设置开机自启
systemctl enable docker
systemctl start docker

1.2 克隆仓库到服务器

# 在服务器上选择一个目录(后续填入 SSH_TARGET_DIR)
git clone https://github.com/你的用户名/docker-nginx.git /opt/docker-nginx

# 切换到部署分支
cd /opt/docker-nginx
git checkout vpn

二、生成部署专用 SSH 密钥

建议生成专用密钥,不要使用日常登录的密钥,方便后续独立管理和吊销。

服务器上执行:

# 生成 ed25519 密钥(推荐)
ssh-keygen -t ed25519 -C "github-actions-deploy" -f ~/.ssh/github_deploy -N ""

执行后会生成两个文件:

  • ~/.ssh/github_deploy私钥(填入 GitHub Secret)
  • ~/.ssh/github_deploy.pub公钥(加入服务器授权列表)

三、配置服务器授权

将公钥加入 authorized_keys,允许持有私钥的客户端(GitHub Actions)登录:

cat ~/.ssh/github_deploy.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

验证公钥已写入:

cat ~/.ssh/authorized_keys

3.2 修改 SSH 配置

确认 /etc/ssh/sshd_config 中已开启公钥认证:

vi /etc/ssh/sshd_config

确保以下几项已启用(去掉注释 # 并设为对应值):

PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

保存退出后重启 SSH 服务使配置生效:

# systemd 系统(CentOS 7+、Ubuntu 16.04+)
systemctl restart sshd

# 旧版系统
service ssh restart

四、配置 GitHub Secrets

在 GitHub 仓库页面进入:Settings → Secrets and variables → Actions → New repository secret

依次添加以下 5 个 Secret:

Secret 名称 说明 示例值
SSH_HOST 服务器 IP 或域名 192.168.44.247
SSH_USER SSH 登录用户名 root
SSH_PRIVATE_KEY 私钥完整内容 见下方说明
SSH_PORT SSH 端口(可选,默认 22) 22
SSH_TARGET_DIR 服务器上的项目路径 /opt/docker-nginx

获取私钥内容

在服务器上执行,将完整输出(含首尾行)粘贴到 SSH_PRIVATE_KEY

cat ~/.ssh/github_deploy

输出格式应如下,完整复制包括 -----BEGIN-----END

-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXkAAAAA...
...
-----END OPENSSH PRIVATE KEY-----

五、工作流说明

工作流文件位于 .github/workflows/auto-deploy.yml,触发条件:

  • 自动触发:push 到 vpn 分支时
  • 手动触发:在 GitHub → Actions → Auto Deploy → Run workflow

部署脚本执行逻辑:

cd /opt/docker-nginx        # 进入项目目录
git fetch --all --prune     # 拉取所有远程更新
git checkout vpn            # 切换到部署分支
git reset --hard origin/vpn # 强制同步到远程最新状态

注意:服务器上的本地修改会被 reset --hard 覆盖,请勿在服务器上直接修改文件。


六、首次验证

  1. 确认以上步骤全部完成
  2. 在 GitHub 仓库页面进入 Actions → Auto Deploy
  3. 点击 Run workflow → Run workflow 手动触发
  4. 查看执行日志,末尾输出 [deploy] done 表示成功

七、日常使用

# 本地修改代码后,提交并推送到 vpn 分支
git add .
git commit -m "更新配置"
git push origin vpn

push 完成后约 10~30 秒,服务器即自动同步最新代码。


八、常见问题排查

问题1:Permission denied (publickey)

  • 检查 authorized_keys 是否包含正确公钥
  • 检查 ~/.ssh/ 目录权限:chmod 700 ~/.ssh
  • 检查 authorized_keys 文件权限:chmod 600 ~/.ssh/authorized_keys

问题2:工作流跳过,未执行部署

  • 检查 GitHub Secrets 是否全部填写(SSH_HOSTSSH_USERSSH_PRIVATE_KEYSSH_TARGET_DIR 不能为空)
  • 工作流有条件判断,任一必填 Secret 为空则跳过部署步骤

问题3:not a git repository

  • 服务器上的目标目录不是 git 仓库,需先执行 git clone
  • 检查 SSH_TARGET_DIR 填写的路径是否正确

问题4:私钥格式错误

  • 确认复制的私钥包含完整的首尾行
  • 确认没有多余的空行或空格
  • 建议直接用 cat ~/.ssh/github_deploy 输出后完整复制