乐趣区

关于hexo:优雅的将hexo到github和coding

原文发表于我的博客,特此做版权申明
CSDN: 优雅的将 hexo 到 github 和 coding
Noosphere.site: 优雅的将 hexo 到 github 和 coding

1. 前言

上次写了篇文章 应用 github action 同时部署 hexo 到 github 和 coding 最优雅的形式,从实现原理的角度做了介绍,因为实现的指标比拟多,所以看起来比较复杂,这次咱们不讲原理,仅仅按实现目标来介绍如何配置。

1.1 指标

先说咱们要实现的指标,而后再按指标来介绍办法

  1. hexo 的 source 寄存在独立公有库 jiuhao
  2. 生成的动态文件寄存在 github 和 coding 独立的库
  3. 本地能够通过 hexo d -g一个命令一次性部署到 github 和 coding
  4. 能够通过 git push 推送到 github 的 surce 仓库后,由 action 触发部署到 github 和 coding 的动态仓库
  5. 同时实现 3 和 4,只须要配置一次_config.yml。而不须要在 action 外面写死指标动态仓库的地址,不便保护
  6. 反对多个 github 账号,同时也反对多个 coding 账号

阐明:

  • 指标 1 把 source 放在独立的公有库是安全性思考
  • 指标 3 和 4 是不便能够应用两种办法都能达到部署的指标,3 是手工部署,4 是通过 git push 触发,比方 github 的 action 可能某些起因失败了,临时又没工夫去保护,那么我能够间接在本地 hexo d -g 部署就行
  • 指标 5 是实现 3 和 4 的时候,两种办法的指标仓库 (publish repo) 地址配置只须要一处保护,不须要扩散在 _config.yml 和 github action
  • 指标 6 是可选指标。如果不须要辨别工作和私人用的 github 账号,能够不须要理睬这个指标。

1.2 已有的解决方案剖析

为了达到下面这些指标,我找了一轮市面上各种各样的解决方案,没看到齐全符合要求的。次要问题包含

  • github action market 上的 hexo action 没有反对同时部署到 github 和 coding 的 action 能够间接重用
  • github action market 上已有的 hexo action 的 deploy 形式无非两种,一种是间接把指标 (publish repo) 仓库地址配置 action,一种是间接应用 source 外面_config.yml 的配置. 如果是前者,则意味着咱们须要吧 publish repo 写在两个中央,一个是本地的_config.yml 一个是 action。保护起来比拟麻烦。显著应该应用后者。
  • 其余的一些博客提到的办法外面,大多存在后面一点存在的问题,而且大多是间接在 action 里定制脚本,没有抽成 action,不不便重用。

