目录

关于一个小白github建blog的故事

发表于
0 6.1~7.9 分钟 2759

前置环境配置

  • cmder(可选)

  • nodejs环境

    • nvm(可选)
    • node -v
    • 改源(可选)
  • hexo

  • github

    • 账号注册
    • 创建私有仓库
    • 创建公有仓库
  • 配置git

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

安装hexo

  • 安装hexo

    • npm install -g hexo-cli
  • 安装主题

    • npm i hexo-theme-butterfly
  • 本地运行

上线github

配置ssh密钥

  • 参考文档 :https://docs.github.com/cn/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

  • 使用cmder/gitbash创建密钥ssh-keygen -t ed25519 -C "your_email@example.com"

  • 绑定github 公钥:https://github.com/settings/keys

  • 验证是否成功ssh -T git@github.com

  • 创建公有仓库

传源码

  • 配置hexo config.yml
deploy:
  - type: git
    repo: git@github.com:testimpdx/testimpdx.github.io.git
    branch: master

执行 hexo d

cdn优化

  • 我们发现当博客在github上时,我们需要很多方法去优化他,不然会非常非常的慢,因为github在国外

  • 可以使用又拍云/百度云/自选cf/奇安信等等,注:国内cdn需备案

自动化CICD

  • 每次发布博客都需要hexo clean && hexo g && hexo d
  • 还需要搭建nodejs环境,那么我们是否可以让他自动化完成这些呢

使用github action

  • 我们首先需要建立一个私有仓库
  • 在这个私有仓库中我们需要把源码同步上去。
  • 之后只需要在这个仓库中把源码拉下来,新增md文档,再次提交就可以直接更新文章了

新建私人仓库

源码上传

  • git clone仓库,直接扔源码
git add .
git commit -m "修改内容"
git push
  • 新建 .github/workflows/main.yml
  • 注意其中几个变量需要添加secrets.HEXO_TOKEN /secrets.GIT_NAME/secrets.GIT_EMAIL 需要在项目中添加
  • 例子为token添加,还有用密钥添加的都有

name: Deploy Blog

on:
  push:
    branches:
      - master
env:
  TZ: Asia/Shanghai

jobs:
  build: # 一项叫做build的任务
    name: Hexo blog build & deploy
    runs-on: ubuntu-latest # 在最新版的Ubuntu系统下运行

    steps:
    - name: Checkout codes # 将仓库内master分支的内容下载到工作目录
      uses: actions/checkout@master # 脚本来自 https://github.com/actions/checkout
      with:
        persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal token
        fetch-depth: 1

    - name: 配置Node环境 # 配置Node环境
      uses: actions/setup-node@v1 # 配置脚本来自 https://github.com/actions/setup-node
      with:
        node-version: '12.x'

    - name: 清理缓存
      uses: actions/cache@v1
      id: cache-dependencies
      with:
        path: node_modules
        key: ${{runner.OS}}-${{hashFiles('**/package-lock.json')}}

    - name: 安装hexo插件&&git基本设置
      if: steps.cache-dependencies.outputs.cache-hit != 'true'
      run: |
        npm install
    - name: Deploy && Create local changes
      run: |
        npm install hexo-cli -g
        hexo clean && hexo g
    - name: Commit files
      run: |
        git config --global core.ignorecase false
        # set git infomation
        git config --global user.email '${{ secrets.GIT_EMAIL }}' # set up GitHub name
        git config --global user.name '${{ secrets.GIT_NAME }}' # set up GitHub email
        cd ./public
        git init
        git add .
        git commit -m "Add changes" -a
    - name: Deploy
      uses: peaceiris/actions-gh-pages@v3
      with:
        github_token: ${{ secrets.HEXO_TOKEN }} # set up GitHub Token for deploy

    - name: 部署 Hexo
      run: |
        hexo clean && hexo g && hexo deploy

seo优化

Search Engine Optimization(搜索引擎优化)

sitemap

npm install hexo-generator-sitemap --save
npm install hexo-generator-baidu-sitemap --save

站长平台

  • 百度站长平台
  • Google站长平台

优化url

  • hexo-abbrlink
  • npm install hexo-abbrlink --save

添加robots

User-agent: *
Allow: /
Allow: /archives/
Allow: /categories/
Allow: /tags/

Disallow: /vendors/
Disallow: /js/
Disallow: /css/
Disallow: /fonts/
Disallow: /vendors/
Disallow: /fancybox/

Sitemap: https://www.impdx.vip/sitemap.xml
Sitemap: https://www.impdx.vip/baidusitemap.xml

关于gulp

  • 如果使用cdn,大部分cdn自带gulp压缩,所以没必要去进行npm安装gulp