使用github action同时部署hexo到github和coding最优雅的方式

原文发于个人博客和公众号:“ Noosphere博客”,“ 智圈云公众号”,了解相关信息能够关注“ 智圈云

目标

咱们知道使用github action 能够很简单的部署hexo的静态文件到github pages,可是若是在国内咱们但愿部署到github pages同时也部署到coding,而后经过dns双线路由,另外,咱们可能有多个帐号,好比公司的和我的的博客或者网站,也是同时部署到coding和github,那就这个github action解决不了,下面咱们改造一下,使其达到这个目标:css

  1. hexo的source存放在独立库
  2. 生成的静态文件存在独立的库
  3. 提交markdown文件后,自动生成静态文件
  4. 自动部署到github pages
  5. 自动部署到coding
  6. 同一份hexo source库,只须要配置一次hexo的_config.yml,就能够直接经过hexo deploy -g 或者git push来触发部署
  7. 支持多个github帐号,同时也支持多个coding帐号

配置hexo的deploy

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

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'

这个配置目标是让咱们直接hexo deploy能够同时推送到 github 和 coding 的 pages 仓库git

配置 hexo deploy命令支持多个github和coding帐号

通常来讲,若是我只有一个github的帐号,在这个配置下直接执行hexo g -d,一个命令就能够直接完成两个仓库的部署了。 github

那么若是咱们的github帐号有多个,好比有一个办公用的,一个私人的,那怎么办?
咱们知道 git 的 ssh 推送方式是须要使用特定的 key 的, 因此,咱们只须要配置 ssh 去路由特定的域名到key便可.web

根据需求,在配置这个ssh key的路由以前,咱们要先生成一个key用于作pages部署npm

ssh-keygen noosphere-coder

而后一路回车就行(不须要太强的安全性的话),生成后key在/home/$USER/.ssh/目录下json

为了这个key能够推送到github或者coding的独立帐号,咱们须要把这个key加入到github和coding的帐号,好比我新建了一个noosphere-coder的github帐号,那么我把这个noosphere-coder的ssh key做为这个帐户的ssh key便可,打开[https://github.com/settings/keys](https://github.com/settings/keys),点击 New SSH key增长便可,加入后,咱们就能够用这个key来操做这个github帐号了(coding也相似)。ubuntu

接下来,咱们用这个key来配置ssh key的路由,达到执行git push命令的时候自动使用不一样的key:sass

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

这个配置告诉ssh,若是碰到Host为noosphere-coder.github.com或者noosphere-coder.coding.net的时候就去使用/home/$USER/.ssh/noosphere-coder这个key安全

可是,因为咱们新建的github的仓库,默认的remote url的是 git@github.com:noosphere-coder/hexo-noosphere.git(coding亦如是)
因此咱们须要修改这个仓库的remote url为git@noosphere-coder.github.com:noosphere-coder/hexo-action.git

git remote set-url origin git@noosphere-coder.github.com:noosphere-coder/hexo-action.git

coding项目也如法炮制便可。

截止目前,你用hexo g -d就能够用不一样的帐号推送到github和coding了。

配置github CI Actions

上面章节,咱们配置了git和hexo,完成了经过一个 hexo g -d的命令直接推送到github和coding,并支持多个帐号。

hexo g是在本地生成静态文件,咱们的source文件的仓库通常是放在github,而后配置为私有仓库,保证安全性,因此,咱们接下来配置github CI actions,来达到直接直接用git push来触发hexo g -d,也就是,当咱们git push的时候,CI自动生成静态文件,而后自动推送到github和coding的静态pages仓库。

下面咱们来看看最终的CI action的配置,而后再来解释

github hexo ci action

# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

# 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-and-deploy:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest
    container:
      image: node:13-alpine

    steps:
    # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
    - uses: actions/checkout@v1
      with:
        submodules: true # Checkout private submodules(themes or something else).
    
    # Caching dependencies to speed up workflows. (GitHub will remove any cache entries that have not been accessed in over 7 days.)
    # - name: Cache node modules
    #   uses: actions/cache@v1
    #   id: cache
    #   with:
    #     path: node_modules
    #     key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
    #     restore-keys: |
    #       ${{ runner.os }}-node-
    - name: Install Dependencies
      # if: steps.cache.outputs.cache-hit != 'true'
      # run: npm ci
      run: |
        npm install

    # Deploy hexo blog website.
    - name: Deploy
      id: deploy
      # uses: noosphere-coder/hexo-action@master
      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 }}  # (or delete this input setting to use hexo default settings)
    # Use the output from the `deploy` step(use for test action)
    - name: Get the output
      run: |
        echo "${{ steps.deploy.outputs.notify }}"

