生成公钥
Windows 上安装 Git for Windows 之后在开始菜单里打开 Git Bash 输入:
1 2
   | git config --global user.name "你的用户名" git config --global user.email "你的电子邮箱"
   | 
 
1 2 3 4
   | cd ~ mkdir .ssh cd .ssh ssh-keygen -t rsa
   | 
 
在系统当前用户文件夹下生成了私钥 id_rsa 和公钥 id_rsa.pub。
上传id_rsa.pub
在右上角个人账户依次点击Settings->SSH and GPG keys添加刚刚生成的公钥,名称随意。
上传id_rsa
然后在 Settings > Secrets 中新增一个 secret,命名为 DEPLOY_KEY,把私钥 id_rsa 的内容复制进去,供后续使用。
配置 GitHub Actions
第一种
在博客根目录新建.github/workflows/gh_pages.yml文件。代码如下:
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
   | name: Hexo Deploy
 
  on:   push:     branches:       - develop
 
  env:   POST_ASSET_IMAGE_CDN: true
  jobs:   build-and-deploy:     runs-on: ubuntu-latest
      steps:              - name: Checkout         uses: actions/checkout@v2.3.4
        - name: Checkout theme repo         uses: actions/checkout@v2.3.4         with:           repository: jerryc127/hexo-theme-butterfly           ref: master           path: themes/butterfly
               - name: Set up Node.js         uses: actions/setup-node@v2         with:           node-version: '14'
               - name: Get yarn cache directory path         id: yarn-cache-dir-path         run: echo "::set-output name=dir::$(yarn cache dir)"
        - name: Use yarn cache         uses: actions/cache@v2.1.6         id: yarn-cache         with:           path: ${{ steps.yarn-cache-dir-path.outputs.dir }}           key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}           restore-keys: |             ${{ runner.os }}-yarn-
               - name: Install dependencies         run: |           yarn install --prefer-offline --frozen-lockfile
               - name: Set up environment         env:           DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }}         run: |           sudo timedatectl set-timezone "Asia/Shanghai"           mkdir -p ~/.ssh           echo "$DEPLOY_KEY" > ~/.ssh/id_rsa           chmod 600 ~/.ssh/id_rsa           ssh-keyscan github.com >> ~/.ssh/known_hosts
               - name: Deploy         run: |           npx hexo deploy --generate                  - name: Deploy Hexo to Server         uses: SamKirkland/FTP-Deploy-Action@4.1.0         with:           server: 104.224.191.88           username: admin           password: ${{ secrets.FTP_MIRROR_PASSWORD }}           local-dir: ./public/           server-dir: /var/www/blog/
   | 
 
第二种
PS:第二种比第一种慢。
首先确认 _config.yml 文件中有类似如下的 GitHub Pages 配置:
1 2 3 4 5
   | deploy:   type: git   repo:     github: git@github.com:iwyang/hexo.git   branch: gh-pages
   | 
 
配置私钥
- 首先在 GitHub 上打开保存 Hexo 的仓库,访问 Settings -> Secrets,然后选择 New secret;
 
- 名字部分填写:HEXO_DEPLOY_KEY,注意大小写,这个后面的 GitHub Actions Workflow 要用到;
 
- 在 Value 的部分填入 github-deploy-key 中的内容。
 
添加公钥
- 接下来我们需要访问存放网页的仓库,也就是 Hexo 部署以后的仓库,访问 Settings -> Deploy keys;
 
- 按 Add deploy key 来添加一个新的公钥;
 
- 在 Title中输入:HEXO_DEPLOY_PUB 字样,当然也可以填写其它自定义的名字;
 
- 在 Key 中粘贴 github-deploy-key.pub文件的内容。
 
