如何使用Github的Action实现博客的自动部署

如何使用Github的Action实现博客的自动部署

以下是详细的采坑记录,花费了不少时间。

现在的状态是,更新完博客时,需要执行以下的指令:

hexo clean && hexo g && hexo d &&  git add . && git commit -m "deploy" && git push

然后才能完成github上静态博客的更新

缺点是很麻烦!

可以用action来自动化部署:

https://blog.esunr.xyz/2022/06/64163235c30f.html#3-3-%E4%BD%BF%E7%94%A8-hexo-deploy-%E6%8C%87%E4%BB%A4

Github Action 可以实现在一个行为触发之后再执行一些其他的行为,利用这个能力我们就可以实现当我们写完一篇文章后,将代码 Push 到 Github 仓库的这一刻,让 Github 来帮我们完成编译以及部署这个流程,也就是实现持续集成(CI)、持续交付(CD)的这个效果。

关于 Github Action,详细教程可以查看 官方文档。按照文档中所描述的,只要我们在代码中添加一层 .github/workflows 目录,并且在目录下创建一个 yml 文件来描述具体的行为,就可以实现开启 Github Action。

照着来了一遍,好像没啥用啊;

名字没有配置好:

 could not read Username for '[https://github.com](https://github.com/)': No such device or address

https://sanonz.github.io/2020/deploy-a-hexo-blog-from-github-actions/

这个博客写的非常详细:

1、A仓库是,我的私密Blog仓库;

2、B仓库是,public的仓库;

3、需要在github上配置,A对B的访问权限,这个很重要。

遇到的问题:

Permission to XXXX/XXXX.github.io.git denied to github-actions[bot].

秘钥,密匙没有设置好


总结:

1、按照如下的方法,在github上边申请,得到一个HEXO_DEPLOY_PRI

image-20221105215707211

2、然后去A仓库(私人仓库)的Setting里边,把Action变量给配置好

image-20221105215815604

3、在A仓库中的deploy.yml

把相关信息和变量都统一好:HEXO_DEPLOY_PRI变量不要用错了,主题也不要用错了

name: CI

on:
  push:
    branches:
      - master

env:
  GIT_USER: xxxx
  GIT_EMAIL: xxxx@qq.com
  THEME_REPO: xxxx/hexo-theme-next
  THEME_BRANCH: master
  DEPLOY_REPO: xxxx/xxxx.github.io
  DEPLOY_BRANCH: master

jobs:
  build:
    name: Build on node ${{ matrix.node_version }} and ${{ matrix.os }}
    runs-on: ubuntu-latest
    strategy:
      matrix:
        os: [ubuntu-latest]
        node_version: [13.14.0]

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

      - name: Checkout theme repo
        uses: actions/checkout@v2
        with:
          repository: ${{ env.THEME_REPO }}
          ref: ${{ env.THEME_BRANCH }}
          path: themes/next

      - name: Checkout deploy repo
        uses: actions/checkout@v2
        with:
          repository: ${{ env.DEPLOY_REPO }}
          ref: ${{ env.DEPLOY_BRANCH }}
          path: .deploy_git

      - name: Use Node.js ${{ matrix.node_version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node_version }}

      - name: Configuration environment
        env:
          HEXO_DEPLOY_PRI: ${{secrets.HEXO_DEPLOY_PRI}}
        run: |
          sudo timedatectl set-timezone "Asia/Shanghai"
          mkdir -p ~/.ssh/
          echo "$HEXO_DEPLOY_PRI" > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa
          ssh-keyscan github.com >> ~/.ssh/known_hosts
          git config --global user.name $GIT_USER
          git config --global user.email $GIT_EMAIL          

      - name: Install dependencies
        run: |
          npm install

      - name: Deploy hexo
        run: |
          npm run deploy

为什么会一直出现这个问题?

Permission to XXXX/XXXX.github.io.git denied to github-actions[bot].

_config.yml里边去把https协议修改成git

# Deployment
deploy:
  type: git
  repo: git@github.com/XXX/xxx.github.io.git
  branch: master #published  
  message: 'hexo blog deploy'

再尝试一下:


还是这个错误:

Permission to xxxx/xxxx.github.io.git denied to github-actions[bot].

The requested URL returned error: 403

终于成功了:

image-20221105234029937

deploy的代码这样写:

最终的CI脚本:

# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the "master" branch
  push:
    branches: [ "master" ]

env:
  GIT_USER: xxxx
  GIT_EMAIL: xxxx@qq.com
  THEME_REPO: xxxx/hexo-theme-next
  THEME_BRANCH: master
  DEPLOY_REPO: xxxx/xxxx.github.io
  DEPLOY_BRANCH: master

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    name: Build on node ${{ matrix.node_version }} and ${{ matrix.os }}
    # The type of runner that the job will run on
    runs-on: ubuntu-latest
    strategy:
      matrix:
        os: [ubuntu-latest]
        node_version: [13.14.0]    

    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Checkout theme repo
        uses: actions/checkout@v3
        with:
          repository: ${{ env.THEME_REPO }}
          ref: ${{ env.THEME_BRANCH }}
          path: themes/next

      - name: Checkout deploy repo
        uses: actions/checkout@v3
        with:
          repository: ${{ env.DEPLOY_REPO }}
          ref: ${{ env.DEPLOY_BRANCH }}
          path: .deploy_git

      - name: Use Node.js ${{ matrix.node_version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node_version }}            

      - name: Install Dependencies
        run: |
          npm install   

      - name: Build
        run: npm run build

      - name: Configuration environment
        env:
          HEXO_DEPLOY_PRI: ${{secrets.HEXO_DEPLOY_PRI}}
        run: |
          sudo timedatectl set-timezone "Asia/Shanghai"
          mkdir -p ~/.ssh/                              
          echo "$HEXO_DEPLOY_PRI" > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa
          ssh-keyscan github.com >> ~/.ssh/known_hosts
          git config --global user.name $GIT_USER
          git config --global user.email $GIT_EMAIL 

      - name: Deploy hexo
        id: deploy
        uses: peaceiris/actions-gh-pages@v3
        with:
          deploy_key: ${{ secrets.HEXO_DEPLOY_PRI }}
          publish_dir: ./public

      - name: Get the output
        run: |
          echo "${{ steps.deploy.outputs.notify }}" 

      # Runs a single command using the runners shell
      - name: Run a one-line script
        run: echo Hello, world!

      # Runs a set of commands using the runners shell
      - name: Run a multi-line script
        run: |
          echo Add other actions to build,
          echo test, and deploy your project.

