GitHub Actions 来自动部署 Hexo

实现过程

一、新建仓库存放Hexo源码

公开或私有repo都可以

二、创建Github Actions

新建 .github/workflows目录,并写入如下代码:

name: 部署hexo至vps

on: 
  push:
    branches:
      - main
      
jobs:
  build: 
    runs-on: ubuntu-latest 

    steps:
    - name: 检出仓库代码
      uses: actions/checkout@master

    - name: 安装Node.js 16.x 
      uses: actions/setup-node@master
      with:
        node-version: '16.14.2'

    - name: 安装Hexo依赖
      run: |
        npm install hexo-cli -g
        npm install
        npm install hexo-admonition --save
        npm install hexo-generator-baidu-sitemap --save
        npm install hexo-lazyload-image --save
        npm install hexo-tag-fancybox_img --save
        npm install markdown-it --save
        npm install markdown-it-abbr --save
        npm install markdown-it-anchor --save
        npm install markdown-it-checkbox --save
        npm install markdown-it-mark --save

    - name: 构建Hexo
      run: |
        hexo clean
        hexo generate

上面代码主要用于在github actions中安装nodejs和hexo主程序,并安装Hexo依赖库

三、部署

1、通过ftp部署(慢)

进入github repo -> Settings -> Secrets -> New secret,,分别新建三条名为FTP_SERVER、FTP_USERNAME和FTP_PASSWORD,值为FTP地址、FTP账号和FTP密码的secret如需更多配置例如端口号等可以参考FTP-Deploy-Action

代码如下:

- name: Deploy Files on Ftp Server
  uses: SamKirkland/[email protected]
  with:
    server: ${{ secrets.ftp_server }}
    username: ${{ secrets.ftp_username }}
    password: ${{ secrets.ftp_password }}
    local-dir: ./public/
    port: ${{ secrets.ftp_port }}

./public是Hexo默认的静态文件生成本地地址,无需更改。

2、通过scp部署(快)

secret的添加方式跟上面一样

- name: Deploy to VPS
  uses: appleboy/scp-action@master
  with:
    host: ${{ secrets.SERVER_HOST }}
    username: ${{ secrets.SERVER_USER }}
    password: ${{ secrets.SERVER_USER_PASSWORD }}
    port: ${{ secrets.SERVER_PORT }}
    strip_components: 2
    source: "./public/"
    target: "/www/wwwroot/coding.f10.org/"

四、完整代码

name: 部署hexo至vps

on: 
  push:
    branches:
      - main
      
jobs:
  build: 
    runs-on: ubuntu-latest 

    steps:
    - name: 检出仓库代码
      uses: actions/checkout@master

    - name: 安装Node.js 16.x 
      uses: actions/setup-node@master
      with:
        node-version: '16.14.2'

    - name: 安装Hexo依赖
      run: |
        npm install hexo-cli -g
        npm install
        npm install hexo-admonition --save
        npm install hexo-generator-baidu-sitemap --save
        npm install hexo-lazyload-image --save
        npm install hexo-tag-fancybox_img --save
        npm install markdown-it --save
        npm install markdown-it-abbr --save
        npm install markdown-it-anchor --save
        npm install markdown-it-checkbox --save
        npm install markdown-it-mark --save

    - name: 构建Hexo
      run: |
        hexo clean
        hexo generate
        
    - name: Deploy to VPS
      uses: appleboy/scp-action@master
      with:
        host: ${{ secrets.SERVER_HOST }}
        username: ${{ secrets.SERVER_USER }}
        password: ${{ secrets.SERVER_USER_PASSWORD }}
        port: ${{ secrets.SERVER_PORT }}
        strip_components: 2
        source: "./public/"
        target: "/www/wwwroot/coding.f10.org/"
阅读量: | 柯西君_BingWong | 2023-02-16