Yikun / yikun.github.com

Yikun's Blog
69 stars 22 forks source link

巧用Github Action同步代码到Gitee #79

Open Yikun opened 4 years ago

Yikun commented 4 years ago

1. 背景

在开源贡献的代码托管的过程中,我们有时候有需要将Github的代码同步到其他远端仓库的需求。具体的,对于我们目前参与的项目来说核心诉求是:以Github社区作为主仓,并且定期自动同步到Gitee作为镜像仓库

2. 调研

最终结论:我们需要自己实现一个工具,通过某种计算资源自动的去完成周期同步功能。

3. 选型

其实调研结论有了后,我们面对的选型就那么几种:

PS:严格来讲,Github Action其实是第二种选择的子集,其实就是单纯的想体验一把,并且把我们的业务需求实现了。

4. 实现

4.1 Github Action的实现

Github Action提供了2种方式去实现Action:

作为以后端开发为主的我们,没太多纠结就选择了第一种类型。关于怎么构建一个Github的Action可以参考Github的官方文档Building actions。官方文档真的写的非常详细了,并且也通了了hello-world级别的入门教程。

4.1 同步的核心代码实现

而最终的关键实现就是,我们需要定义这个容器运行的脚本,原理很简单: image 大致就是以上4步:

  1. 通过Github API读取Repo列表。
  2. 下载或者更新Github repo的代码
  3. 设置远端分支
  4. 将最新同步的commit、branch、tag推送到Gitee。

关心细节的同学,具体可以参考代码:https://github.com/Yikun/gitee-mirror-action/blob/master/entrypoint.sh

5. 怎么用呢?

举了个简单的例子,我们想将Github/kunpengcompute同步到Gitee/kunpengcompute上面,需要做的非常简单,只需要2步:

  1. 将Gitee的私钥和Token,上传到项目的setting的Secrets中。 image

可以参考官方指引

  1. 新建一个Github workflow,在这个workflow里面使用Gitee Mirror Action。
    name: Gitee repos mirror periodic job
    on:
    # 如果需要PR触发把push前的#去掉
    # push:
    schedule:
    # 每天北京时间9点跑
    - cron:  '0 1 * * *'
    jobs:
    build:
    runs-on: ubuntu-latest
    steps:
    - name: Mirror the Github organization repos to Gitee.
      uses: Yikun/gitee-mirror-action@v0.01
      with:
        # 必选,需要同步的Github用户(源)
        src: github/Yikun
        # 必选,需要同步到的Gitee的用户(目的)
        dst: gitee/yikunkero
        # 必选,Gitee公钥对应的私钥,https://gitee.com/profile/sshkeys
        dst_key: ${{ secrets.GITEE_PRIVATE_KEY }}
        # 必选,Gitee对应的用于创建仓库的token,https://gitee.com/profile/personal_access_tokens
        dst_token:  ${{ secrets.GITEE_TOKEN }}
        # 如果是组织,指定组织即可,默认为用户user
        # account_type: org
        # 还有黑、白名单,静态名单机制,可以用于更新某些指定库
        # static_list: repo_name
        # black_list: 'repo_name,repo_name2'
        # white_list: 'repo_name,repo_name2'

可以参考鲲鹏库的实现

image

上图,大概是每个阶段的原理和最终的效果。现在,这个使用Gitee Mirror Action的workflow已经运行起来了,可以在链接看到。

6. 最后

好啦,这篇硬核软文就写到这里,有同步需求的同学,放心使用。更多用法,可以参考Hub-mirror-action的主页Readme。

Github Action 官方链接https://github.com/marketplace/actions/hub-mirror-action 代码仓库https://github.com/Yikun/hub-mirror-action

有任何问题或者疑问,希望可以和大家一起改进这个Action,有问题可以直接提Issue或者PR,不用客气。

Yikun commented 4 years ago

2.0已发布,现在支持github与gitee的双向同步。

leopardciaw commented 4 years ago

请问下,如何优雅的在fork 其他人的GitHub项目代码后,能单向同步更新他人代码呢。不想因自己不小心,就pull给他人而造成困扰。谢谢~

Yikun commented 4 years ago

@leopardciaw 加个upstream,然后,每次用rebase更新

git remote add upstream http://github.com/xxx/test.git

git remote update upstream

git rebase upstream/master

git push origin dev:dev
leopardciaw commented 4 years ago

@leopardciaw 加个upstream,然后,每次用rebase更新

git remote add upstream http://github.com/xxx/test.git

git remote update upstream

git rebase upstream/master

git push origin dev:dev

谢谢答复,我试下,不懂再来请教。

jalenzz commented 4 years ago

你好,可否支持 issue 的同步

Yikun commented 4 years ago

@jalenchuh 暂时没有支持计划哈

codingriver commented 4 years ago

@Yikun 2.0已发布,现在支持github与gitee的双向同步。

我用github做镜像仓库,用gitee做主仓库,怎么用action定时触发同步从gitee拉取到github,用你的action我没有配置好,出现错误了

2020-09-01T08:26:45.7736441Z (2/3) Importing... 2020-09-01T08:26:45.7822264Z load pubkey "/root/.ssh/id_rsa": invalid format 2020-09-01T08:26:45.8026664Z Warning: Permanently added 'github.com,140.82.114.3' (RSA) to the list of known hosts. 2020-09-01T08:26:46.2317529Z To github.com:codingriver/hugo-project.git 2020-09-01T08:26:46.2318579Z ! [rejected] origin/master -> master (fetch first) 2020-09-01T08:26:46.2319607Z error: failed to push some refs to 'git@github.com:codingriver/hugo-project.git'