服了,还是没用,blog没有更新


https://blog.zhanganzhi.com/zh-CN/2022/06/0800d76d306e/

再来试一试;

name: Deployment
on: push

jobs:
  deployment:
    runs-on: ubuntu-latest
    name: Deployment

    steps:
      - name: Check Out
        uses: actions/checkout@v3

      - name: Clone Repo
        uses: actions/checkout@v3
        with:
          repository: AAA/GITHUB.IO
          path: .deploy_git

      - name: Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: 16

      - name: Setup Git
        run: |
          git config --global user.name "Example"
          git config --global user.email "email@example.com"

      - name: Setup SSH Key
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.SSH_DEPLOY_KEY }}" > ~/.ssh/id_ed25519
          chmod 600 ~/.ssh/id_ed25519

      - name: Deploy
        run: |
          npm install --location=global hexo-cli
          npm install
          hexo d

https://wuuconix.link/2022/01/08/actions/

name: Deploy                      # Actions 显示的名字,随意设置
on: [push]                        # 监听到 push 事件后触发
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout              # 拉取当前执行 Actions 仓库的指定分支
      uses: actions/checkout@v2
      with:
        ref: main

    - name: Setup Node            # 使用node 14
      uses: actions/setup-node@v2
      with:
        node-version: "14"

    - name: NPM INSTALL           #安装依赖
      run: |
        npm install hexo-cli -g
        npm install

    - name: Add KEY               #这里需要提供一个ssh私钥,用你平时常用机器里的 ~/.ssh/id_rsa即可
      env:
        SSH_PRIVATE: ${{ secrets.SSH_PRIVATE }}
      run: |
        mkdir -p ~/.ssh/
        echo "$SSH_PRIVATE" > ~/.ssh/id_rsa
        chmod 700 -R ~/.ssh
        ssh-keyscan github.com >> ~/.ssh/known_hosts
        git config --global user.email "输入你的邮箱"
        git config --global user.name "输入你的github名字"

    - name: Hexo Deploy           # hexo deploy 会自动build然后上传到github.io项目中
      run: |
        hexo clean
        hexo deploy

放弃了,最终也没顺利的更新成功,不知道哪个环节出问题了。。。

哈哈 笑死我了:

image-20221106005343195

这个action,测试一次就需要花费10分钟左右,真是时间黑洞!!!


在本地尝试了一遍,OK的

git clone https://github.com/XXXX/XXXX.git
cd .\XXXX\
git clone https://github.com/XXXX/hexo-theme-next.git themes/next
npm install
npm run deploy

重新写actions的脚本

name: CI

on:
  push:
    branches: [ "master" ]

env:
  GIT_USER: xxxx
  GIT_EMAIL: xxxx@qq.com
  THEME_REPO: xxxx/hexo-theme-next
  THEME_BRANCH: master
  DEPLOY_REPO: xxxx/xxxx.github.io
  DEPLOY_BRANCH: master

jobs:
  build:
    name: Build on node ${{ matrix.node_version }} and ${{ matrix.os }}
    runs-on: ubuntu-latest
    strategy:
      matrix:
        os: [ubuntu-latest]
        node_version: [13.14.0]    

    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Checkout theme repo
        uses: actions/checkout@v3
        with:
          repository: ${{ env.THEME_REPO }}
          ref: ${{ env.THEME_BRANCH }}
          path: themes/next

      - name: Use Node.js ${{ matrix.node_version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node_version }}            

      - name: Configuration environment
        env:
          HEXO_DEPLOY_PRI: ${{secrets.HEXO_DEPLOY_PRI}}
        run: |
          sudo timedatectl set-timezone "Asia/Shanghai"
          mkdir -p ~/.ssh/                              
          echo "$HEXO_DEPLOY_PRI" > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa
          ssh-keyscan github.com >> ~/.ssh/known_hosts
          git config --global user.name $GIT_USER
          git config --global user.email $GIT_EMAIL 

      - name: Install Dependencies
        run: |
          npm install             

      - name: Deploy hexo
        run: |
          npm run deploy

_config.yml中,修改成git,竟然成功了!!!

image-20221107003203115

但是编译非常花费时间,能不能提速呢?

image-20221107003235458

https://dwye.dev/post/github-action-npm-cache/

尝试一下npm ci,看看速度怎么样:我的天啊,神速啊,只用6秒就可以了!!!

https://segmentfault.com/q/1010000040245798/a-1020000040248108

小插曲:

npm install --package-lock-only

image-20221107003716102

posted @ 2022-11-16 11:07  bH1pJ  阅读(227)  评论(0编辑  收藏  举报