hexo 自动化

Last updated on September 15, 2024 pm

🧙 Questions

构建windows脚本,自动创建hexo博客模板
post
Hexo 如何优化文档
然后自动在指定项目中创建博客
github
hexo
如何优化文档.md

☄️ Ideas

实现思路

首先使用python代码,通过pyinstaller工具,转换成post.exe文件,将文件保存到onedriver/Post文件夹中,指定环境变量配置
实现在cmd中直接打印post命令,自动创建博客,支持外部配置文件

hexo:
  post_project_dir: D:\\isxcode.com\\blogs-galaxy
  categories:
    database: 
      - mongodb
      - mysql
      - oracle
      - redis
      - sqlserver
    github:
      - git
      - github
      - hexo
      - markdown
    hadoop:
      - clickhouse
      - cloudera
      - doris
      - flink
      - hadoop
      - hbase
      - hive
      - kafka
      - spark
      - sqoop
      - zookeeper
    linux:
      - docker
      - jenkins
      - kubernetes
      - linux
      - rancher  
    react:
      - nginx
      - node
      - react
    spring:
      - gradle
      - java
      - jetbrains
      - maven
      - spring
    tensorflow:
      - python
      - scrapy
      - tensorflow
    windows:
      - windows
import sys
import os
import yaml

# 获取配置文件路径
config_file_name = 'post_config.yml'
if getattr(sys, 'frozen', False):
    config_file_path = os.path.join(os.path.dirname(sys.executable), config_file_name)
elif __file__:
    config_file_path = os.path.join(os.path.dirname(__file__), config_file_name)

# 获取配置文件内容
config_file = open(config_file_path, mode='r', encoding='utf-8')
config = yaml.load(config_file, Loader=yaml.FullLoader)

# 解析yml文件
blogs_path = config['hexo']['post_project_dir']
categories = config['hexo']['categories']

# 请用户输入博客名称
input_title = input('Input title: \n')

# 判断输入名称是否合法
if len(input_title) == 0:
    print('title is empty!!!')
    sys.exit()

# 检查是否有空格
title_split = input_title.split(' ', 1)
if len(title_split) == 1:
    print('title is empty!!!')
    sys.exit()

# 检查模板是否支持
template__dir = ''
template_name = title_split[0].lower()

# 遍历所有的模板类型
all_templates = []
for metaCategories in categories:
    for metaCategory in categories[metaCategories]:
        if template_name == metaCategory:
            template__dir = metaCategories
        all_templates.append(metaCategory)

if template_name not in all_templates:
    print('template is not support!!!')
    sys.exit()

# hexo new git -post=github/git/"demo" "Demo"
hexo_command = 'hexo new ' + template_name + ' -post=' + template__dir + '/' + template_name + '/"' + title_split[
    1] + '" "' + title_split[1] + '" '
print("execute ==> " + hexo_command)

# 创建windows执行命令
execute_command = 'cd ' + blogs_path + ' && ' + hexo_command
os.system(execute_command)
hexo开启history路有
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
    }
}
github action
# 部署官网
name: deploy docsify

# 发版后触发
on:
  push:
    branches:
      - xx

# 配置阿里云镜像仓库信息
env:
  REGISTRY: registry.cn-hongkong.aliyuncs.com
  IMAGE_NAME: isxcode/zhiqingyun-website
  REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
  REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}

jobs:
  build:
    runs-on: ubuntu-latest

    steps:

      # 拉取代码
      - name: Checkout repository
        uses: actions/checkout@v2

      # 登录阿里云镜像仓库
      - name: Log into registry ${{ env.REGISTRY }}
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v2
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ env.REGISTRY_USERNAME }}
          password: ${{ env.REGISTRY_PASSWORD }}

      # 构建docker元数据
      - name: Extract Docker metadata
        id: meta
        uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

      # 构建镜像并推到阿里云镜像仓库
      - name: Build and push Docker image
        id: build-and-push
        uses: docker/build-push-action@v3.2.0
        with:
          context: ./docs
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}

      # 部署项目到自己到服务器
      - name: deploy
        uses: appleboy/ssh-action@v0.1.4
        with:
          host: isxcode.com
          username: ${{ secrets.USERNAME }}
          password: ${{ secrets.PASSWORD }}
          script_stop: false
          script: |
            docker stop zhiqingyun-website
            docker rm zhiqingyun-website
            docker rmi ${{ steps.meta.outputs.tags }}
            docker pull ${{ steps.meta.outputs.tags }}
            docker run --restart=always --name zhiqingyun-website -p 30210:80 -d ${{ steps.meta.outputs.tags }}
FROM nginx

COPY ./nginx.conf /etc/nginx/conf.d/default.conf
COPY ./ /usr/share/nginx/html/

EXPOSE 80

# 本地打包
# docker buildx create --name spark-yun-builder
# docker buildx use spark-yun-builder
# docker buildx build --platform linux/amd64,linux/arm64/v8 -t isxcode/zhiqingyun-website:latest -f ./Dockerfile . --push

# 远程部署
# docker stop zhiqingyun-website
# docker rm zhiqingyun-website
# docker rmi isxcode/zhiqingyun-website:latest
# docker run --restart=always --name zhiqingyun-website -p 30210:80 -d isxcode/zhiqingyun-website

hexo 自动化
https://ispong.isxcode.com/github/hexo/hexo 自动化/
Author
ispong
Posted on
March 9, 2021
Licensed under