如何在Azure DevOps和GitHub之间正确设置双向同步 [英] How to properly setup a two-way sync between Azure DevOps and GitHub

查看:264
本文介绍了如何在Azure DevOps和GitHub之间正确设置双向同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过执行以下操作在Azure DevOps和GitHub之间创建双向同步:

I want to create a two-way sync between Azure DevOps and GitHub by doing the following:

  1. 使用CI触发器创建Azure DevOps管道,以将更改从Azure DevOps存储库推送到GitHub中的分支
  2. 创建第二个管道,以侦听来自GitHub的更改并将其拉回到Azure DevOps中的分支

这是我用于两个管道作业的命令:

Here are the commands that I'm using for both pipeline jobs:

git clone --mirror <source>
cd <repo name>
git remote add --mirror=fetch target <target>
mkdir ../workdir
git fetch origin
git --work-tree=../workdir/ checkout <branch name>
git push target <branch name>

当我测试管道时,最终会出现如下错误:

When I test the pipeline I end up with an error like this:

! [rejected]        <branch name> -> <branch name> (fetch first)
error: failed to push some refs to 'git@github.com:<target>/<target repo>.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

我绝不是git专家,因此非常感谢您的帮助.

I am by no means a git expert, so any help is very much appreciated.

更新#1

UPDATE # 1

以下捕获显示了我构建的用于将更改推送到GitHub的管道(基于此

The captures below show the pipeline that I've built to push changes to GitHub (based on this answer), but it still isn't working.

这是新错误:

Cloning into '<repo>'...
Switched to a new branch '<branch name>'
Branch '<branch name>' set up to track remote branch '<branch name>' from 'origin'.
warning: adding embedded git repository: <repo>
hint: You've added another git repository inside your current repository.
hint: Clones of the outer repository will not contain the contents of
hint: the embedded repository and will not know how to obtain it.
hint: If you meant to add a submodule, use:
hint: 
hint:   git submodule add <url> <repo>
hint: 
hint: If you added this path by mistake, you can remove it from the
hint: index with:
hint: 
hint:   git rm --cached <repo>
hint: 
hint: See "git help submodule" for more information.
[<branch name> XXXXXXX] Update from Azure DevOps
 1 file changed, 1 insertion(+)
 create mode 160000 <repo>
To git@github.com:<org>/<repo>.git
 ! [rejected]        <branch name> -> <branch name> (fetch first)
error: failed to push some refs to 'git@github.com:<org>/<repo>.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

更新#2

UPDATE # 2

我更新了脚本(如下所示),以便根据更新后的答案将更改从GitHub拉到Azure DevOps,但仍然无法正常工作.

I updated the script (shown below) to pull changes from GitHub to Azure DevOps based on the updated answer, but it still isn't working.

git clone git@github.com:XXX/XXX.git 

cd <repo folder>

git config --global user.email "XXX"
git config --global user.name "XXX"

git checkout <source branch>
git add .
git commit -m "Pull from GitHub"
git push https://{AzureDevopsPAT}@dev.azure.com/{org}/{pro}/_git/XXX.git <target branch>

这是错误:

Cloning into '<repo>'...
Warning: Permanently added the RSA host key for IP address '140.82.113.4' to the list of known hosts.
Already on '<source branch>'
Your branch is up to date with 'origin/<source branch>'.
On branch <source branch>
Your branch is up to date with 'origin/<source branch>'.

nothing to commit, working tree clean
error: src refspec <target branch> does not match any
error: failed to push some refs to '***'

推荐答案

如果我也在GitHub中进行更改,能否将其推回Azure DevOps?

if I also make a change in GitHub, can I get that pushed back to Azure DevOps?

对于此问题,首先您可以创建新的构建定义:

For this issue , first you can create new build definition:

在命令行任务中,第一步是使用2条命令设置您的信息:

In command line tasks, first step setup your info using 2 command:

git config --global user.email ""
git config --global user.name ""

第二步,我们使用此命令遍历github中的所有分支:

Second step we use this command to loop through all branch in github:

git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done

要推送到所有分支的最后一步:

Last step to push to all branch:

git push https://$(VSTSToken)@dev.azure.com/xxx/_git/xxx -u --all

为了在每次更改代码时启动构建,我们需要转到触发选项卡和设置,如下所示:

In order to kick the build every time there is a code change we need to go to trigger tab and setup like this:

关于将azure devops repo同步到github repo,请参考我以前的

Regarding syncing azure devops repo to github repo, please refer to my previous answer.

更新:

对于单个分支,将更新从github repo推送到azure devops repo,请参见以下命令:

For a single branch,push update from github repo to azure devops repo,see below command:

git clone https://github.com/XXX/XXX.git
git config --global user.name "XXX"
git checkout master
git add .
git commit -m "abc"
git push https://{AzureDevopsPAT}@dev.azure.com/{org}/{pro}/_git/delete.git

这篇关于如何在Azure DevOps和GitHub之间正确设置双向同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