目录

GitHub Repository Sync

让一个本地 Git Repository 使用多个 Remote, git push 时, 同时推送到多个 Remote, 参考 git remote set-url --add <name> <newurl>, 运行命令, 比如:

bash

git remote set-url --add origin [email protected]:senzyo_sama/blog.git

使用 git remote -v 查看得到类似信息:

bash

origin  [email protected]:senzyo/blog.git (fetch)
origin  [email protected]:senzyo/blog.git (push)
origin  [email protected]:senzyo_sama/blog.git (push)

这样在 git push 时, 就会同时推送到两个源。

  1. 打开 https://github.com/settings/tokens 生成 Fine-grained personal access tokens。
  2. Repository access 选择全部仓库或指定仓库。
  3. Permissions 的 Repository permissions 设置 ContentsRead and write 即可。
  4. 生成 token 并复制。
  5. 在你专门用于运行 GitHub Actions 来同步的 Repository 中点击 SettingsSecrets and variablesActionsNew repository secrets
  6. Name 填写 PERSONAL_TOKEN, Secret 粘贴刚才的 token。
  7. 按照你的需要编辑下面的 GitHub Actions 文件。

yaml

name: Pull from upstream every 3 days

on:
  schedule:
    - cron: "0 20 */3 * *" # 4:00 AM UTC+8
  workflow_dispatch:
  push:
    branches:
      - master

jobs:
  ChatGemini:
    name: Sync ChatGemini
    runs-on: ubuntu-latest
    steps:
      - name: Sync ChatGemini
        run: |
          git clone --branch master https://github.com/bclswl0827/ChatGemini.git
          cd ChatGemini
          git remote set-url origin https://${{ secrets.PERSONAL_TOKEN }}@github.com/senzyo/ChatGemini.git
          git push -uf origin master:master

  github-readme-stats:
    name: Sync github-readme-stats
    runs-on: ubuntu-latest
    steps:
      - name: Sync github-readme-stats
        run: |
          git clone --branch master https://github.com/anuraghazra/github-readme-stats.git
          cd github-readme-stats
          git remote set-url origin https://${{ secrets.PERSONAL_TOKEN }}@github.com/senzyo/github-readme-stats.git
          git push -uf origin master:master
  1. 打开 https://gitlab.com/-/user_settings/personal_access_tokens 生成个人访问令牌。
  2. 注意: 选择范围勾选 api, read_api, read_user, read_repository, write_repository
  3. 生成 token 并复制。
  4. 在你专门用于运行 GitHub Actions 来同步的 Repository 中点击 SettingsSecrets and variablesActionsNew repository secrets
  5. Name 填写 GITLAB_PERSONAL_TOKEN, Secret 粘贴刚才的 token。
  6. 按照你的需要编辑下面的 GitHub Actions 文件。

yaml

name: Push to GitLab every 3 days

on:
  schedule:
    - cron: "0 20 */3 * *" # 4:00 AM UTC+8
  workflow_dispatch:
  push:
    branches:
      - master

jobs:
  as-gist:
    name: Sync as-gist
    runs-on: ubuntu-latest
    steps:
      - name: Sync as-gist
        run: |
          git clone --branch master https://github.com/senzyo/as-gist.git
          cd as-gist
          git remote set-url origin https://oauth2:${{ secrets.GITLAB_PERSONAL_TOKEN }}@gitlab.com/senzyo_sama/as-gist.git
          git push -uf origin master:master

  blog:
    name: Sync blog
    runs-on: ubuntu-latest
    steps:
      - name: Sync blog
        run: |
          git clone --branch master https://github.com/senzyo/blog.git
          cd blog
          git remote set-url origin https://oauth2:${{ secrets.GITLAB_PERSONAL_TOKEN }}@gitlab.com/senzyo_sama/blog.git
          git push -uf origin master:master