GitHub Actions自动部署博客到远程服务器
2023年11月12日
预计阅读:1min
主要分几步
- 拉代码
- 设置node版本
- 生成ssh/confg配置文件
- 安装依赖打包
- 同步打包文件到远程服务器
~/.ssh/confg配置文件
Host 别名
HostName 主机名
Port 端口
User 用户名
IdentityFile 密钥文件的路径
IdentitiesOnly 只接受SSH key 登录
PreferredAuthentications 强制使用Public Key验证
name: next
on:
push:
branches: [ main ]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
ref: main
- name: setNodeVersion
uses: actions/setup-node@v1
with:
node-version: '18.x'
- name: set ssh
env:
SSH_KEY: ${{ secrets.NEXT_KEY }}
run: |
mkdir -p ~/.ssh/
echo "$SSH_KEY" > ~/.ssh/id_rsa
cat ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
echo -e "Host cdn.hackshen.com\nStrictHostKeyChecking no\nHostName cdn.hackshen.com\nUser root\nIdentityFile ~/.ssh/id_rsa\n" >> ~/.ssh/config
# 使用云开发 Github Action 部署
- name: npm install
run: |
npm install
npm run build
- name: init rsync
env:
USE_SSH: true
GIT_USER: hackshen
DEPLOYMENT_BRANCH: test
run: |
cd ./out
rsync -rv --delete -e 'ssh -o stricthostkeychecking=no' ./ root@cdn.hackshen.com:/data/www/blog