使用 Hexo + GitHub Actions自动部署博客+vercel加速

slug
hexo-github-actions-vercel
status
Published
tags
hexo
博客
summary
type
Post

创建 Personal Access Token 用于 GitHub Actions 所构建得虚拟系统可以内容推送到仓库

勾选repo和workflow
将创建好的 Personal Access Token 添加到仓库的 Secrets 中,并设置名称ACCESS_TOKEN
粘贴刚刚生成的token
 
本地配置
到.github\workflows目录创建一个hexo.yml
粘贴下面代码
name: Blog CI/CD

# 触发条件:在 push 到 master 分支后触发
on:
  push:
    branches: 
      - master
env:
  TZ: Asia/Shanghai

jobs:
  blog-cicd:
    name: Hexo blog build & deploy
    runs-on: ubuntu-latest # 使用最新的 Ubuntu 系统作为编译部署的环境

    steps:
    - name: Checkout codes
      uses: actions/checkout@v2

    - name: Setup node
      # 设置 node.js 环境
      uses: actions/setup-node@v1
      with:
        node-version: '12.x'

    - name: Cache node modules
      # 设置包缓存目录,避免每次下载
      uses: actions/cache@v1
      with:
        path: ~/.npm
        key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}

    - name: Install hexo dependencies
      # 下载 hexo-cli 脚手架及相关安装包
      run: |
        npm install -g hexo-cli
        npm install

    - name: Generate files
      # 编译 markdown 文件
      run: |
        hexo clean
        hexo generate

    - name: Deploy hexo blog
      env: 
        # Github 仓库
        GITHUB_REPO: github.com/waimao365/hexo2
      
      # 将编译后的博客文件推送到指定仓库
      run: |
        cd ./public && git init && git add .
        git config user.name "waimao365"
        git config user.email "xxxxx@qq.com"
        git add .
        git commit -m "GitHub Actions Auto Builder at $(date +'%Y-%m-%d %H:%M:%S')"
        git push --force --quiet "https://${{ secrets.ACCESS_TOKEN }}@$GITHUB_REPO" master:master
 
上传部署
git init #如果错误可以用命令删除 rm -rf .git
git add . # themes文件夹不能上传,需要在themes文件夹内删除.git和.gaihub.文件夹
git commit -m “a”
git remote add origin git@github.com:waimao365/xxx.git
git push -u origin master #如果出现错误可以强制上次 git push -f origin master
 
 
我建立了两个项目,一个设置成私密,一个设置成公开
以上都是在私密的项目操作
下面这个改成你的公开的项目就可以了
GITHUB_REPO: github.com/waimao365/hexo2 这里也要改成你的公开的项目库
git config user.name "waimao365" 这里要改成你的 git config user.email "xxxxx@qq.com" 这里要改成你的
 
vercel导入公开库
为了访问速度更好一些,可以到https://vercel.com/导入公开项目库,再绑定域名,就可以了
域名需要提前解析道
一级域名A到 76.223.126.88
二级域名 CNAME cname.vercel-dns.com

© 精灵猫 2021 - 2025