::: tip
使用 github action 实现个人 vuepress 博客自动部署到阿里云服务器
:::

当 github 仓库发生 push 操作时,action 会通过 ssh(配置公钥与私钥与阿里云服务器免密连接)传送到 nginx 服务器的静态资源目录下。

写在前面的

因为在我购买阿里云服务器时选择的系统配置是 windows server 2019 版本的,所以不能通过 ssh 来实现阿里云服务器与本地电脑实现 ssh 免密连接,进而也不能实现 github action 与阿里云服务器的免密连接。

选项卡

最后的解决方案是更改阿里云服务器的系统配置。

选项卡

更改服务器系统配置的具体操作如下:

选项卡
选项卡
选项卡

正文开始

生成一个新的 ssh key:

将生成的 ssh 公钥 public key 添加到 github 中:

选项卡
选项卡
选项卡

遇到的主要问题

选项卡

添加阿里云服务器.ssh 目录下的 ssh 私钥 private key 到 github 当前项目的 setting》secrets 中:

选项卡

将公钥写入 authorized_keys 文件中的命令是:

1
[root@iZm5e8dv3a3meqZ .ssh]# cat id_rsa.pub >> ~/.ssh/authorized_keys

打开 authorized_keys 文件中的命令是:

1
vim authorized_keys

退出 authorized_keys 文件中的命令是:

1
:wq

删除文件命令:rm -f authorized_keys

我的 github action(nodejs)配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [12.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build --if-present
env:
CI: true
# Deploy
- name: Deploy
uses: easingthemes/[email protected]
env:
SSH_PRIVATE_KEY: ${{ secrets.ALIYUN }}
ARGS: "-rltgoDzvO --delete"
SOURCE: "./docs/.vuepress/dist/"
REMOTE_HOST: "你的阿里云公网ip"
REMOTE_USER: "用户名:如root"
TARGET: "web服务器静态文件托管文件地址如:/usr/share/nginx"

写在后面的

this all thank you!

References

  1. 阿里云服务器-自动部署一键部署(哔哩哔哩视频)
  2. Centos 7 下安装配置 Nginx
  3. GitHub Actions 入门教程
  4. ssh key 与 github 配置(实现本地电脑/阿里云服务器与 github 的免密通信)
  5. Linux 学习笔记–rm 命令(删除文件或目录)

__END__