logo
0
0
WeChat Login
docs: 补充 dependabot 分支同步失败的说明

Git Sync Plugin

一个用于在不同 Git 平台之间同步代码的插件。支持通过 HTTPS 或 SSH 方式同步代码到其他 Git 托管平台。

例如从 CNB 同步到 GitHub。

功能特点

  • 支持 HTTPS (推荐) 和 SSH 两种认证方式
  • 支持推送指定分支或所有分支
  • 支持推送标签
  • 支持强制推送
  • 可配置 Git 用户信息
  • 支持自定义 Git 服务器
  • 支持私有仓库认证

同步模式

插件支持两种同步模式:推送模式(默认)和 Rebase 模式。

具体参数请查阅下方参数列表

从 GitHub 同步到 CNB

使用默认模式同步到 CNB

在 GitHub Actions 中使用推送模式,注意需要使用 GitHub Secrets 来安全地存储敏感信息,如果dependabot创建的分支也要同步,请同步设置 Dependabot Secrets

# .github/workflows/sync-cnb.yml

name: Sync to CNB
on: [push]

jobs:
  sync:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      
      - name: Sync to CNB Repository
        uses: docker://tencentcom/git-sync
        env:
          PLUGIN_TARGET_URL: "https://cnb.cool/username/repo.git"
          PLUGIN_AUTH_TYPE: "https"
          PLUGIN_USERNAME: "cnb"
          PLUGIN_PASSWORD: ${{ secrets.GIT_PASSWORD }}
          PLUGIN_FORCE: "true"

使用 rebase 模式同步到 CNB

在 GitHub Actions 中使用 rebase 模式:

# .github/workflows/sync-cnb.yml

name: Sync to CNB
on: [push]

jobs:
  sync:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      
      - name: Sync to CNB Repository
        uses: docker://tencentcom/git-sync
        env:
          PLUGIN_TARGET_URL: "https://cnb.cool/username/repo.git"
          PLUGIN_AUTH_TYPE: "https"
          PLUGIN_USERNAME: "cnb"
          PLUGIN_PASSWORD: ${{ secrets.GIT_PASSWORD }}
          PLUGIN_SYNC_MODE: "rebase"

使用拉取模式同步到 CNB

在 CNB 中配置流水线来定时拉取 GitHub 仓库

# .cnb.yml

main:
  "crontab: 0 1 * * *": # 每天1点执行
    - name: sync from github
      stages:
        - name: sync from github
          image: tencentcom/git-sync
          settings:
            target_url: https://github.com/username/repo.git
            auth_type: https
            username: ${GIT_USERNAME}
            password: ${GIT_ACCESS_TOKEN}
            sync_mode: pull

从 CNB 同步到 GitHub

使用默认模式同步到 GitHub

在 CNB 中使用推送模式, 建议使用密钥仓库来存储密 GIT_USERNAME、GIT_PASSWORD 等敏感信息, 然后使用 imports 引用变量,提高安全性。

# .cnb.yml
main:
  push:
    - stages:
        - name: sync to github
          image: tencentcom/git-sync
          settings:
            target_url: https://github.com/username/repo.git
            auth_type: https
            username: ${GIT_USERNAME}
            password: ${GIT_ACCESS_TOKEN}

使用 rebase 模式同步到 GitHub

通过 rebase 方式同步代码,保留目标仓库中的特定文件(如平台特定的配置文件)。适用于从 GitHub 同步到 CNB 时保留 .cnb.yml 等特有文件的场景。

在 CNB 中使用 rebase 模式:

# .cnb.yml
main:
  push:
    - stages:
        - name: sync to github with rebase
          image: tencentcom/git-sync
          settings:
            target_url: https://github.com/username/repo.git
            auth_type: https
            username: ${GIT_USERNAME}
            password: ${GIT_ACCESS_TOKEN}
            sync_mode: rebase

使用拉取模式同步到 GitHub

在 GitHub 中配置流水线来定时拉取 CNB 仓库

# .github/workflows/sync-cnb.yml

name: Sync from CNB
on:
  schedule:
    - cron: "0 1 * * *" # 每天1点执行

jobs:
  sync:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      
      - name: Sync from CNB Repository
        uses: docker://tencentcom/git-sync
        env:
          PLUGIN_TARGET_URL: "https://cnb.cool/username/repo.git"
          PLUGIN_AUTH_TYPE: "https"
          PLUGIN_USERNAME: "cnb"
          PLUGIN_PASSWORD: ${{ secrets.GIT_PASSWORD }}
          PLUGIN_SYNC_MODE: "pull"

使用 Docker 直接运行

docker run --rm \
  -e PLUGIN_TARGET_URL="https://github.com/username/repo.git" \
  -e PLUGIN_AUTH_TYPE="https" \
  -e PLUGIN_USERNAME="your-username" \
  -e PLUGIN_PASSWORD="your-access-token" \
  -e PLUGIN_BRANCH="main" \
  -v $(pwd):$(pwd) \
  -w $(pwd) \
  tencentcom/git-sync

参数说明

参数名必填默认值说明
target_url-目标仓库的URL,支持HTTPS或SSH格式
auth_typehttps认证类型,可选值:httpsssh
username否*-HTTPS认证时的用户名(*使用HTTPS时必填)
password否*-HTTPS认证时的密码或访问令牌(*使用HTTPS时必填)
ssh_key否*-SSH私钥内容(*使用SSH时必填)
branch-要推送的目标分支,指定后只推送这个分支。不指定时推送所有分支
forcefalse是否强制推送(使用--force选项)。在rebase模式下默认为true
push_tagsfalse是否推送标签
git_userGit Sync PluginGit提交时使用的用户名
git_emailgit-sync@plugin.localGit提交时使用的邮箱
git_host-自定义Git服务器域名
sync_modepush同步模式,可选值:push(推送),rebase(保留目标仓库文件) 或 pull(拉取)

安全建议

  1. 使用 HTTPS 认证时,建议使用访问令牌(Access Token)而不是实际密码
  2. 确保将敏感信息(如密码、访问令牌、SSH 密钥)保存在 CNB 的密钥仓库中,然后通过 imports 引入
  3. 如果使用 SSH 密钥,确保密钥具有适当的权限
  4. 建议在目标仓库上设置适当的访问权限控制

常见问题

  1. HTTPS 认证失败

    • 检查用户名和密码/令牌是否正确
    • 确认令牌是否具有足够的权限
    • 验证目标仓库 URL 是否正确
  2. 推送失败

    • 检查是否有目标仓库的写入权限
    • 确认分支名称是否正确,是否指定了只推送某个分支
    • 如果遇到冲突,考虑使用 force: true
  3. 自定义 Git 服务器

    • 确保 git_host 参数设置正确
    • 检查服务器的 SSH 指纹是否正确添加
  4. dependabot[bot] 创建的分支同步失败

License

MIT License