侧边栏壁纸
  • 累计撰写 19 篇文章
  • 累计创建 22 个标签
  • 累计收到 2 条评论

目 录CONTENT

文章目录

Hexo博客自动部署与重建

Yttrium
2022-01-30 / 0 评论 / 0 点赞 / 244 阅读 / 7880 字

准备

平台

使用GitHub, 利用GitHub Actions进行自动部署
首先得有一个GitHub账号, 另外中国大陆访问GitHub有时候会有亿点点慢, 所以科学上网可以准备一下.

本地步骤重现

在本地进行一次从环境到部署的全部步骤, 记录下来.

配置

创建一个仓库

为你的博客源文件建立一个仓库, 将source文件夹commit(或者其它自定义的源文件文件夹), 这个仓库可以设为私有, 如果你更改过主题, 记得还要上传主题(themes)文件夹, 另外别忘了\_config.yml\_config.<theme>.yml这两个配置文件.
如果感觉理解困难的话, 直接把整个项目打包吧.
使用.gitignore将一些不需要的文件排除.

public/*
node_modules/*
db.json
package-lock.json
package.json

secrets

相当于在actions配置文件中使用的环境变量, 可以用来储存密钥之类的不方便在代码中出现的内容

使用Actions

在项目的Actions新建一个workflow.

# This is a basic workflow to help you get started with Actions
name: HexoAutoDeploy
# Controls when the workflow will run
on:
     # Triggers the workflow on push or pull request events but only for the main branch
     # 表示push后运行action
    push:
     # 表示main分支, 这是个数组
    branches: [ main ]
     # Allows you to run this workflow manually from the Actions tab
    workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
 # This workflow contains a single job called "build"(叫做 build的任务
     build:
         # The type of runner that the job will run on
         runs-on: ubuntu-latest
         # 步骤 内容是一个数组, 将会顺序执行
         steps:
        
             # 步骤的名字
             - name: install nodejs
                 # 调用其它actions安装nodejs
               uses: actions/setup-node@v2
               with:
                   node-version: '16.13.0'
                     
            # 环境配置
            - name: Configuration environment
                 # run后写需要执行的命令
              run: |
              # 设置时区
              sudo timedatectl set-timezone "Asia/Shanghai"
              # 安装博客
              npm install hexo -g
              # 创建目录
              mkdir <hexo-root>
              # 进入
              cd <hexo-root>
              # 初始化
              hexo init .
              npm install
              # 安装渲染插件以及其它插件
              npm install hexo-renderer-pug --save
              npm install hexo-renderer-stylus --save
              npm install hexo-wordcount --save
              npm install hexo-tag-dplayer --save
              npm install hexo-generator-sitemap --save
              npm install hexo-generator-searchdb --save
              # 返回
              cd ..
            
            - name: checkout repo
              # 这个action将clone一个repo到本地, 默认是当前repo  
              uses: actions/checkout@v2
              with:
                  path: <path> # 后面用到的路径

              # 将所需配置文件放到博客相应目录中
            - name: set files  
              run: |
              # 博客文章目录
              cp -r <path>/source <hexo-root>/
              # 主题, 如果有的话
              cp -r <path>/themes/ <hexo-root>/
              # 配置文件
              cp <path>/_config* <hexo-root>/
            
            # 生成静态网页文件
            - name: generate
              run: |
              cd hexo
              hexo g -d
              cd ..
              
            # 使用rsync 将生成的文件传到你的服务器上, 或者部署到GitHub Pages
            - name: rsync deployments
              uses: burnett01/[email protected]
              with:
                  switches: -avr --delete # rsync使用的参数
                  path: <hexo-root>/public/
                  remote_path: /usr/share/nginx/html/ # 主机上的目录
                  remote_host: # 主机
                  remote_user: # 用户名
                  remote_key: ${{ secrets.SSH_KEY }} # 密钥,如果需要的话  

可能的问题

unknown block tag:

出现这个问题可能是跟主题有关, 也可能是和插件有关

  1. 在steps中合适位置加入 ls 命令来查看主题文件是否正确
  2. 检查环境配置是否安装完所有 hexo-tag 插件

rsync error

当出现rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1330) [sender=3.2.3]时, 检查部署服务器的文件夹权限, 重点检查报错的位置如:rsync: failed to set times on "/usr/share/nginx/html/.": Operation not permitted (1) . 此处html/. 里的.指代当前文件夹, 也就是html文件夹, 检查该文件夹的权限.

Dependencies lock file is not found

安装nodejsError: Dependencies lock file is not found in /home/runner/work/出现这个时, 删除workflow中相应位置的cache: npm.

localsearch 样式异常

在本地生成的文件显示正常, 自动部署的显示异常, 排查问题后想起来可能是CDN的缓存, 清除缓存后显示恢复正常.

尝试过但失败了

本想在自己的服务器上生成文件, 利用Actions ssh连上服务器运行提前写好的脚本, 但遇到一些问题.

私有仓库的密码问题

git pull一个私有仓库的时候需要输入用户名和密码, 这个与自动部署完全不兼容, 网上的解决方案git config --global user.name=不可行.
密码是需要在网站上自己获取的personal access token.

在网站服务器上生成

失败理由同上一个问题, 因为没办法解决输入密码问题, 所以没办法自动pull
不考虑设置成公共仓库, 因为之前就是用的公共仓库

0

评论区