从手动到自动:代码多平台同步的最终秘籍
一、背景与需求
在日常开发中,我习惯将代码存储在 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 }}
四、新方法:SSH 私钥同步方案
后来,我从豆包处了解到一种新的同步方法。只需将 SSH 的私钥保存到 Github 仓库的 Secret 中,当执行 Github Action 脚本时,服务器就拥有私钥,从而可以同步绑定了相同公钥的代码平台。虽然这种方式存在一定风险,因为只要拥有私钥就可以访问所有仓库,但对于个人开发者而言,影响不大。
4.1 操作流程
-
找到本地的 SSH 私钥,一般默认绑定的是id_rsa。
-
打开id_rsa文件。
-
将文件内容配置到 Github 仓库中,每个需要同步的仓库都需要进行此配置。
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公钥,并且创建相同的仓库名。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~