首页 > 解决方案 > 如何在 Azure DevOps 和 GitHub 之间正确设置双向同步

问题描述

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

  1. 使用 CI 触发器创建 Azure DevOps 管道,将更改从 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(基于此答案)而构建的管道,但它仍然无法正常工作。

在此处输入图像描述

在此处输入图像描述

这是新的错误:

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 '***'

标签: gitazuregithubazure-devops

解决方案


我已经使用带有 Jenkins 的 Webhooks 完成了类似的场景。

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

在 GitHub 上:

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

2-从 Github repo -> Settings -> Webhooks -> Add Webhook -> 在 Payload URL providehttp://<your-jenkins-public-ip>:<port>/github-webhook/中,根据需要选择其余参数并保存。我设置内容类型:应用程序/json,提供在 repo 秘密中预定义的秘密。您希望在哪些事件中触发此 webhook?我选择了“将所有内容发送给我”。确保保存后验证。

在 Azure DevOps 上:

3-在 Azure DevOps 配置文件上创建个人访问令牌 (PAT),复制它并确保其安全。

在詹金斯:

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

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

6-在“构建触发器”选项卡中选择“GitHub hook trigger for GITScm polling”。

7-在“构建”选项卡中,在“执行外壳”中添加下面的脚本

# 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 上的反向步骤进行反向过程。


推荐阅读