hexo_actions

发布时间 2023-07-26 18:01:35作者: 0wwlww0
title: github actions部署hexo 
date: 2022-09-29 15:09:09
categories: 博客
toc: true
tags: hexo

简介:在将hexo搭建到github后,我发现每次都要用多个hexo的命令来推送并部署博客,并且原代码的上传也需要额外操作,比较麻烦。但是在浏览多个相关博客内容后,发现相关的教程都比较复杂,不太具备直接copy的能力,于是有此文。利用github action实现一键部署hexo到pages并备份博客代码。

1.创建仓库

我们需要两个github仓库来实现博客代码的私有化存储和hexo编译文件的pages布置。

github仓库
另一个应该在部署hexo到github pages时已经建立,仓库名应该为github用户名.github.io

2.创建ssh密钥来让本地与github之间实现无密码拉取与推送

在终端输入创建ssh密钥的命令:ssh-keygen -t rsa -C "your_email@example.com"

注意:需要输入的是注册github的邮箱。并且在输入命令后一直回车就行。直到让你输入y/n,输入y即可,后面让输入密码不用管,直接回车,否则以后每次拉取都要输入密码。

成功创建后,windows系统是在C:user/用户名/.ssh/文件夹下产生ssh密钥。
ssh0
其中id_rsa是私钥,id_rsa.pub是公钥。找到刚才建立的博客仓库的设置页面。选择secrets中的action选项。
secret
在此添加私钥,起名为HEXO_DEPLOY_KEY将之前创建的id_rsa文件用记事本打开并复制到该密钥下。
id-rsa1
在github中点击自己的头像,进入settings页面,选择SSH and GPG keys,点击new SSH key,输入公钥。
pub

3.设置github action

在hexo的主目录下,找到.github/workflows/文件夹,创建deploy.yml,输入如下代码修改后保存。


name: Deploy

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    name: A job to deploy blog.
    steps:
      - name: Checkout
        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

      # Deploy hexo blog website.
      - name: Deploy
        id: deploy
        uses: sma11black/hexo-action@v1.0.3
        with:
          deploy_key: ${{ secrets.HEXO_DEPLOY_KEY }}//这是之前设置的私钥名
          user_name: 此处输入github名 # (or delete this input setting to use bot account)
          user_email: 此处输入github注册邮箱  # (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 }}"

4.测试部署是否成功

到此部署完成,进入终端,将代码push到存储博客代码的仓库,当actions检测到master分支接到推送后,会运行deploy,自动将代码编译并部署到github pages。

注意:在hexo的config文件中,deploy部分的repo要设置为github.io仓库的ssh地址,格式为git@github.com:用户名/用户名.github.io.git