4

GitHub Action + Gitee 构建同步国内和国外站点

 2 years ago
source link: https://lianpf.github.io/posts/%E5%BC%80%E5%8F%91%E6%97%A5%E8%AE%B0/20.gitee%E5%90%8C%E6%AD%A5%E6%9E%84%E5%BB%BA%E5%8D%9A%E5%AE%A2/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

一、回顾『国外站点gitHub』自动构建发布

GitHub Action + Hugo 自动构建发布个人博客请参考这里:自动化构建博客


二、『国内站点gitee』同步构建发布

因为不使用付费的Gitee Pages Pro服务,那么Gitee不能像GitHub一样自动部署Pages,而我们又想实现Blogs国外和国内两个站点的同步构建发布。那么思路如下:

  • 首先保证使用GitHub Actions,可以实现国外站点的自动构建发布
  • 同步GitHub代码到Gitee: 使用GitHub actions Yikun/hub-mirror-action@master
  • Gitee构建发布:yanglbme/gitee-pages-action@main

1. secrets配置

在执行gitHub Actions脚本的仓库里:

  • 配置secrets.GITEE_PRIVATE_KEY
  • 配置secrets.GITEE_TOKEN
  • 配置secrets.GITEE_PASSWORD
  • 配置secrets的仓库,是执行Actions的仓库xxx/Blogs.git,而不是国外站点静态文件存储仓库xxx/xxx.github.io.git
  • 配置secrets的位置是在xxx/Blogs.git仓库的Settings -> Secrets -> Actions位置,点击 New repository secret
  • secrets.GITEE_PRIVATE_KEY配置一定要是『本地的密钥对』全局ssh key的密钥,我这里配置的是id_rsa
  • secrets.GITEE_PRIVATE_KEY对应的公钥(id_rsa.pub),要配置在『需要同步的远程仓库』里,我这里是Gitee

ssh生成参考:git中ssh与https | Blogs

2.『非同名仓库』代码同步问题:Yikun/hub-mirror-action@master

  • 使用Yikun/hub-mirror-action@master可实现从GitHubGitee的代码同步
  • 但对于国外站点lianpf.github.io和国内站点lianpf仓库不同名的问题,需要增加配置:mappings: "lianpf.github.io=>lianpf"

使用配置如下:

name: GiteePages

...

jobs:
  deploy:
    runs-on: ubuntu-18.04
    steps:
      - name: Sync to Gitee
        uses: Yikun/hub-mirror-action@master
        with:
          # 替换为你的 GitHub 用户名
          src: github/lianpf
          # 替换为你的 Gitee 用户名
          dst: gitee/lianpf
          dst_key: ${{ secrets.GITEE_PRIVATE_KEY }}
          dst_token:  ${{ secrets.GITEE_TOKEN }}
          # 仓库名不同时,同步
          mappings: "lianpf.github.io=>lianpf"
          static_list: "lianpf.github.io"
          force_update: true
          debug: true
      ...

3. 国内站点Gitee Page发布问题:yanglbme/gitee-pages-action@main

name: GiteePages
...

jobs:
  deploy:
    runs-on: ubuntu-18.04
    steps:
      ...
      - name: Build Gitee Pages
        uses: yanglbme/gitee-pages-action@main
        with:
          # 注意替换为你的 Gitee 用户名
          gitee-username: lianpf
          # 注意替换为你的 Gitee 用户名
          gitee-password: ${{ secrets.GITEE_PASSWORD }}
          # 注意替换为你的 Gitee 仓库,仓库名严格区分大小写,请准确填写,否则会出错
          gitee-repo: lianpf/lianpf
          # 要部署的分支,默认是 master,若是其他分支,则需要指定(指定的分支必须存在)
          branch: master

三、双站点『自动化同步构建发布』完整配置

name: GitHubPages

on:
  push:
    branches:
      - master  # 设置部署分支
    paths-ignore: # 忽略文件
      - 'README.md'
      - '.env'
      - '.gitignore'
      - 'bin'

jobs:
  deploy:
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: recursive  # Fetch Hugo themes (true OR recursive)
          fetch-depth: 0    # Fetch all history for .GitInfo and .Lastmod

      # - name: Read .env
      #   id: hugo-version
      #   run: |
      #     . ./.env
      #     echo "::set-output name=HUGO_VERSION::${HUGO_VERSION}"

      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v2
        with:
          # hugo-version: '${{ steps.hugo-version.outputs.HUGO_VERSION }}'
          hugo-version: '0.75.1' # 0.75.1 latest
          extended: true
          
      - name: Build
        run: |
          hugo --minify
          echo -e "Generated by Hugo.\nLatest build: $(date).\n" > public/README

      - name: Deploy GitHub
        uses: peaceiris/actions-gh-pages@v3
        with:
          deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }}
          external_repository: lianpf/lianpf.github.io
          publish_branch: master
          publish_dir: ./public
      
      - name: Sync to Gitee
        uses: Yikun/hub-mirror-action@master
        with:
          # 替换为你的 GitHub 用户名
          src: github/lianpf
          # 替换为你的 Gitee 用户名
          dst: gitee/lianpf
          dst_key: ${{ secrets.GITEE_PRIVATE_KEY }}
          dst_token:  ${{ secrets.GITEE_TOKEN }}
          # 仓库名不同时,同步
          mappings: "lianpf.github.io=>lianpf"
          static_list: "lianpf.github.io"
          force_update: true
          debug: true

      - name: Build And Deploy Gitee Pages
        uses: yanglbme/gitee-pages-action@main
        with:
          # 注意替换为你的 Gitee 用户名
          gitee-username: lianpf
          # 注意替换为你的 Gitee 用户名
          gitee-password: ${{ secrets.GITEE_PASSWORD }}
          # 注意替换为你的 Gitee 仓库,仓库名严格区分大小写,请准确填写,否则会出错
          gitee-repo: lianpf/lianpf
          # 要部署的分支,默认是 master,若是其他分支,则需要指定(指定的分支必须存在)
          branch: master


最后, 希望大家早日实现:成为前端高手的伟大梦想!
欢迎交流~

微信公众号

本文版权归原作者曜灵所有!未经允许,严禁转载!对非法转载者, 原作者保留采用法律手段追究的权利!
若需转载,请联系微信公众号:连先生有猫病,可获取作者联系方式!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK