前言

鉴于已经实现了Coding与Github的双部署,而目前使用了GitHub的actions作为主要的生成Hexo页面的环境,但是由于私有仓库使用时间收到了限制。因此考虑将token方式转换为ssh方式。

  1. GitHub可以通过自己的私钥通过ssh地址部署,但Coding不可以

    发现用户级别公钥可以实现本地的部署, 但是无法实现 repo 之间的部署, 一直出现Permission denied (publickey)

    因此只能使用token

  2. 通过Coding的token尝试,发现了也可以在GitHub使用token,并且比较方便。

Coding配置文件

pipeline {
  agent any
  stages {
    stage('获取最新git提交') {
      steps {
        checkout([$class: 'GitSCM', branches: [[name: env.GIT_BUILD_REF]],
        userRemoteConfigs: [[url: env.GIT_REPO_URL, credentialsId: env.CREDENTIALS_ID]]])
      }
    }
    stage('安装依赖') {
      steps {
        sh 'npm install hexo-cli -g'
        sh 'npm install gulp -g'
        sh 'npm install'
      }
    }
    stage('生成页面并压缩') {
      steps {
        sh 'hexo cl && hexo g && gulp'
      }
    }
    stage('发布网页') {
      steps {
        sh '''
          pwd
          cd ./public
          pwd
          git init
          git config --global user.name \'${GIT_USER}\'
          git config --global user.email \'${GIT_MAIL}\'
          git add .
          git commit -m "Update"
          git push --force --all https://${CODING_TOKEN}@${CODING_REPO}
          git push --force --all https://${GH_TOKEN}@${GH_REPO}
        '''
      }
    }
  }
}

其中需要配置的环境变量:

  • GH_TOKEN

    GitHub的token。获取方式:Creating a personal access token

  • CODING_TOKEN

    Coding的token。coding的token比较特殊,包含用户和密码。因此获取后将此字段填入的为账户:密码。例如:

    image-20200626161411316

    以上图为例的账户和密码,则此字段完整填入的为abc:aabbcc

    令牌获取方式参照:Hexo 利用 coding 自动部署

  • GIT_USER

    git的name。用于设置全局git基本信息

  • GIT_MAIL

    git的email。用于设置全局git基本信息

  • GH_REPO

    GitHub仓库地址。格式为HTTPS后边的地址。例如github.com/xiaokang/xiaokang.git。切记不要加上协议头或者使用ssh地址

  • CODING_REPO

    Coding的仓库地址。格式为HTTPS后边的地址。例如e.coding.net/xiaokang/xiaokang.git。切记不要加上协议头或者使用ssh地址

Github Actions配置文件

1. 密钥方式

# workflow name
name: Hexo Blog CI

# master branch on push, auto run
on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [10.x]
    steps:
      # check it to your workflow can access it
      # from: https://github.com/actions/checkout
      - name: 1. 检查master分支
        uses: actions/checkout@master

      # from: https://github.com/actions/setup-node
      - name: 2. 设置Node.js
        uses: actions/setup-node@master
        with:
          node-version: ${{ matrix.node-version }}

      - name: 3. 安装 Hexo CI
        run: |
          export TZ='Asia/Shanghai'
          npm install hexo-cli -g
          npm install gulp -g

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

      - name: 5. 安装插件
        if: steps.cache-dependencies.outputs.cache-hit != 'true'
        run: |
          npm install

      - name: 6. 设置私钥信息
        env:
          # HEXO_DEPLOY_PRIVATE_KEY: ${{ secrets.HEXO_DEPLOY_PRIVATE_KEY }}
          CODEING_KEY: ${{ secrets.CODEING_KEY }}
          GH_KEY: ${{ secrets.GH_KEY }}
        run: |
          mkdir -p ~/.ssh/
          echo "$GH_KEY" > ~/.ssh/id_rsa
          echo "$CODEING_KEY" > ~/.ssh/coding_rsa
          chmod 600 ~/.ssh/id_rsa
          chmod 600 ~/.ssh/coding_rsa
          ssh-keyscan github.com >> ~/.ssh/known_hosts
          ssh-keyscan e.coding.net >> ~/.ssh/known_hosts  # 注意,Coding的地址已经改成e.coding.net,很多教程中使用的地址都是旧的,导致无法完成部署
      - name: 7. 设置git信息
        run: |
          git config --global user.name '${{ secrets.GIT_USER }}' 
          git config --global user.email '${{ secrets.GIT_MAIL }}'
      - name: 8. hexo命令三连
        run: |
          hexo clean
          hexo generate 
          gulp
          hexo deploy
  • GIT_USER

    git的name。用于设置全局git基本信息

  • GIT_MAIL

    git的email。用于设置全局git基本信息

  • CODEING_KEY

    向coding仓库推送的私钥

  • GH_KEY

    向github仓库推送的私钥

2. Token方式

# workflow name
name: Hexo Blog CI

# master branch on push, auto run
on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [10.x]
    steps:
      # check it to your workflow can access it
      # from: https://github.com/actions/checkout
      - name: 1. 检查master分支
        uses: actions/checkout@master

      # from: https://github.com/actions/setup-node
      - name: 2. 设置Node.js
        uses: actions/setup-node@master
        with:
          node-version: ${{ matrix.node-version }}

      - name: 3. 安装 Hexo CI
        run: |
          export TZ='Asia/Shanghai'
          npm install hexo-cli -g
          npm install gulp -g

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

      - name: 5. 安装插件
        if: steps.cache-dependencies.outputs.cache-hit != 'true'
        run: |
          npm install
      - name: 6. 生成页面并压缩
        run: |
          hexo cl && hexo g && gulp
      - name: 7. 部署页面
        run: |
          cd ./public
          ls
          git init
          git config --global user.name '${{ secrets.GIT_USER }}' 
          git config --global user.email '${{ secrets.GIT_MAIL }}'
          git add .
          git commit -m "Update"
          git push --force --all https://${{ secrets.GH_TOKEN }}@${{ secrets.GH_REPO }}
          git push --force --all https://${{ secrets.CODING_TOKEN }}@${{ secrets.CODING_REPO }}

其中需要配置的环境变量:

  • GH_TOKEN

    GitHub的token。获取方式:Creating a personal access token

  • CODING_TOKEN

    Coding的token。coding的token比较特殊,包含用户和密码。因此获取后将此字段填入的为账户:密码。例如:

    image-20200626161411316

    以上图为例的账户和密码,则此字段完整填入的为abc:aabbcc

    令牌获取方式参照:Hexo 利用 coding 自动部署

  • GIT_USER

    git的name。用于设置全局git基本信息

  • GIT_MAIL

    git的email。用于设置全局git基本信息

  • GH_REPO

    GitHub仓库地址。格式为HTTPS后边的地址。例如github.com/xiaokang/xiaokang.git。切记不要加上协议头或者使用ssh地址

  • CODING_REPO

    Coding的仓库地址。格式为HTTPS后边的地址。例如e.coding.net/xiaokang/xiaokang.git。切记不要加上协议头或者使用ssh地址