gitlab Action+hexo的博客搭建

gitlab Action+hexo的博客搭建

其实之前搭建过一次hexo静态博客,本次搭建主要是学习下git的相关操作,利用gitlab Action进行文章发布,相当于简单的CI流程;主要是能学到一些新东西,搭建都搭建了,就简单记录下吧,免得还有些坑;

hexo史上最全搭建教程_zjufangzh的博客-CSDN博客_hexo

Github Actions自动化部署 hexo博客 - 云+社区 - 腾讯云 (tencent.com)

xuthus5/hexo-theme-cactus: Friendly hexo theme based on cactus-white(基于cactus-white的中文友好Hexo主题) (github.com)

本地hexo搭建

1、git相关环境搭建

git下载相关就不记录了

ssh-keygen -t rsa -C "youremail"	#用户.ssh目录生成公私钥
gitlab进行公钥设置绑定;
ssh -T git@github.com	#查看是否成功


git config --global user.name "yourname"
git config --global user.email "youremail"	#主要设置git相关操作记录者信息

2、相关环境搭建

nodejs直接官网安装就行

Download | Node.js (nodejs.org)

node -v
npm -v	#查看相关环境是否成功

npm install -g hexo-cli
hexo -v

hexo init myblog	#名字其实还可以更改
cd myblog
npm install
npm install hexo-deployer-git --save	#git部署组件

3、hexo静态博客

之后将博客目录下config.yml文件更改:

deploy:
  type: git
  repo: github page地址,这里因为是ssh密钥设置的,所以使用ssh链接方式;
  branch: master

常用命令:

hexo clean
hexo generate	#hexo g生成静态文章
hexo deploy		#hexo d部署发布文章

hexo new newpapername	#生成一个md格式文章,之后使用上面方式部署发布

gitlab Action做集成发布

其实按照上面就已经可以做静态博客发布了,但是主要接触都是hexo相关命令操作,所以参考相关大佬文章使用gitlab Action做发布;

1、仓库配置

设置一个私有仓库进行同步本地的hexo源代码,github pages项目为博客页面仓库,其实就是源代码的public文件夹中内容,使用hexo g生成的静态文件;

ssh-keygen -f hexo-deploy-key -t rsa -C "youremail"

执行命令目录下生成秘钥 hexo-deploy-key 和公钥 hexo-deploy-key.pub,之后进行两个仓库密钥绑定:

  • 页面静态文件仓库:

    Settings > Deploy keys 中添加 Deploy key,内容为 hexo-deploy-key.pub 文件内容,同时勾选 Allow write access 选项。

pub的内容可能会显示格式不符

Key is invalid. You must supply a key in OpenSSH public key format github_疯狂的鸭血的博客-CSDN博客

使用clip < id_rsa.pub回车复制到粘贴板 直接粘贴就行;

  • 博客源文件:

    Settings > Secrets 中添加一个 Secret,名称为 DEPLOY_KEY,内容为 hexo-deploy-key 文件内容。后续在 Workflow 中通过名称 DEPLOY_KEY 使用这个密钥。

2、github action中workflow配置

github action是一个很不做的功能,在此不做过多记录;

使用上文中参考修改后的deploy.yml文件:

# workflow name
name: actions single

# 当有 push 到仓库和外部触发的时候就运行
on: [push, repository_dispatch]

# YQ_TOKEN
# YUQUE_GIT_HEXO
jobs:
  deploy:
    name: Deploy Hexo Public To Pages
    runs-on: ubuntu-latest
    env:
      TZ: Asia/Shanghai

    steps:
      # check it to your workflow can access it
      # from: https://github.com/actions/checkout
      - name: Checkout Repository master branch
        uses: actions/checkout@master

      # from: https://github.com/actions/setup-node
      - name: Setup Node.js 12.x
        uses: actions/setup-node@master
        with:
          node-version: "12.x"

      #安装依赖(包含yuque-hexo,此处无需安装) from https://github.com/x-cold/yuque-hexo
      - name: Install dependencies
        run: |
          npm install hexo-cli -g
          npm install
          npm install hexo-deployer-git --save
          
      # 生成可访问的文档
      - name: hexo generate
        run: |
          hexo clean
          hexo g
          
      # 生成pages且推送到文件仓库 from https://github.com/peaceiris/actions-gh-pages
      - name: Deploy hexo to Github pages
        uses: peaceiris/actions-gh-pages@v3
        with:
          deploy_key: ${{ secrets.DEPLOY_KEY }} # 此处为 hexo-deploy-key
          external_repository: qweqweqwe123/qweqweqwe123.github.io
          publish_branch: master
          publish_dir: ./public
          commit_message: deploy githubPage
          # commit_message: ${{ github.event.head_commit.message }}

git发布

上述如果都没问题其实就可以使用git进行文章相关发布了;因为前文已经配置了git环境,这里就直接操作了;

常用操作

这里只简单记录下;

git init	#在本地博客目录内执行就可以将其变成git可以管理的仓库
git add .	#所有文件放进暂存区
git commit -m "the first commit"	#提交到本地版本库
git remote add origin ssh博客源文件项目地址	#将本地仓库和远程github仓库关联
git pull --rebase origin master		#远程仓库可能有文件(README文档),进行同步;
git push -u origin master		#提交master分支内容,之后不用带-u参数就行;

cactus主题设置

默认主题进行更换为cactus,git仓库内下载cactus仓库,git的原因出了点问题,找了好久才找到解决方法;

执行git add . 后仍然报changes not staged for commit错误的解决方法_SailorCai的博客-CSDN博客

博客根目录下进行,下载主题cactus到相关目录
git clone https://github.com/xuthus5/hexo-theme-cactus.git themes/cactus

修改根目录下config.yml中theme配置
# theme: landscape
theme: cactus

这个其实很正常,之后就git add .、 git commit、git push等操作同步到源代码仓库,但是发现theme目录下只同步了一个cactus文件夹,相关内容都没有同步;找了相关解决方案:

1、进入theme cactus clone仓库目录:
rd /s /q .git
ls .git

2、回到博客根目录,删除空文件夹
git rm -r --cached "themes/materail"
git commit -m "remove empty folder"
git push origin master

3、重新提交代码
git add .
git commit -m "repush"
git push origin master

看来git工作组项目内还有git项目的话是无法正常git add .的;

posted @ 2022-05-03 23:37  Yg~  阅读(363)  评论(0)    收藏  举报