从手动到自动:代码多平台同步的最终秘籍

20250214165707358134.png

一、背景与需求

在日常开发中,我习惯将代码存储在 Github 上。然而,由于网络访问的限制,时常会遇到无法访问的情况。为了确保其他用户能够顺利访问我的代码,我期望在向 Github 提交代码时,能够同步将代码推送到多个代码托管平台,如 Gitee、Gitlab、Bitbucket 等。

二、初始方案:本地配置多仓库地址

起初,我通过在本地配置多个仓库地址来实现这一需求,具体命令如下:

git remote add github 仓库地址
git remote add gitee 仓库地址 

但是,每次提交代码时,都需要手动依次将代码推送到各个对应的仓库,操作较为繁琐:

git push github main
git push gitee main

三、使用 Github Action 实现自动化同步

后来,Github Action 的出现提供了自动化操作的解决方案。我开始使用它来实现多平台同步。不过,在使用多个平台同步功能时,需要事先向每个同步平台申请访问 Token。这些 Token 通常只能显示一次,当开启新的项目时,我常常忘记之前的 Token,导致难以再次配置。

之前使用的同步代码如下:

# Sample workflow for building and deploying a Jekyll site to GitHub Pages
name: Push Other Pages
on:
  # Runs on pushes targeting the default branch
  push:
    branches: ["main"]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:
  # Build job
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          ref: gh-pages
          fetch-depth: 0 # <-- clone with complete history
      - name: Sync Repo to Bitbucket
        uses: heussd/mirror-to-bitbucket-github-action@v2
        with:
          username: flowstone
          spacename: flowstone
          repository: flowstone.bitbucket.io
          password: ${{ secrets.BITBUCKET_PASSWORD }}
      - name: Sync Repo to Coding
        uses: serverlesslife-cn/sync-repo-to-coding@master
        env:
            # 在 GitHub Settings->Secrets 配置 CODING_PRIVATE_KEY
            SSH_PRIVATE_KEY: ${{ secrets.CODING_PRIVATE_KEY }}
        with:
            # 注意替换为你的 GitHub 源仓库地址
            github-repo: "git@github.com:flowstone/flowstone.github.io.git"
            # 注意替换为你的 CODING 目标仓库地址
            coding-repo: "git@e.coding.net:flowstone/flowstone.coding.me.git"
      - name: Sync Repo to Gitlab
        uses: action-pack/gitlab-sync@v3
        with:
          username: ${{ secrets.USERNAME }}
          url: ${{ secrets.GITLAB_URL }}
          # Personal access token from gitlab.com 
          token: ${{ secrets.GITLAB_PAT }}

tmpuia1yr2i.png

四、新方法:SSH 私钥同步方案

后来,我从豆包处了解到一种新的同步方法。只需将 SSH 的私钥保存到 Github 仓库的 Secret 中,当执行 Github Action 脚本时,服务器就拥有私钥,从而可以同步绑定了相同公钥的代码平台。虽然这种方式存在一定风险,因为只要拥有私钥就可以访问所有仓库,但对于个人开发者而言,影响不大。

4.1 操作流程

  1. 找到本地的 SSH 私钥,一般默认绑定的是id_rsa。

    tmpraji5xgq.png

  2. 打开id_rsa文件。

    tmpanzzb6ox.png

  3. 将文件内容配置到 Github 仓库中,每个需要同步的仓库都需要进行此配置。

    tmpumvj3lcs.png

    tmp6fz1k9yd.png

4.2 同步脚本

同步脚本如下:

name: Sync Multi - Branches to Multiple Platforms via SSH

on:
  push:
    branches:
      - '*'  # 监听所有分支的推送事件
  workflow_dispatch:

jobs:
  sync:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0  # 进行完整克隆
      
      - name: Configure Git user
        run: |
          git config --global user.name '流体石头'
          git config --global user.email 'example@example.com'

      - name: Add SSH key
        uses: webfactory/ssh-agent@v0.8.0
        with:
          ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}

      - name: Add remote repositories
        run: |
          git remote add gitee git@gitee.com:flowstone/FS-Tool-Pro.git
          git remote add bitbucket  git@bitbucket.org:flowstone/FS-Tool-Pro.git
          git remote add gitlab  git@gitlab.com:flowstone/FS-Tool-Pro.git

      - name: Add host keys to known_hosts
        run: |
          ssh-keyscan -H gitee.com >> ~/.ssh/known_hosts
          ssh-keyscan -H bitbucket.org >> ~/.ssh/known_hosts
          ssh-keyscan -H gitlab.com >> ~/.ssh/known_hosts

      
      - name: Get current branch
        id: get_branch
        run: echo "::set-output name=branch::${GITHUB_REF#refs/heads/}"

      - name: Push to targets
        run: |
          git push --force gitee ${{ steps.get_branch.outputs.branch }}
          git push --force bitbucket ${{ steps.get_branch.outputs.branch }}
          git push --force gitlab ${{ steps.get_branch.outputs.branch }}

五、注意事项

在上述同步代码中,需要指定同步的仓库地址。每次推送代码时,工作流都会自动执行。需要注意的是,其他代码平台必须配置相同的id_rsa公钥,并且创建相同的仓库名。

tmp7ng6p80f.png

tmplu3zu14t.png

posted @   薛尧笔记  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~
薛尧的博客
点击右上角即可分享
微信分享提示