You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将GitLab仓库提交推送至其他私有仓库?能否通过GitLab CI实现?

手动将GitLab仓库提交推送到另一个私有仓库的方法
  • 首先在本地克隆源GitLab仓库:git clone <源仓库地址>
  • 进入本地仓库目录后,添加目标私有仓库作为新的远程节点,可命名为target:git remote add target <目标私有仓库地址>
  • 若需推送全部分支和标签,执行:git push target --all && git push target --tags
  • 若只需推送特定分支(如main分支),执行:git push target main
  • 后续源仓库有更新时,可拉取最新内容后同步到目标仓库:git pull origin main && git push target main
用GitLab CI实现跨仓库自动推送的方案

完全可以实现,按以下步骤操作即可:

  • 首先生成有权限推送目标私有仓库的SSH密钥对,不要设置密码:ssh-keygen -t ed25519 -C "gitlab-ci-cross-repo-push" -f deploy_key
  • 将公钥(deploy_key.pub文件内容)添加到目标私有仓库的部署密钥中,务必勾选允许写入权限选项,否则仅能拉取无法推送
  • 将私钥(deploy_key文件内容)添加到源仓库的CI/CD变量中,变量名可设为TARGET_DEPLOY_KEY,勾选掩码变量选项避免密钥泄露
  • 在源仓库根目录创建.gitlab-ci.yml配置文件,参考配置如下:
stages:
  - sync

sync_to_target_repo:
  stage: sync
  only:
    - main # 可调整为你需要触发同步的分支
  before_script:
    - 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )'
    - eval $(ssh-agent -s)
    - echo "$TARGET_DEPLOY_KEY" | tr -d '\r' | ssh-add -
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - ssh-keyscan gitlab.com >> ~/.ssh/known_hosts # 私有部署GitLab替换为你的实例域名
    - chmod 644 ~/.ssh/known_hosts
    - git config --global user.name "GitLab CI Sync Bot"
    - git config --global user.email "ci-bot@yourdomain.com"
  script:
    - git remote add target git@<GitLab域名>:<目标仓库命名空间>/<目标仓库名>.git
    - git push target main # 不需要强制覆盖就去掉--force参数,有同步覆盖需求可添加
  • 若需同步所有分支和标签,可将script中的push命令替换为git push target --all && git push target --tags
  • 若目标仓库属于不同GitLab实例,将ssh-keyscan对应的域名替换为目标仓库的GitLab实例域名即可
  • 所有敏感信息必须通过CI变量存储,禁止直接硬编码在配置文件中

内容的提问来源于stack exchange,提问作者János

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 14:45:02