创建 Workflow
在 Hexo 的仓库中创建一个新文件:.github/workflows/auto_deploy.yml,文件的内容如下:
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
   | name: auto deploy 
 
  on:   push:     branches:       - develop
  jobs:   build:      runs-on: ubuntu-latest      name: auto deploy     steps:     - name: Checkout        uses: actions/checkout@v1        with:          submodules: true      - name: Setup Node.js 12.x       uses: actions/setup-node@master       with:         node-version: "12.x"     - name: Generate Public Files       run: |         npm i         npm install hexo-cli -g         hexo clean && hexo generate     - name: Deploy       uses: peaceiris/actions-gh-pages@v3       with:         deploy_key: ${{ secrets.HEXO_DEPLOY_KEY }}         external_repository: iwyang/hexo         publish_branch: public         publish_dir: ./public         commit_message: ${{ github.event.head_commit.message }}         user_name: 'github-actions[bot]'         user_email: 'github-actions[bot]@users.noreply.github.com'     - name: Deploy Hexo to Server       uses: SamKirkland/FTP-Deploy-Action@4.1.0       with:         server: 104.224.191.88         username: hexo         password: ${{ secrets.FTP_MIRROR_PASSWORD }}         local-dir: ./public/         server-dir: /var/www/hexo/
   | 
 
github、gitee、服务器三线部署
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 89 90 91 92 93 94 95 96 97 98 99 100
   | name: Hexo Deploy
 
  on:   push:     branches:       - develop
 
  env:   POST_ASSET_IMAGE_CDN: true
  jobs:   build-and-deploy:     runs-on: ubuntu-latest
      steps:              - name: Checkout         uses: actions/checkout@v2.3.4
        - name: Checkout theme repo         uses: actions/checkout@v2.3.4         with:           repository: jerryc127/hexo-theme-butterfly           ref: master           path: themes/butterfly
               - name: Set up Node.js         uses: actions/setup-node@v2         with:           node-version: '14'
               - name: Get yarn cache directory path         id: yarn-cache-dir-path         run: echo "::set-output name=dir::$(yarn cache dir)"
        - name: Use yarn cache         uses: actions/cache@v2.1.6         id: yarn-cache         with:           path: ${{ steps.yarn-cache-dir-path.outputs.dir }}           key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}           restore-keys: |             ${{ runner.os }}-yarn-
               - name: Install dependencies         run: |           yarn install --prefer-offline --frozen-lockfile
               - name: Set up environment         env:           DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }}         run: |           sudo timedatectl set-timezone "Asia/Shanghai"           mkdir -p ~/.ssh           echo "$DEPLOY_KEY" > ~/.ssh/id_rsa           chmod 600 ~/.ssh/id_rsa           ssh-keyscan github.com >> ~/.ssh/known_hosts
               - name: Deploy         run: |           npx hexo deploy --generate                  - name: Deploy Hexo to Server         uses: SamKirkland/FTP-Deploy-Action@4.1.0         with:           server: 104.224.191.88           username: admin           password: ${{ secrets.FTP_MIRROR_PASSWORD }}           local-dir: ./public/           server-dir: /var/www/blog/                  - name: Sync to Gitee         uses: wearerequired/git-mirror-action@master         env:                      SSH_PRIVATE_KEY: ${{ secrets.GITEE_RSA_PRIVATE_KEY }}         with:                      source-repo: git@github.com:iwyang/iwyang.github.io.git                      destination-repo: git@gitee.com:iwyang/iwyang.git
        - name: Build Gitee Pages         uses: yanglbme/gitee-pages-action@main         with:                      gitee-username: iwyang                      gitee-password: ${{ secrets.GITEE_PASSWORD }}                      gitee-repo: iwyang/iwyang                      branch: master
   | 
 
将hexo三线部署(由于部署hexo较慢,如果单独为gitee建立一个workflows,gitee会先部署完成,这样无法同步;hugo可以单独为gitee建立一个workflows,因为hugo部署到服务器会先于部署到gitee)
推送到远端
配置Hexo的_config.yml
1 2 3 4 5 6 7
   | deploy:   type: git   repo:     github: git@github.com:iwyang/iwyang.github.io.git   branch: master   name: iwyang   email: 455343442@qq.com
   | 
 
当然,具体步骤还是得根据自己的需求进行相应的修改。
提交源码
今后只需备份源码到develop分支,gitbub action就会自动部署博客到iwyang.github.io仓库。
1 2 3 4 5 6
   | git init git checkout -b develop git remote add origin git@github.com:iwyang/iwyang.github.io.git git add . git commit -m "备份源码" git push --force origin develop
   | 
 
最终部署脚本
deploy.sh内容:
1 2 3 4 5 6 7 8 9
   | #!/bin/bash
  echo -e "\033[0;32mDeploying updates to gitee...\033[0m"
 
  git config --global core.autocrlf false git add . git commit -m "site backup" git push origin develop --force
   | 
 
参考链接