配置: 2020-09-01T08:26:36.0366725Z ##[group]Run Yikun/hub-mirror-action@master 2020-09-01T08:26:36.0367327Z with: 2020-09-01T08:26:36.0367713Z src: gitee/codingriver 2020-09-01T08:26:36.0368027Z dst: github/codingriver 2020-09-01T08:26:36.0370476Z dst_key: ***

2020-09-01T08:26:36.0370864Z dst_token: *** 2020-09-01T08:26:36.0371173Z white_list: hugo-project 2020-09-01T08:26:36.0371486Z account_type: user 2020-09-01T08:26:36.0371994Z clone_style: https 2020-09-01T08:26:36.0372336Z cache_path: /github/workspace/hub-mirror-cache 2020-09-01T08:26:36.0372670Z force_update: false 2020-09-01T08:26:36.0372976Z debug: false 如果支持,麻烦楼主给指点下,codingriver@163.com

Yikun commented 4 years ago

@codingriver https://github.com/Yikun/hub-mirror-action/issues/37 在项目里提issue哈。

看了下,主要是因为源和目的端的base不一样导致的。

对于这种,建议把force_update设置为true,进行强制刷新。(目的端请备份)。 另外,单个项目同步建议使用static_list:

codingriver commented 4 years ago

@codingriver Yikun/hub-mirror-action#37 在项目里提issue哈。

看了下,主要是因为源和目的端的base不一样导致的。

对于这种,建议把force_update设置为true,进行强制刷新。(目的端请备份)。 另外,单个项目同步建议使用static_list:

  • white_list会先动态拿列表,然后再判断是否在list里
  • static_list直接从配置取值

谢谢,自己看错了,以为rsa配置错了,留言后发现是仓库冲突,已经修复,再次感谢作者提供工具

classmatezhu commented 2 years ago

我最近尝试了一下,发现不行了。然后去看了一眼您的鲲鹏里的运行情况,发现也是不能运行。

Yikun commented 2 years ago

@classmatezhu

Please make sure you have the correct access rights
and the repository exists.
Retry 2/3 exited 128, retrying in 2 seconds...
Load key "/root/.ssh/id_rsa": invalid format
Retry 3/3 exited 128, no more retries left.
git@gitee.com: Permission denied (publickey).
 Push failed 
fatal: Could not read from remote repository.
Total: 1, skip: 0, successed: 0, failed: 1.

看了下,你这个问题是因为key设置的问题,请确认secrets.GITEE_PRIVATE_KEY已经配置到了gitee侧的账户。鲲鹏目前无问题哦,显示失败是因为部分库为空导致的同步失败。

numajinfei commented 2 years ago

使用master版本,ssh方式,gh-->gitee正常,gitee-->gh(调换src, dst)失败:

(1/3) Downloading...
Updating repo...
(2/3) Creating...
binocular_vision repo exist, skip creating...
(3/3) Force pushing...
Remote exsits, re-create: set github to git@github.com:numajinfei/binocular_vision.git
(3/3) Force pushing...
Remote exsits, re-create: set github to git@github.com:numajinfei/binocular_vision.git
(3/3) Force pushing...
Cmd('git') failed due to: exit code(128)
  cmdline: git push -f github refs/remotes/origin/*:refs/heads/* --tags --prune
  stderr: 'Warning: Permanently added the ECDSA host key for IP address '192.30.255.112' to the list of known hosts.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.'
numajinfei commented 2 years ago

也有报错如下:

(1/3) Downloading...
Updating repo...
(2/3) Creating...
binocular_vision repo exist, skip creating...
(3/3) Force pushing...
Remote exsits, re-create: set github to git@github.com:numajinfei/binocular_vision.git
(3/3) Force pushing...
Remote exsits, re-create: set github to git@github.com:numajinfei/binocular_vision.git
(3/3) Force pushing...
Cmd('git') failed due to: exit code(128)
  cmdline: git push -f github refs/remotes/origin/*:refs/heads/* --tags --prune
  stderr: 'git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.'
Total: 1, skip: 0, successed: 0, failed: 1.
Failed: ['binocular_vision']
+ exit 1
Yikun commented 2 years ago

@numajinfei 调换顺序以后,token和key也要刷新为新的目的仓的配置。

numajinfei commented 2 years ago

@Yikun @numajinfei 调换顺序以后,token和key也要刷新为新的目的仓的配置。

gh--repo---secrets下新增了gh token, 而key具体怎么配置? 之前生成的是无密码的,现在需要gitee-->gh 同步的话,gh secrets端是存放pub key? 如gh配置private key,则workflow文件只需变化 token就行,gitee端只有pub key可配置项;只修改token的话,依然出现上述错误

Yikun commented 2 years ago

@numajinfei

classmatezhu commented 2 years ago

@Yikun @classmatezhu

Please make sure you have the correct access rights
and the repository exists.
Retry 2/3 exited 128, retrying in 2 seconds...
Load key "/root/.ssh/id_rsa": invalid format
Retry 3/3 exited 128, no more retries left.
git@gitee.com: Permission denied (publickey).
 Push failed 
fatal: Could not read from remote repository.
Total: 1, skip: 0, successed: 0, failed: 1.

看了下,你这个问题是因为key设置的问题,请确认secrets.GITEE_PRIVATE_KEY已经配置到了gitee侧的账户。鲲鹏目前无问题哦,显示失败是因为部分库为空导致的同步失败。

万分感谢