配置说明:

  1. 在这个配置里面,咱们use了一个action,[noosphere-coder/hexo-action@master](https://github.com/noosphere-coder/hexo-action),这个是我根据目标定制的一个action,来自于[sma11black/hexo-action](https://github.com/sma11black/hexo-action)
  2. 这里没有使用dependency cache,由于 node-sass这本地构建的包在cache的状况下存在版本不一致的问题,暂时没找到解决办法

那么,这里咱们为何要定制一个本身的action,缘由是smallblack/hexo-action不支持同时推送到github和coding

所以,咱们fork这个action的仓库来改造一下

改造 smallblack/hexo-actionentrypoint.sh

#!/bin/sh

set -e

# setup ssh-private-key
mkdir -p /root/.ssh/
echo "$INPUT_DEPLOY_KEY" > /root/.ssh/id_rsa

chmod 600 /root/.ssh/id_rsa

ssh-keyscan -t rsa github.com >> /root/.ssh/known_hosts
ssh-keyscan -t rsa e.coding.net >> /root/.ssh/known_hosts

# you can change here to router domain with defferent key with you need
cat << EOF > /root/.ssh/config
Host github.com  
    HostName github.com  
    PreferredAuthentications publickey  
    IdentityFile /root/.ssh/id_rsa

Host $GITHUB_ACTOR.github.com  
    HostName github.com  
    PreferredAuthentications publickey
    IdentityFile /root/.ssh/id_rsa

Host e.coding.net  
    HostName e.coding.net  
    PreferredAuthentications publickey  
    IdentityFile /root/.ssh/id_rsa 

Host $GITHUB_ACTOR.coding.net  
    HostName e.coding.net  
    PreferredAuthentications publickey  
    IdentityFile /root/.ssh/id_rsa 
EOF

chmod 600 /root/.ssh/config

# setup deploy git account
git config --global user.name "$INPUT_USER_NAME"
git config --global user.email "$INPUT_USER_EMAIL"

# install hexo env
npm install hexo-cli -g
npm install hexo-deployer-git --save

git clone https://github.com/$GITHUB_ACTOR/$GITHUB_ACTOR.github.io.git .deploy_git
echo 'have clone .deploy_git'

# npm remove node-sass hexo-renderer-scss
# npm install hexo-renderer-scss

# deployment
if [ "$INPUT_COMMIT_MSG" == "" ]
then
    hexo g -d
else
    hexo g -d -m "$INPUT_COMMIT_MSG"
fi

echo ::set-output name=notify::"Deploy complate."

这个改造也很简单

  1. 把咱们上面生成的noosphere-coder的这个ssh key 配置成 变量,而后把它生成为跑action的容器的ssh key /root/.ssh/key
  2. 把coding的证书加入的known_hosts
  3. 配置ssh key 路由

看到第一点,咱们就知道,咱们须要把noosphere-coder的ssh key的秘钥加入到source仓库的secret key,而且命名为 DEPLOY_KEY:

  1. 打开你github的source 仓库
  2. 点击settings
  3. 点击Secrets
  4. 点击New secret

而后把本地的/home/$USER/.ssh/noosphere-coder的内容复制进去便可。

总结

  1. 经过以上折腾,咱们完成了一个比较优雅的hexo部署方式,既能够直接用在本地一条命令hexo g -d直接部署到github和coding,也能够经过git push来触发这个同时部署,并且github和coding的静态pages的仓库配置只须要在hexo的_config.yml配置一次就能够了。
  2. 同时,咱们这个方法支持在机器上使用多个github或者coding帐号,能够区分同时拥有如办公和私人帐号的这类状况

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

相关文章
相关标签/搜索