前幾天寫了個基于 rsync 進行文件同步的 Action -> rsync-deploy-action。目的有三個:
- 1、深入了解波 GitHub Actions,感受下 GitHub 的文檔;
- 2、個人博客在我的騰訊云 CVM 服務器上是部署有一份的「域名:shan333.cn」,之前的博客同步方式是通過 Linux 的定時任務,覺得不太行,當前博客的更新并沒有那么頻繁,沒必要每隔幾個小時就
git pull一下,且服務器還掛著其他東西,性能還是有點損耗的,換成通過 rsync 進行主動推送的方式好點; - 3、熟悉波 SSH 協(xié)議和 rsync 協(xié)議。
今天擼一篇文章簡單記錄下這次折騰。
rsync-deploy-action 的創(chuàng)建
挑 rsync 協(xié)議,不挑 FTP 或 scp 的新建 GitHub Actions 的部分原因是 rsync 可以做增量備份。GitHub Action 有三種類別,即 Docker container、JavaScript 和 Composite run steps。這次手擼的 Action 歸屬于 GitHub 官方文檔介紹的 Docker container action。rsync-deploy-action 已經發(fā)布到 GitHub 的 Marketplace。源倉庫地址:https://github.com/yeshan333/rsync-deploy-action。
rsync 與其他文件傳輸工具(如 FTP 或 scp)不同,rsync 的最大特點是會檢查發(fā)送方和接收方已有的文件,僅傳輸有變動的部分(默認規(guī)則是文件大小或修改時間有變動)。
rsync-deploy-action 基于 SSH 協(xié)議進行文件的遠程同步,從元數據文件 action.yml 可以看到,Action 支持 8 個參數「6個必選,2個可選」。
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 執(zhí)行日志可以一窺 GitHub Actions 背后是如何執(zhí)行的。 Action 的 with 參數的傳遞在一定程度上利用了 Dockerfile 的 ENTRYPOINT。
# 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 服務器的過程。
Hexo 博客同步到云服務器
由于 rsync 是基于 SSH 協(xié)議的,rsync-deploy-action 需要使用到 SSH 私鑰,所以需要先創(chuàng)建一個可以進行 SSH 遠程登錄的用戶,不建議直接使用 root 用戶。
SSH 密鑰登錄
1、先配置好遠程騰訊云 CVM 服務器 SSH 允許密鑰登錄。編輯 SSH 配置文件 /etc/ssh/sshd_config。
vim /etc/ssh/sshd_config
- StrictModes yes 改成 StrictModes no (去掉注釋后改成 no)
- 找到 #PubkeyAuthentication yes 改成 PubkeyAuthentication yes (去掉注釋)
- 找到 #AuthorizedKeysFile .ssh/authorized_keys 改成 AuthorizedKeysFile .ssh/authorized_keys (去掉注釋)
保存后,重啟 sshd。
systemctl restart sshd
2、新建用戶 github,用于遠程 SSH 免密登錄。
在遠程服務器執(zhí)行如下命令:
# root 用戶下創(chuàng)建用戶 github 并且指定 HOME 目錄
useradd -d /home/github github
# 也可以使用 adduser github,adduser會自動創(chuàng)建 HOME 目錄等
# 設置 github 用戶密碼,用于后續(xù) SSH 登錄公鑰的上傳
passwd github
3、在本地 PC 執(zhí)行如下命令,創(chuàng)建用于登錄的密鑰對。
# 生成密鑰對
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 博客同步
個人 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」:
- ssh_login_username:可以進行 SSH 密鑰登錄的用戶名,即之前配置好的 github 用戶。
- remote_server_ip:云服務器 IP
- ssh_private_key:SSH 登錄用戶的私鑰,即之前的 ~/.ssh/id_rsa 文件的內容
- source_path:博客相關所有文件路徑
- destination_path:同步到云服務器的目標路徑
~/shan333.cn,即/home/github/shan333.cn - rsync_args:action 的可選參數,.git 目錄是沒必要同步上傳的,排除掉
“大功告成”。rsync 協(xié)議的更多介紹可參考:WangDoc.com rsync。
More
個人的騰訊云 CVM 服務器之前為了方便維護安裝了個運維面板BT.CN,Hexo 博客是用過 Nginx Serving 的,但 Nginx 是通過運維面板安裝的,默認的 user 為 www「寶塔對 Nginx 的配置文件進行了拆分」,但同步后的博客是放在 github 用戶的 HOME 目錄下的 shan333.cn 目錄中的,www 無權限執(zhí)行博客的相關文件(403 Forbidden),所以需要操作波給 www 用戶可執(zhí)行權限,讓 Nginx Worker 可以 serving 博客:
# root 用戶下執(zhí)行,github 目錄下的所有文件 755 權限
chmod -R 755 /home/github
# 將 www 用戶添加到 github user group
usermod www -G -a www
參考
- GitHub Actions-Creating actions
- GitHub Actions-Workflow syntax
- SSH 密鑰登錄-網道文檔
- nginx-split-large-configuration-file
- Dockerfile ENTRYPOINT
本文由博客群發(fā)一文多發(fā)等運營工具平臺 OpenWrite 發(fā)布