自动化部署

感谢小冰啵啰鸽提供的帮助

自动化部署带来的便利: 由于时间积累下来,该站点添加的资源越来越多,在经过hexo clean && hexo g && hexo d

之后花费的时间越来越长,引起的不便,于是看到了关于自动化部署.只需要对文件进行传统提交即可,actions 自动进行hexo 任务操作,

解决了不足问题。

创建github 私有仓库

  1. 请备份博客原有内容

  2. 创建仓库流程

  3. 私有仓库的作用: 存储博客源文件

  4. 创建git 密钥

    1
    2
    3
    4
    5
    6
    7
    # 新配置 密钥
    ssh-keygen -t rsa -C "github 的邮箱地址"

    # 原有密钥在 C:\Users\computer-name\.ssh
    公钥: id_rsa.pub
    密钥: id_rsa

    • github 私有仓库添加密钥

      1
      2
      3
      4
      1. 进入创建的私有仓库 
      2. 点击 Settings
      3. 在左侧导航栏目最低测有: Secrets
      4. 添加已有 git 私钥
  5. 创建tocken

创建actions

  • 在私有仓库的博客根目录下创建一下结构

    1
    2
    3
    -- .github(文件夹)
    -- workflows(文件夹)
    -- xxx.yml (文件:名称随意 )
  • 个人xxx.yml 文件内容

    说明: 个人使用两个仓库进行自动化部署

    1. 仓库一: github账户名.github.io 使用 master 分支,只用于生成类似于public下的静态文件
    2. 仓库二: 私有仓库源码存放 main 分支
    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
    # 当有改动推送到master分支时,启动Action
    name: 自动部署

    on: [push, repository_dispatch]

    jobs:
    deploy:
    runs-on: ubuntu-latest
    steps:
    - name: 检查分支
    uses: actions/checkout@v2
    with:
    ref: main #202010月后github新建仓库默认分支改为main,注意更改 私有仓库提交 main 分支

    - name: 安装 Node
    uses: actions/setup-node@v1
    with:
    node-version: "12.x"

    - name: 安装 Hexo
    run: |
    export TZ='Asia/Shanghai'
    npm install hexo-cli gulp-cli -g


    - name: 缓存 Hexo
    uses: actions/cache@v1
    id: cache
    with:
    path: node_modules
    key: ${{runner.OS}}-${{hashFiles('**/package-lock.json')}}

    - name: 安装依赖
    if: steps.cache.outputs.cache-hit != 'true'
    run: |
    npm install --save

    - name: 生成静态文件
    run: |
    gulp

    - name: 部署
    run: |
    git config --global user.name "lovobin" # 需要修改处: lovobin
    git config --global user.email "3327511395@qq.com" # 邮箱修改:
    hexo deploy

  • 注意gulp 任务

    1
    2
    3
    4
    需要在根目录下有文件: gulpfile.js

    可参考 `akilar.top/`中的静态资源有关的一篇文章

修改博客根目录下的配置文件

  • 修改repo

    1
    2
    3
    4
    # _config.yml
    repo: https://账户名称:tocken@github.com/账户名称/账户名称.github.io.git
    # repo: https://lovobin:[修改为tocken]@github.com/lovobin/lovobin.github.io.git

  • 个人遇到其他事项

    1
    魔改主题时切勿为了方便对下载内容进行覆盖操作

验证actions

在这里插入图片描述