利用GitHub Actions 自动部署 Hexo博客 全自动运行 不知所措的新哥

之后就可以直接在私人仓库source/_posts/里面添加.md文件啦,可以随时随地写文章发文章

文章来源:

不知所措的新哥
https://xin520.xyz
https://xin520.site
https://xin520.plus
https://anxinweb.github.io
https://xin-lac.vercel.app

参考借鉴来源:

GitHub Actions 来自动部署 Hexo:https://zhuanlan.zhihu.com/p/170563000
github将整个文件夹推送到自己的仓库:https://blog.csdn.net/viafcccy/article/details/85527118

一、配置github actions部分

HEXO正常运作

前提

  • node.js 环境 和 git 都已正确安装 (其实能正常运行 hexo 就已经说明正确安装了)

  • hexo 可以正常运行 可以正常部署(这里介绍部署到 github pages )

  • 配置好hexo的主题,博客名称等等

    ok,有了以上前提,可以继续了

改 _config.yml 配置

_config.yml 文件中在前提情况下,已经配置好了:

deploy:
  type: git
  repo: https://github.com/用户名/仓库名.git
  branch: master

此时我们需要将上面 repo 的配置改成 ssh 格式——即 [email protected]:用户名/仓库名.git
避免在执行 actions 时 部署出错

再次生成密钥

随便在任何文件位置可以直接右键 git bash here
复制粘贴这个 ssh-keygen -t rsa -b 4096 -C "Hexo Deploy Key" -f github-deploy-key -N ""

会在当前目录生成两个文件

  • github-deploy-key —— 私钥
  • github-deploy-key.pub —— 公钥

私钥直接存放在 hexo 原始文件(hexo源文件)的仓库代码里

  • 自行新建一个私人仓库来存放hexo源文件
  • 然后访问私人代码仓库 Settings -> Secrets,New secret
  • Name 填写 EXO_DEPLOY_KEY 注意大小写,这个后面的 GitHub Actions Workflow 要用到,一定不能写错。
  • 在 Value 填入 github-deploy-key(私钥) 中的内容

公钥放到 GitHub Pages 对应的代码仓库里面

  • 访问 github pages 对应的代码仓库 Settings -> Deploy keys,Add deploy key
  • Title:HEXO_DEPLOY_PUB 可自定义名字
  • 在 Key 填入 github-deploy-key.pub(公钥)中的内容
  • Allow write access 一定要勾上

创建 workflow

在私人代码仓库里点 Actions
然后创建一个新文件 .github/workflows/deploy.yml
deploy 名字可以自取但是一定要放在.github/workflows目录中

  • deploy.yml 内容如下:
name: Hexo Deploy

on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-18.04
    if: github.event.repository.owner.id == github.event.sender.id

    steps:
      - name: Checkout source
        uses: actions/checkout@v2
        with:
          ref: master

      - name: Setup Node.js
        uses: actions/setup-node@v1
        with:
          node-version: '12'

      - name: Setup Hexo
        env:
          ACTION_DEPLOY_KEY: ${{ secrets.HEXO_DEPLOY_KEY }}
        run: |
          mkdir -p ~/.ssh/
          echo "$ACTION_DEPLOY_KEY" > ~/.ssh/id_rsa
          chmod 700 ~/.ssh
          chmod 600 ~/.ssh/id_rsa
          ssh-keyscan github.com >> ~/.ssh/known_hosts
          git config --global user.email "改成你的邮箱"
          git config --global user.name "改成你的用户名"
          npm install hexo-cli -g
          npm install

      - name: Deploy
        run: |
          hexo clean
          hexo deploy

ok,这样就完美搞定 github action 和 GitHub pages 的连接啦,并且可以自动触发 Workflow 执行动作

二、推送部署 hexo 博客源文件到私人仓库

俺是一个纯小白,只能傻瓜式的推送部署到仓库了

  • 在任意位置 git bash here 然后 输入 git clone https://github.com/用户名/仓库名.git 将私人仓库给克隆下来

  • 将所有的hexo文件都复制到刚刚克隆下来的文件夹里面

  • 然后 git init 将该克隆下的文件夹变成Git可以管理的仓库

  • git add . 通过git add将所有文件提交到暂存区

  • git commit -m 'the initial edition' 版本描述

  • git remote add origin https://github.com/用户名/仓库名.git与仓库关联

  • git pull 第一次推送需要

  • git push -u origin master 带有-u这个参数是指,将master分支的所有内容都提交,第一次关联之后后边你再提交就可以不用这个参数了

  • git push origin master 之后你的每一次修改,你就可以只将你修改用这个push就好了

    此时,所有的hexo文件全都git到仓库了,之后就可以直接在私人仓库source/_posts/里面添加.md文件啦,可以随时随地写文章发文章,不受设备和配置环境干扰啦。
    俺是一个纯小白,都是一步一步按照别人的步骤踩坑摸索来的,不容易呜呜呜。

你可能感兴趣的:(利用GitHub Actions 自动部署 Hexo博客 全自动运行 不知所措的新哥)