一个用于在不同 Git 平台之间同步代码的插件。支持通过 HTTPS 或 SSH 方式同步代码到其他 Git 托管平台。
例如从 CNB 同步到 GitHub。
插件支持两种同步模式:推送模式(默认)和 Rebase 模式。
具体参数请查阅下方参数列表
在 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"
在 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 中配置流水线来定时拉取 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 中使用推送模式, 建议使用密钥仓库来存储密 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 同步到 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 中配置流水线来定时拉取 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 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_type | 否 | https | 目标仓库的认证类型,可选值:https 或 ssh |
| username | 否* | - | 目标仓库 HTTPS 认证的用户名(*使用 HTTPS 时必填) |
| password | 否* | - | 目标仓库 HTTPS 认证的密码或访问令牌(*使用 HTTPS 时必填) |
| ssh_key | 否* | - | 目标仓库 SSH 私钥内容(*使用 SSH 时必填) |
| source_url | 否 | - | 源仓库(来者)的 URL;不指定时使用当前工作目录的 origin |
| source_auth_type | 否 | 同 auth_type | 源仓库的认证类型,可选值:https 或 ssh |
| source_username | 否 | 同 username | 源仓库 HTTPS 认证的用户名 |
| source_password | 否 | 同 password | 源仓库 HTTPS 认证的密码或访问令牌 |
| source_ssh_key | 否 | 同 ssh_key | 源仓库 SSH 私钥内容 |
| branch | 否 | - | 要推送的目标分支,指定后只推送这个分支。不指定时推送所有分支 |
| force | 否 | false | 是否强制推送(使用--force选项)。在 rebase 模式下默认为 true |
| push_tags | 否 | false | 是否推送标签 |
| git_user | 否 | Git Sync Plugin | Git 提交时使用的用户名 |
| git_email | 否 | git-sync@plugin.local | Git 提交时使用的邮箱 |
| git_host | 否 | - | 自定义 Git 服务器域名 |
| sync_mode | 否 | push | 同步模式,可选值:push(推送)、rebase(保留目标仓库文件) 或 pull(拉取) |
HTTPS 认证失败
推送失败
force: true自定义 Git 服务器
git_host 参数设置正确dependabot[bot] 创建的分支同步失败