如何正确地设置Azure DevOps和GitHub之间的双向同步?

4
我希望通过以下步骤在Azure DevOps和GitHub之间创建双向同步:
  1. 创建一个Azure DevOps流水线,其中包含CI触发器,将更改从Azure DevOps存储库推送到GitHub上的分支
  2. 创建第二个流水线,监听来自GitHub的更改并将它们拉回Azure DevOps中的分支
这是我用于两个流水线作业的命令:
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>

当我测试管道时,出现了这样的错误:
! [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 专家,所以非常感谢任何帮助。
更新 #1
下面的截图显示了我构建的管道来将更改推送到 GitHub(基于此 答案),但它仍然无法正常工作。

enter image description here

enter image description here

这是新错误:

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

我已更新以下脚本,根据最新答案从GitHub将更改拉到Azure DevOps,但它仍未起作用。

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 '***'
2个回答

0

我曾经使用Webhooks和Jenkins完成了类似的场景。

以下是将GitHub上的私有仓库与Azure Repos上的私有仓库同步

在GitHub上:

1- 使用ssh-keygen添加SSH密钥对认证到GitHub,将公钥复制到“设置”->“SSH和GPG密钥”->“新SSH密钥”中。

2- 从Github仓库 -> 设置 -> Webhooks -> 添加Webhook -> 在Payload URL中提供http://<your-jenkins-public-ip>:<port>/github-webhook/,选择所需的其余参数并保存。我设置了内容类型:application/json,在仓库机密中提供预定义的密钥。在“您想触发此Webhook的事件”中,我选择了“发送所有内容”。保存后确保已验证。

在Azure DevOps上:

3- 在Azure DevOps个人资料中创建个人访问令牌(PAT),将其复制并妥善保管。

在Jenkins上:

4- 配置Jenkins自由风格项目,在“源代码管理”中使用“SSH用户名和私钥”配置Git,提供您的GitHub用户名和在步骤1中生成的私钥。

5- 在“附加行为”选项卡中选择“清除存储库并强制克隆”。这将强制每次清除和克隆以避免错误。

6- 在“构建触发器”选项卡中选择“GitHub挂钩触发器用于GITScm轮询”。

7- 在“构建”选项卡中,在“执行shell”中添加以下脚本

# Now Jenkins already accessed the repo (source) and cloned it using credentials provided in the "Source Code Management" section above.
# Now checkout the branch you want to trigger from
git checkout main

# Our build is to push these updates to a remote repo, in this case "Azure Repos".
# Now, point to the remote repo,
git config remote.origin.url https://<username>@dev.azure.com/<username>/<org_name>/_git/<proj_name>

# Now checkout the branch you want to push to
git checkout main

# Push with personal access token (PAT) configured on Azure DevOps platform.
git push --force https://<Azure_PAT>@dev.azure.com/<username>/<org_name>/_git/<proj_name>

8- 将更改推送到您的 GitHub 存储库,并查看 Jenkins 构建历史记录及其对 Azure 存储库的影响。

您可能需要在 Azure Pipeline 上按相反步骤执行相反的过程。


0
如果我在 GitHub 上也进行了更改,那么我可以将其推回 Azure DevOps 吗?
对于这个问题,首先您可以创建新的构建定义:

enter image description here

enter image description here

在命令行任务中,第一步是使用两个命令设置您的信息:
git config --global user.email ""
git config --global user.name ""

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

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

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

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

为了在代码更改时每次启动构建,我们需要进入触发器选项卡并进行如下设置:

enter image description here

关于将Azure DevOps仓库同步至GitHub仓库,请参考我的answer更新: 针对单个分支,从GitHub仓库推送更新至Azure DevOps仓库,请使用以下命令:
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

enter image description here


抱歉,Hugh,你的解决方案对我不起作用。我会更新我的问题并附上结果。 - Steve L.
抱歉回复晚了。在 git push 步骤中,您需要在推送 URL 中提供您的 GitHub PAT,就像下面的示例一样:git push https://{yourPAT}@github.com/xxx/yyy.git - Hugh Lin
那么在结账时,我可以选择来自GitHub的任何源分支,对吗?然后对于推送,我仍然需要指定目标分支,是吗?例如: git clone https://github.com/XXX/XXX.git git config --global user.name "XXX" git checkout sourcebranch git add . git commit -m "abc" git push https://{AzureDevopsPAT}@dev.azure.com/{org}/{pro}/_git/delete.git targetbranch - Steve L.
1
是的,就是这样 :) - Hugh Lin
仍然没有成功。:/ - Steve L.
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接