原文发于我的博客和公众号:“Noosphere博客”,“智圈云公众号”,理解相干信息能够关注“智圈云”
指标
咱们晓得应用github action 能够很简略的部署hexo的动态文件到github pages,然而如果在国内咱们心愿部署到github pages同时也部署到coding,而后通过dns双线路由,另外,咱们可能有多个账号,比方公司的和集体的博客或者网站,也是同时部署到coding和github,那就这个github action解决不了,上面咱们革新一下,使其达到这个指标:
- hexo的source寄存在独立库
- 生成的动态文件存在独立的库
- 提交markdown文件后,主动生成动态文件
- 主动部署到github pages
- 主动部署到coding
- 同一份hexo source库,只须要配置一次hexo的
_config.yml
,就能够间接通过hexo deploy -g
或者git push
来触发部署 - 反对多个github账号,同时也反对多个coding账号
配置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'
这个配置指标是让咱们间接hexo deploy
能够同时推送到 github 和 coding 的 pages 仓库
配置 hexo deploy命令反对多个github和coding账号
一般来说,如果我只有一个github的账号,在这个配置下间接执行hexo g -d
,一个命令就能够间接实现两个仓库的部署了。
那么如果咱们的github账号有多个,比方有一个办公用的,一个私人的,那怎么办?
咱们晓得 git 的 ssh 推送形式是须要应用特定的 key 的, 所以,咱们只须要配置 ssh 来路由特定的域名到key即可.
依据需要,在配置这个ssh key的路由之前,咱们要学生成一个key用于做pages部署
ssh-keygen noosphere-coder
而后一路回车就行(不须要太强的安全性的话),生成后key在/home/$USER/.ssh/
目录下
为了这个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也相似)。
接下来,咱们用这个key来配置ssh key的路由,达到执行git push
命令的时候主动应用不同的key:
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 }}"
配置阐明:
- 在这个配置外面,咱们use了一个action,
[noosphere-coder/hexo-action@master](https://github.com/noosphere-coder/hexo-action)
,这个是我依据指标定制的一个action,来自于[sma11black/hexo-action](https://github.com/sma11black/hexo-action)
- 这里没有应用dependency cache,因为
node-sass
这本地构建的包在cache的状况下存在版本不统一的问题,临时没找到解决办法
那么,这里咱们为什么要定制一个本人的action,起因是smallblack/hexo-action
不反对同时推送到github和coding
因而,咱们fork这个action的仓库来革新一下
革新 smallblack/hexo-action
的entrypoint.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."
这个革新也很简略
- 把咱们下面生成的
noosphere-coder
的这个ssh key 配置成 变量,而后把它生成为跑action的容器的ssh key/root/.ssh/key
- 把coding的证书退出的known_hosts
- 配置ssh key 路由
看到第一点,咱们就晓得,咱们须要把noosphere-coder的ssh key的秘钥退出到source仓库的secret key,并且命名为 DEPLOY_KEY
:
- 关上你github的source 仓库
- 点击
settings
- 点击
Secrets
- 点击
New secret
而后把本地的/home/$USER/.ssh/noosphere-coder
的内容复制进去即可。
总结
- 通过以上
折腾
,咱们实现了一个比拟优雅的hexo部署形式,既能够间接用在本地一条命令hexo g -d
间接部署到github和coding,也能够通过git push
来触发这个同时部署,而且github和coding的动态pages的仓库配置只须要在hexo的_config.yml配置一次就能够了。 - 同时,咱们这个办法反对在机器上应用多个github或者coding账号,能够辨别同时领有如办公和私人账号的这类状况
欢送关注公众号和我互动
发表回复