优雅的将hexo到github和coding

原文发表于个人博客, 特此作版权声明
CSDN: 优雅的将hexo到github和coding
Noosphere.site: 优雅的将hexo到github和coding

1. 前言

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

1.1 目标

先说咱们要实现的目标,而后再按目标来介绍方法git

  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帐号

说明:github

  • 目标 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 已有的解决方案分析

为了达到上面这些目标,我找了一轮市面上各类各样的解决方案,没看到彻底符合要求的。主要问题包括npm

  • 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 见上一篇文章%}ubuntu

通过简单改造的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就好。那么配置很简单hexo

  1. 配置hexo的deploy

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

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的目标仓库post

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

至此,实现1-5目标的配置已经完成,也就是,咱们能够分别用下面这两种方式均可以同时部署到github和codingui

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服务商的控制面板配置

DSN服务商的控制面板配置

  1. github的域名配置

在这里插入图片描述

  1. coding的的域名配置

在这里插入图片描述

原文发布于:
CSDN: 优雅的将hexo到github和coding
个人博客 优雅的将hexo到github和coding - Noosphere
​公众号 优雅的将hexo到github和coding

欢迎关注公众号关注公众号和我互动

相关文章
相关标签/搜索