github action

本文是 github workflows 部署的第一篇 blog 。

github Action 部署 github page 。这里以 blog 源码和 page 仓库为不同的仓库举例。

bloghexo 仓库。在 .github/workflows/node.js.yml 编写 action 流程

# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Node.js CI

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:
  build:

    runs-on: ubuntu-latest
    if: github.event.repository.owner.id == github.event.sender.id
    strategy:
      matrix:
        node-version: [18.17.0]

        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

    steps:
    - uses: actions/checkout@v3
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v3
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
    - name: Install Dependencies
      run: |
        wget -c https://github.com/jgm/pandoc/releases/download/2.14.0.3/pandoc-2.14.0.3-1-amd64.deb
        sudo dpkg -i pandoc-2.14.0.3-1-amd64.deb
        pandoc --version
    - run: npm ci
    - name: Init Env
      env:
        ACTION_DEPLOY_KEY: ${{ secrets.HEXO_DEPLOY_KEY }}
      run: |
        mkdir -p ~/.ssh/
        echo "$ACTION_DEPLOY_KEY" > ~/.ssh/id_rsa
        chmod 700 ~/.ssh
        chmod 600 ~/.ssh/id_rsa
        ssh-keyscan github.com >> ~/.ssh/known_hosts
        git config --global user.email "username@outlook.com"
        git config --global user.name "username"
    - name: Deploy
      run: |
        npm run clean
        npm run build
        npm run deploy

这里还不能直接使用。因为ACTION_DEPLOY_KEY: ${{ secrets.HEXO_DEPLOY_KEY }} 是一个需要设置的变量。我们针对blog单独生成一对密钥。

ssh-keygen -f github-deploy-key -C "HEXOCD"

执行后,会生成github-deploy-keygithub-deploy-key.pub,其中blog仓库是推送方,需要设置私钥也就是github-deploy-key,page是目标仓库,需要设置公钥

位置如下

评论列表