0%

利用Gitee作为跳板使用Github Pages

其实现在的AI真的很强,随便选择一家的产品,都能得到想要的结果。从这一方面来看,用博客来记录,或多或少都没有什么太大必要。因此在这篇里,主要记录个思路,细节上的调整都可以交给大模型来处理。

我的初衷是来自于写了一个自动发布博客的脚本,每天定时用hexo发布github pages。但由于中国大陆访问github总是不太稳定,脚本经常失败。想找一个既能保证推送稳定,又能使用github pages的方法。这才想到是不是可以将gitee.io作为源头,每次用hexo发布到gitee.io。将github.io作为目的地,同步gitee.io的内容,发布github pages博客。

主要的步骤其实很简单:

  1. 在gitee建立一个名称为 XXXXX.gitee.io 的仓库

  2. 在github建立一个名称为 XXXXX.github.io 的仓库

  3. 在本地生成SSH密钥

    1
    ssh-keygen -t ed25519 -C "your_email@example.com"

    按提示操作

    1
    2
    3
    4
    5
    6
    7
    8
    # 第1次提示:选择保存位置(直接回车使用默认位置)
    > Enter file in which to save the key (/Users/yourname/.ssh/id_ed25519): [回车]

    # 第2次提示:设置 passphrase(直接回车不设密码)
    > Enter passphrase (empty for no passphrase): [回车]

    # 第3次提示:确认 passphrase(直接回车)
    > Enter same passphrase again: [回车]

    会生成id_ed25519id_ed25519.pub

  4. 配置Gitee SSH公钥

    • 登录Gitee访问https://gitee.com/profile/sshkeys

    • 点击 「添加公钥」按钮

    • 填写信息。标题 GitHub Actions Sync (可自定义);公钥内容黏贴id_ed25519.pub 的完整内容

    • 点击 「确定」

  5. 配置Github Secrets

    • 在Github仓库XXXXX.github.io 点击 「Settings」 标签

    • 左侧菜单找到 「Secrets and variables」 → 「Actions」

    • 添加2个Secrets

      Secret 名称 说明
      GITEE_SSH_PRIVATE_KEY id_ed25519 私钥完整内容 用于 Gitee SSH 认证
      GITEE_PAGES_REPO_URL git@gitee.com:用户名/仓库名.git Gitee 仓库 SSH 地址
  6. 创建GitHub Actions工作流

    • 在Github仓库XXXXX.github.io 创建 .github/workflows/sync-from-gitee.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
      name: Sync from Gitee

      on:
      schedule:
      - cron: '*/10 * * * *'
      workflow_dispatch:

      jobs:
      sync:
      runs-on: ubuntu-latest

      steps:
      # 1. 检出 GitHub 仓库(启用凭证持久化)
      - name: Checkout GitHub Repo
      uses: actions/checkout@v4
      with:
      fetch-depth: 0
      persist-credentials: true # ✅ 关键:启用凭证持久化

      # 2. 配置 SSH 密钥(仅用于 Gitee)
      - name: Setup SSH Key for Gitee
      run: |
      mkdir -p ~/.ssh
      echo "${{ secrets.GITEE_SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
      chmod 600 ~/.ssh/id_ed25519
      ssh-keyscan -H gitee.com >> ~/.ssh/known_hosts
      git config --global user.name "GitHub Actions"
      git config --global user.email "actions@github.com"

      # 3. 添加 Gitee 远程仓库
      - name: Add Gitee Remote
      run: |
      git remote add gitee ${{ secrets.GITEE_PAGES_REPO_URL }}

      # 4. 从 Gitee 拉取最新代码
      - name: Fetch from Gitee
      run: |
      git fetch gitee
      # 自动检测 Gitee 的主分支名称
      GITEE_BRANCH=$(git remote show gitee | grep 'HEAD branch' | awk '{print $NF}')
      git checkout -b gitee-temp gitee/$GITEE_BRANCH 2>/dev/null || \
      git checkout -b gitee-temp gitee/main 2>/dev/null || \
      git checkout -b gitee-temp gitee/master

      # 5. 合并代码到主分支
      - name: Merge Gitee to GitHub
      run: |
      # 自动检测本地主分支名称
      LOCAL_BRANCH=$(git remote show origin | grep 'HEAD branch' | awk '{print $NF}')
      git checkout $LOCAL_BRANCH
      git merge gitee-temp --no-edit --allow-unrelated-histories
      git branch -D gitee-temp

      # 6. 推送到 GitHub(使用 GITHUB_TOKEN)
      - name: Push to GitHub
      run: |
      LOCAL_BRANCH=$(git remote show origin | grep 'HEAD branch' | awk '{print $NF}')
      git push origin $LOCAL_BRANCH
      env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # ✅ 关键:使用 GitHub Token
  7. 开启读写权限

    • 在仓库 Settings 找到 Code and automation

    • 点击 Actions → General

    • 将 Workflow permissions 改为 Read and write permissions