Deploying Hexo Anywhere using Github Action

本文最后更新于:July 27, 2024 8:58 PM

According to Github Action’s documentation, this is a tool that helps you to:

Automate, customize, and execute your software development workflows right in your repository with GitHub Actions. You can discover, create, and share actions to perform any job you’d like, including CI/CD, and combine actions in a completely customized workflow.

TL;DR: CI/CD platform that automates your build, test, and deployment pipeline.

After getting github action integrated, you no longer need to use any hexo/npm related commands when you deploy. It simplifies the process to just pushing changes to a private repository.

In the following guide, I would assume you already have your Hexo blog’s generated static files in a github repo.

Here’s what you need to do

Initializing Repository

Initialize a private Important! There are sensitive files! repository on GitHub that will store all the files in your blog’s root directory. Call it whatever you want, I’m using blog-backup.

Add SSH Key

  1. On your computer, open up git bash and use this command
    1
    ssh-keygen -f github-deploy-key
    enter until you see the key’s randomart image.
  2. Go to the directory which you used git bashed at and find these two files:github-deploy-key and github-deploy-key.pub

    The first one is the private key and second one is the public key. Keep them safe and don’t send them to anyone or accidentally push them to github…

  3. Head to your root file repository and go to Settings -> Secrets -> New repository secret.

    Use HEXO_DEPLOY_PRI for the name and paste the contents in your github-deploy-key for value and click Add Secret.

  4. Head to your original Hexo blog’s repository (The generated static files), and go to Settings -> Deploy keys -> Add deploy key.

    Paste the contents in your github-deploy-key.pub file into the key, set the title to HEXO_DEPLOY_PUB, and select the checkbox Allow write access.

Create config file

  1. Create a new directory under your root folder .github/workflows/deploy.yml
  2. Confilg the yaml file

    Here’s mine: (Remember to change the env variables and time zone under Configuration environment)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    # Action name
    name: Hexo Auto Deploy

    on:
    # Trigger condition 1: main branch receives a push
    push:
    branches:
    - main
    # Trigger condition 2: Run workflow button manually pressed
    workflow_dispatch:

    # change this enviroenment varibles to your own
    env:
    GIT_USER: GitUserId
    GIT_EMAIL: gituser@email.com
    # Deploy to this repo after generated
    GIT_DEPLOY_REPO: GitUser/My-Blog
    # Deploy to this branch after generated
    GIT_DEPLOY_BRANCH: master
    # Hexo's source repo (the public one)
    GIT_SOURCE_REPO: git@github.com:GitUser/My-Blog.git


    jobs:
    build:
    name: Build on node ${{ matrix.node_version }} and ${{ matrix.os }}
    runs-on: ubuntu-latest
    if: github.event.repository.owner.id == github.event.sender.id
    strategy:
    matrix:
    os: [ubuntu-18.04]
    node_version: [12.x]

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

    - name: Checkout deploy repo
    uses: actions/checkout@v2
    with:
    repository: ${{ env.GIT_DEPLOY_REPO }}
    ref: ${{ env.GIT_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: | # Make sure you change the timezone
    sudo timedatectl set-timezone "Continent/City"
    mkdir -p ~/.ssh/
    echo "$HEXO_DEPLOY_PRI" > ~/.ssh/id_rsa
    chmod 600 ~/.ssh/id_rsa
    ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts
    ssh-keyscan -t rsa e.coding.net >> ~/.ssh/known_hosts
    ssh-keyscan -t rsa gitee.com >> ~/.ssh/known_hosts
    git config --global user.name $GIT_USER
    git config --global user.email $GIT_EMAIL

    - name: Install Hexo # Install Hexo
    run: |
    npm install hexo-cli -g


    - name: Cache Modules # Cache Hexo extensions
    uses: actions/cache@v1
    id: cache-modules
    with:
    path: node_modules
    key: ${{runner.OS}}-${{hashFiles('**/package-lock.json')}}

    - name: Install Dependencies # Runs if the extension is not cached, or new extensions added
    if: steps.cache-modules.outputs.cache-hit != 'true'
    run: | # Please make sure you have package-lock.json in your root repo (this one)
    npm ci

    - name: Generate
    run: |
    hexo clean
    hexo generate

    - name: Deploy
    run: |
    npm run deploy

Push Codes

The finial step is just to push everything you have from you root directory to your private repo, and remember to check if the workflow ran successfully on the Github Actions page.


Congrats! Now you can edit you blog anywhere and have something to do when you’re slacking off at work . pls.. hr, don’t see this.

Feel free to leave a comment below for any questions and suggestions you have.