Github Action 自动部署博客
我是用的是 hexo 静态博客,该博客原理是解析 markdown 成 HTML 文件。
背景
命令 hexo generate
会把原始的博客转换成一个可以访问的 public 文件夹(类似于前端工程的 dist 文件夹)。 把这个文件夹放服务器上 nginx 代理就是你的博客了。 我是用 github 仓库管理博客,hexo generate
生成文件夹要部署到服务器上,得把它打包到服务器上 /www/wwwroot/leijiba.cn
解压才行。
为了写完博客提交到github的后自动执行部署操作,于是有了这篇文章。
其实有网上有很多方案,我采用的是 Github Action。
Github Action
原理是推送代码到仓库后,github 会给你一个虚拟机,配置好配置文件虚拟机就会按照yaml文件执行命令。
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| name: Deploy Hexo to GitHub Pages
on: push: branches: - main
jobs: deploy: runs-on: ubuntu-latest
steps: - name: Checkout blog source uses: actions/checkout@v2 with: path: blog
- name: Set up Node.js uses: actions/setup-node@v1 with: node-version: "18"
- name: Install dependencies run: npm install working-directory: ./blog
- name: Install Hexo CLI run: npm install -g hexo-cli working-directory: ./blog
- name: Generate static pages run: hexo generate working-directory: ./blog
- name: Deploy to GitHub Pages uses: peaceiris/actions-gh-pages@v3 with: personal_token: ${{ secrets.PERSONAL_TOKEN }} publish_dir: ./blog/public external_repository: notfornothing/notfornothing.github.io publish_branch: main - name: Deploy to JD_Server uses: easingthemes/ssh-deploy@v2.1.1 env: SSH_PRIVATE_KEY: ${{ secrets.JD_SSH_PRIVATE_KEY }} ARGS: "-avzr --delete" SOURCE: "./blog/public/" REMOTE_HOST: ${{ secrets.JD_REMOTE_HOST }} REMOTE_USER: "root" TARGET: '/opt/hexo'
|