就这样,观摩了一轮,我决定 fork 一个 github mark 上的 hexo action 进去,按需要本人弄一个 action,通过考查,[sma11black/hexo-action](https://github.com/sma11black/hexo-action)这个 action 比拟靠近需要,只须要简略革新就能够应用,怎么革新这里就不说了,{% post_link hexo-action-deploy-to-coding-and-multi-account 见上一篇文章 %}

通过简略革新的 action 的仓库是 [noosphere-coder/hexo-action@master](https://github.com/noosphere-coder/hexo-action), 上面咱们间接应用就好

2 注释

2.1 只实现目标 1 - 5 的配置

排除掉指标 6,只须要是实现下面说的 1 - 5 的指标,也即是咱们能够应用咱们平时曾经在 github 和 coding 外面应用的 ssh key 就好。那么配置很简略

  1. 配置 hexo 的 deploy

找到 hexo 根目录的_config.yml, 而后配置 deploy 字段的内容如下

deploy:
  type: 'git'
  repo: 
    github: 'git@noosphere-coder.github.com:noosphere-coder/noosphere-coder.github.io.git'
    coding: 'git@e.coding.net:noosphere/noosphere.git'
  branch: 'master'
  1. 配置 github CI Actions

name: CI

on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    container:
      image: node:13-alpine

    steps:
    - uses: actions/checkout@v1
      with:
        submodules: true 
    
    - name: Install Dependencies
      run: |
        npm install

    - name: Deploy
      id: deploy
      uses: noosphere-coder/hexo-action@master
      with:
        deploy_key: ${{secrets.DEPLOY_KEY}}
        # user_name: your github username  # (or delete this input setting to use bot account)
        # user_email: your github useremail  # (or delete this input setting to use bot account)
        commit_msg: ${{github.event.head_commit.message}}  
    - name: Get the output
      run: |
        echo "${{steps.deploy.outputs.notify}}"
  1. ssh key 配置
    3.1 私钥配置: 把 /home/$USER/ssh/id_rsa 的内容复制进去,在入口:{公有库}->settings->Secrets->New secret,新建名为 DEPLOY_KEY 的 key,退出 id_rsa 的内容
    3.2 公钥配置: 把/home/$USER/ssh/id_rsa.pub 别离配置在 github 和 coding 的指标仓库

    • github 的配置入口是 {指标仓库}->settings->Deploy keys->add deploy keys
    • coding 的配置入口是 {指标仓库}-> 设置 -> 部署公钥 -> 新建部署公钥

至此,实现 1 - 5 指标的配置曾经实现,也就是,咱们能够别离用上面这两种形式都能够同时部署到 github 和 coding

hexo g -d # 或者 hexo d -g

git push origin master

2.2 [可选, 不须要能够跳过] 实现目标 6:应用独立的 ssh key 来部署(也就是能够新建多个的 github 账号来部署你的 hexo 站点)

指标 6 是采纳另外的 ssh key,而不是间接应用/home/$USER/ssh/id_rsa, 一般来说,这样作无非是这几种目标

  1. 辨别私人的 id 和工作用的 key
  2. 不要间接应用 id_rsa, 缩小安全隐患
  3. 同时保护多个隔离的 hexo 站点

为了达到这个目标,咱们须要通知 ssh,在碰到这个 hexo 的仓库的时候,请应用独立的 key。上面咱们来实现这个指标

  1. 生成独立的 key 用于部署(这里的 key 命名是 nooshpere-coder)
ssh-keygen noosphere-coder
  1. 通知 ssh 进行路由, 在终端执行上面命令,生成一份 config 配置到 /home/$USER/.ssh 目录
cat << EOF > /home/$USER/.ssh/config
Host github.com  
    HostName github.com  
    PreferredAuthentications publickey  
    IdentityFile /home/$USER/.ssh/id_rsa

Host noosphere-coder.github.com  
    HostName github.com  
    PreferredAuthentications publickey 
    IdentityFile /home/$USER/.ssh/noosphere-coder

Host e.coding.net  
    HostName e.coding.net  
    PreferredAuthentications publickey  
    IdentityFile /home/$USER/.ssh/id_rsa 

Host noosphere-coder.coding.net  
    HostName e.coding.net  
    PreferredAuthentications publickey  
    IdentityFile /home/$USER/.ssh/noosphere-coder
EOF
  1. ssh key 配置,和下面的 ssh key 配置 配置步骤一样,只须要把 id_rsa 改成 noosphere-coder 的内容就行了。
  2. 把公有仓库的 remote 改掉
git remote set-url origin git@noosphere-coder.github.com:noosphere-coder/hexo-action.git

以上 noosphere-coder 请替换成你本人的仓库名称

至此,指标 6 曾经实现,这个时候,你再去执行 git push 的时候,ssh 会主动应用独立的 key 而不会应用默认的 id_rsa.

3. DNS 双线配置

同时部署到 github 和 coding 次要是为了国内拜访和国外拜访流量辨别,一个为了速度,二者为了搜索引擎收录(github 封了百度的爬虫)

这个配置,切实没什么好讲的,间接上图吧

  1. DSN 服务商的控制面板配置

  1. github 的域名配置

  1. coding 的的域名配置

原文公布于:
我的博客 优雅的将 hexo 到 github 和 coding – Noosphere
​公众号 优雅的将 hexo 到 github 和 coding


关注公众号和我互动

退出移动版