github actions 自動部署 hexo 博客 到 github 庫

前提已創建兩個倉庫:

  1. 私有倉庫 - 存儲 博客源代碼
  2. 公共倉庫 - 存儲 生成後的博客靜態文件

首先 設置ssh 密鑰對

  1.生成密鑰 可以使用git bash 命令工具,執行下面的命令,生成 私鑰 和公鑰

ssh-keygen -t rsa -C '郵箱'

  2.設置github

  •  

    • 打開公鑰文件,複製內容到 key 輸入框

    • 打開私有庫 settings-》secrets,設置私有密鑰,點擊 New repository secret;打開私鑰文件,將私鑰內部複製進key輸入框中。

    •  設置actions,打開私有庫的 actions ,點擊 set up a workflow yourself.

    • 編寫配置文件

    • # This is a basic workflow to help you get started with Actions
      
      name: clrs-blog
      
      # Controls when the action will run. 
      on:
        # Triggers the workflow on push or pull request events but only for the main2021 branch
        push:
          branches: [ main2021 ]
      
        # Allows you to run this workflow manually from the Actions tab
        workflow_dispatch:
      
      # 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:
          # The type of runner that the job will run on
          runs-on: ubuntu-latest
      
          # Steps represent a sequence of tasks that will be executed as part of the job
          steps:
            # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
            - uses: actions/checkout@v2
            - uses: actions/setup-node@v2
              with: 
                node-version: '>=12.x'
      
            # Runs a set of commands using the runners shell
            - name: Install hexo-cli
              run: |
               npm install hexo-cli -g
               npm install
               
            - name: Config Private Key
              env:
               PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }} #此處的PRIVATE_KEY 是配置私有庫時settings-》secrets的名稱
              run: |
               mkdir -p ~/.ssh/
               echo "$PRIVATE_KEY" > ~/.ssh/github_rsa 
               chmod 600 ~/.ssh/github_rsa
               ssh-keyscan github.com >> ~/.ssh/known_hosts
            - name: Config Git
              run: | 
               git config --global user.name 'xxx' 
               git config --global user.email '[email protected]'
            - name: Deploy
              run: |
               hexo clean & hexo g & hexo d
      

  3.設置hexo 配置文件

   在_config.yml中找到deploy節點,配置如下

deploy:
  type: git
  repo: [email protected]:xxxxx/xxxxx.github.io.git #github庫 ssh 地址,此處一定要複製 ssh地址,如果用https地址,actions 運行的時候,執行到hexo deploy 會報錯 could not read Username for 'https://github.com': No such device or address
  branch: main2021
  name: xxxx
  email: [email protected]

  最後,做好以上配置,即可寫新文章然後推送到github,actions 將自動運行並將生成的靜態文件部署到公開庫中。

yaml 參考:YAML 語言教程 - 阮一峯的網絡日誌 (ruanyifeng.com)

github actions 參考:GitHub Actions 入門教程 - 阮一峯的網絡日誌 (ruanyifeng.com)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章