Xlenco

Xlenco

github
zhihu
email

Automate the deployment of your Hexo blog using Github Actions.

Github Actions Concept#

GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that can be used to automate build, test, and deployment pipelines. You can create workflows to build and test each pull request in your repository, or deploy merged pull requests to production.

GitHub Actions is not just for DevOps, it also allows you to run workflows when other events occur in your repository. For example, you can run a workflow to automatically add corresponding labels when someone creates a new issue in your repository.

GitHub provides Linux, Windows, and macOS virtual machines to run workflows, or you can host your own self-hosted runners in your own data center or cloud infrastructure.

Body#

The common connection schemes for pushing Git to a GitHub repository are HTTPS and SSH.

Therefore, Actions automation can also be roughly divided into two types. Here, we mainly discuss the HTTPS connection deployment method.

HTTPS Connection Deployment Method#

Obtain GitHub token#

  1. Obtain GitHub access tokens
    Open https://github.com/settings/tokens
    Click "Generate new token" to create a new token
    image

image

image

Create a private repository to store Hexo source code#

After creating it, you need to push the blog's source code to it. First, get the remote repository address, both SSH and HTTPS are acceptable. SSH does not require entering a password on devices that have bound an SSH key, while HTTPS requires entering a password, but SSH occasionally encounters port occupation.

After completing the above steps, create [Blogroot].github/workflows/autodeploy.yml

# Start the action when changes are pushed to the master branch
name: Auto Deploy

on:
  push:
    branches:
      - main 

  release:
    types:
      - published

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
    - name: Check out branch
      uses: actions/checkout@v2
      with:
        ref: main

    - name: Install Node
      uses: actions/setup-node@v1
      with:
        node-version: "16.x"

    - name: Install Hexo
      run: |
        export TZ='Asia/Shanghai'
        npm install hexo-cli -g

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

    - name: Install Dependencies
      if: steps.cache.outputs.cache-hit != 'true'
      run: |
        npm install --save

    - name: Generate Static Files
      run: |
        hexo clean
        hexo generate

    - name: Deploy # Here, master:master means submitting from the local master branch to the remote repository's master branch (not the blog branch, just write master), if there is no corresponding branch in the remote repository, create one. If there are other requirements, you can change it according to your needs.
      run: |
        cd ./public
        git init
        git config --global user.name '${{ secrets.GITHUBUSERNAME }}'
        git config --global user.email '${{ secrets.GITHUBEMAIL }}'
        git add .
        git commit -m "${{ github.event.head_commit.message }} $(date +"%Z %Y-%m-%d %A %H:%M:%S") Updated By Github Actions"
        git push --force --quiet "https://${{ secrets.GITHUBUSERNAME }}:${{ secrets.GITHUBTOKEN }}@github.com/${{ secrets.GITHUBUSERNAME }}/${{ secrets.GITHUBUSERNAME }}.github.io.git" master:master

Add environment variables#

Variable declaration#

Variable NameConstant Explanation
[Blogroot]The folder path where the blog source code is stored locally
GITHUBUSERNAMEGitHub username
GITHUBTOKENGitHub user email address
TOKENUSERSecrets for your deployment service, for example GITHUBTOKEN
GITHUBTOKENGitHub secret key

In the Settings->Secrets->actions of your repository

image

Check the deployment status#

At this point, open the private repository where the source code is stored on GitHub and find the action.

image

image

Find the corresponding task based on the commit record just now, click "Deploy" to view the deployment status. If all checkboxes are checked, congratulations, you can now enjoy the pleasure of automatic deployment.

SSH Connection Deployment Method#

Generate public and private keys#

Execute in the terminal:

ssh-keygen -t rsa -C "GitHub email address"

Ignore this step if you have already generated it before.

Create a new action flow on GitHub#

In the root directory of the GitHub repository, create a workflows folder, and create Hexo Deploy.yml in the workflows folder.
The content is as follows:

# Automation name
name: Hexo Deploy

# Trigger conditions
on:
  push:
    branches:
      - main

# Set up environment
jobs:
  build:
    runs-on: ubuntu-latest

    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 16.x
        uses: actions/setup-node@master
        with:
          node-version: "16.x"

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

      - name: Set up private key
        env:
          HEXO_DEPLOY_PRIVATE_KEY: ${{ secrets.HEXO_DEPLOY_PRIVATE_KEY }}
        run: |
          mkdir -p ~/.ssh/
          echo "$HEXO_DEPLOY_PRIVATE_KEY" > ~/.ssh/id_rsa 
          chmod 600 ~/.ssh/id_rsa
          ssh-keyscan github.com >> ~/.ssh/known_hosts  

      - name: Set up Git information
        run: |
          git config --global user.name 'Your GitHub username' 
          git config --global user.email 'Your GitHub email'      

      - name: Hexo Three-in-One
        run: |
          hexo clean
          hexo generate 
          hexo deploy

Configure the public key: In the GitHub website->Settings->SSH and GPG keys, the name is HEXO_DEPLOY_PRIVATE_KEY, and the content is the one in .ssh/id_rsa.pub.

Configure the private key: In the Settings->Secrets of the private repository, the name is HEXO_DEPLOY_PRIVATE_KEY, and the content is the one in .ssh/id_rsa. Note that there may be an extra space when copying, be sure to delete it.
Then you're done!

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.