一个用于在不同 Git 平台之间同步代码的插件。支持通过 HTTPS 或 SSH 方式同步代码到其他 Git 托管平台。
例如从 CNB 同步到 GitHub。
插件支持两种同步模式:推送模式(默认)和 Rebase 模式。
具体参数请查阅下方参数列表
直接将源仓库内容推送到目标仓库。
在 CNB 中使用推送模式, 建议使用密钥仓库来存储密 GIT_USERNAME、GIT_PASSWORD 等敏感信息, 然后使用 imports 引用变量,提高安全性。
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}
在 GitHub Actions 中使用推送模式,注意需要使用 GitHub Secrets 来安全地存储敏感信息
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
run: |
docker run --rm \
-v ${{ github.workspace }}:${{ github.workspace }} \
-w ${{ github.workspace }} \
-e PLUGIN_TARGET_URL="https://cnb.cool/username/repo.git" \
-e PLUGIN_AUTH_TYPE="https" \
-e PLUGIN_USERNAME="cnb" \
-e PLUGIN_PASSWORD=${{ secrets.GIT_PASSWORD }} \
-e PLUGIN_FORCE="true" \
tencentcom/git-sync
通过 rebase 方式同步代码,保留目标仓库中的特定文件(如平台特定的配置文件)。适用于从 GitHub 同步到 CNB 时保留 .cnb.yml 等特有文件的场景。
在 CNB 中使用 rebase 模式:
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 Actions 中使用 rebase 模式:
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
run: |
docker run --rm \
-v ${{ github.workspace }}:${{ github.workspace }} \
-w ${{ github.workspace }} \
-e PLUGIN_TARGET_URL="https://cnb.cool/username/repo.git" \
-e PLUGIN_AUTH_TYPE="https" \
-e PLUGIN_USERNAME="cnb" \
-e PLUGIN_PASSWORD=${{ secrets.GIT_PASSWORD }} \
-e PLUGIN_SYNC_MODE="rebase" \
tencentcom/git-sync
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时必填) |
| 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(保留目标仓库文件) |
HTTPS 认证失败
推送失败
force: true自定义 Git 服务器
git_host 参数设置正确