原文發於個人博客和公衆號:「 Noosphere博客」,「 智圈雲公衆號」,瞭解相關信息能夠關注「 智圈雲」
咱們知道使用github action 能夠很簡單的部署hexo的靜態文件到github pages,可是若是在國內咱們但願部署到github pages同時也部署到coding,而後經過dns雙線路由,另外,咱們可能有多個帳號,好比公司的和我的的博客或者網站,也是同時部署到coding和github,那就這個github action解決不了,下面咱們改造一下,使其達到這個目標:css
_config.yml
,就能夠直接經過hexo deploy -g
或者git push
來觸發部署找到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
通常來講,若是我只有一個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了。
上面章節,咱們配置了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的配置,而後再來解釋
# 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 }}"
配置說明:
[noosphere-coder/hexo-action@master](https://github.com/noosphere-coder/hexo-action)
,這個是我根據目標定製的一個action,來自於[sma11black/hexo-action](https://github.com/sma11black/hexo-action)
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
看到第一點,咱們就知道,咱們須要把noosphere-coder的ssh key的祕鑰加入到source倉庫的secret key,而且命名爲 DEPLOY_KEY
:
settings
Secrets
New secret
而後把本地的/home/$USER/.ssh/noosphere-coder
的內容複製進去便可。
折騰
,咱們完成了一個比較優雅的hexo部署方式,既能夠直接用在本地一條命令hexo g -d
直接部署到github和coding,也能夠經過git push
來觸發這個同時部署,並且github和coding的靜態pages的倉庫配置只須要在hexo的_config.yml配置一次就能夠了。歡迎關注公衆號和我互動