前几天写了个基于 rsync 进行文件同步的 Action -> rsync-deploy-action。目的有三个:html
git pull
一下,且服务器还挂着其余东西,性能仍是有点损耗的,换成经过 rsync 进行主动推送的方式好点;今天撸一篇文章简单记录下此次折腾。nginx
挑 rsync 协议,不挑 FTP 或 scp 的新建 GitHub Actions 的部分缘由是 rsync 能够作增量备份。GitHub Action 有三种类别,即 Docker container、JavaScript 和 Composite run steps。此次手撸的 Action 归属于 GitHub 官方文档介绍的 Docker container action。rsync-deploy-action 已经发布到 GitHub 的 Marketplace。源仓库地址:https://github.com/yeshan333/rsync-deploy-action。git
rsync 与其余文件传输工具(如 FTP 或 scp)不一样,rsync 的最大特色是会检查发送方和接收方已有的文件,仅传输有变更的部分(默认规则是文件大小或修改时间有变更)。github
rsync-deploy-action 基于 SSH 协议进行文件的远程同步,从元数据文件 action.yml 能够看到,Action 支持 8 个参数「6个必选,2个可选」。docker
name: 'rsync-deploy-action' description: 'Synchronize files to the remote server using the SSH private key' inputs: ssh_login_username: description: 'SSH login username' required: true remote_server_ip: description: 'remote server ip' required: true ssh_port: description: 'remote server SSH port' required: true default: "22" ssh_private_key: description: 'login user SSH private key' required: true source_path: description: 'The source storage path of the synchronous files' required: true destination_path: description: 'The destination storage path of the synchronous files' required: true ssh_args: description: 'SSH args' required: false rsync_args: description: 'rsync args' required: false outputs: start_time: description: 'Start time of synchronization' end_time: # id of output description: 'End time of synchronization' runs: using: 'docker' image: 'Dockerfile' args: - ${{ inputs.ssh_login_username }} - ${{ inputs.remote_server_ip }} - ${{ inputs.ssh_port }} - ${{ inputs.ssh_private_key }} - ${{ inputs.source_path }} - ${{ inputs.destination_path }} - ${{ inputs.ssh_args }} - ${{ inputs.rsync_args }} branding: icon: 'file' color: 'green'
利用 rsync-deploy-action 的 Dockerfile 文件配合 Action 执行日志能够一窥 GitHub Actions 背后是如何执行的。 Action 的 with 参数的传递在必定程度上利用了 Dockerfile 的 ENTRYPOINT。shell
# Container image that runs your code FROM alpine:latest RUN apk update \ && apk upgrade \ && apk add --no-cache \ rsync \ openssh-client # Copies your code file from your action repository to the filesystem path `/` of the container COPY entrypoint.sh /entrypoint.sh RUN chmod +x /entrypoint.sh # Code file to execute when the docker container starts up (`entrypoint.sh`) ENTRYPOINT ["/entrypoint.sh"]
接下来介绍下利用 rsync-deploy-action 进行 Hexo 博客同步到我的腾讯云 CVM 服务器的过程。npm
因为 rsync 是基于 SSH 协议的,rsync-deploy-action 须要使用到 SSH 私钥,因此须要先建立一个能够进行 SSH 远程登陆的用户,不建议直接使用 root 用户。ubuntu
一、先配置好远程腾讯云 CVM 服务器 SSH 容许密钥登陆。编辑 SSH 配置文件 /etc/ssh/sshd_config。vim
vim /etc/ssh/sshd_config
保存后,重启 sshd。服务器
systemctl restart sshd
二、新建用户 github,用于远程 SSH 免密登陆。
在远程服务器执行以下命令:
# root 用户下建立用户 github 而且指定 HOME 目录 useradd -d /home/github github # 也可使用 adduser github,adduser会自动建立 HOME 目录等 # 设置 github 用户密码,用于后续 SSH 登陆公钥的上传 passwd github
三、在本地 PC 执行以下命令,建立用于登陆的密钥对。
# 生成密钥对 ssh-keygen -t rsa -b 4096 -C "1329441308@qq.com" # 将生成的公钥上传到云服务器,server_ip 为服务器 IP 地址,ssh_port 为 SSH 端口号 cat ~/.ssh/id_rsa.pub | ssh github@server_ip -p ssh_port "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys" # ssh 密钥登陆测试,server_ip 为服务器 IP 地址,ssh_port 为 SSH 端口号 ssh -p ssh_port -i ~/.ssh/id_rsa github@server_ip
以上操做完成没问题以后,便可使用 rsync-deploy-action 了,后面以我的 Hexo 博客的同步为例子,简单看下如何使用此 Action。
我的 Hexo 博客以前已经配置过 GitHub Action 的 workflows 进行博客的自动部署「博客源仓库:yeshan333/actions-for-hexo-blog」,因此再添加个 step 给 rsync-deploy-action 便可启用博客同步。step 声明以下:
name: Site CI on: pull_request: branches: [master] push: branches: [master] jobs: build: runs-on: ubuntu-latest steps: ...... - name: Release to GitHub Pages run: | git config --global user.email "1329441308@qq.com" git config --global user.name "yeshan333" git clone git@github.com:yeshan333/yeshan333.github.io.git .deploy_git chmod 755 -R .deploy_git hexo clean hexo generate hexo deploy - name: Push to tencentyun CVM uses: yeshan333/rsync-deploy-action@v1.0.0 with: ssh_login_username: ${{ secrets.SSH_LOGIN_USERNAME }} remote_server_ip: ${{ secrets.REMOTE_SERVER_IP }} ssh_port: ${{ secrets.SSH_PORT }} ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }} source_path: ./.deploy_git/* destination_path: ~/shan333.cn rsync_args: --exclude="./.deploy_git/.git/*"
workflow 中名为 Push to tencentyun CVM 的 step 会将 .deploy_git 下的全部文件上传到云服务器「存放到 ~/shan333.cn 目录下」,熟悉 Hexo 的朋友应该知道 hexo deploy
生成的文件会放在 .deploy_git
目录下,这里用到了 Hexo 的一个 Plugin -> hexo-deployer-git。其实也能够将 hexo generate
生成的 public 目录下的全部文件同步到云服务器。
rsync-deploy-action 使用到的参数解释「有部分数据也算是隐私数据,这里意思下用了 GitHub 的 repository secrets」:
~/shan333.cn
,即/home/github/shan333.cn
“大功告成”。rsync 协议的更多介绍可参考:WangDoc.com rsync。
我的的腾讯云 CVM 服务器以前为了方便维护安装了个运维面板BT.CN,Hexo 博客是用过 Nginx Serving 的,但 Nginx 是经过运维面板安装的,默认的 user 为 www「宝塔对 Nginx 的配置文件进行了拆分」,但同步后的博客是放在 github 用户的 HOME 目录下的 shan333.cn 目录中的,www 无权限执行博客的相关文件(403 Forbidden),因此须要操做波给 www 用户可执行权限,让 Nginx Worker 能够 serving 博客:
# root 用户下执行,github 目录下的全部文件 755 权限 chmod -R 755 /home/github # 将 www 用户添加到 github user group usermod www -G -a www
本文由博客群发一文多发等运营工具平台 OpenWrite 发布