如何同步 Github 和 Azure DevOps 仓库?

6

我有两个仓库,一个在Azure DevOps上,另一个在GitHub上。我想更新GitHub仓库,使其具有两个仓库中相同的代码。如何才能在不手动复制源代码的情况下完成此操作?

5个回答

8
您可以包含运行 git 的流水线步骤,以复制存储库。类似于以下内容:
steps:
- bash: |
    git push --prune https://$(GITHUB_PAT)@github.com/$REPO_NAME \
        +refs/remotes/origin/*:refs/heads/* +refs/tags/*:refs/tags/*
  displayName: 'Copy to Github'
  condition: eq(variables['Build.SourceBranch'], 'refs/heads/master')

(从 https://github.com/Azure/AzureStor/blob/master/azure-pipelines.yml 复制,稍作修改)


5

我该如何在不手动复制源代码的情况下完成它?

据我所知,Azure Devops Repos本身没有这样的功能来监视git存储库并自动保持这两个存储库同步。

但是,我认为有一些解决方法可以满足您的要求。您可以查看this blog了解有关如何在管道中使用git命令执行上述操作的详细信息。此外,专为将一个Git存储库与另一个存储库同步而设计的免费Git Tools for Azure Devops扩展程序可能有助于您的情况。

此外:如果您喜欢这个同步功能,请随时在Azure Devops的User Voice forum中提交功能请求。产品团队会考虑您的想法,并在有任何更新时给出回复。


2
自 github 同步代码到 Azure DevOps 没有解决方案,因此我们可以使用 Github Actions 在每次 push 时将代码从 GitHub 存储库推送到 Azure DevOps。请参阅以下代码:https://github.com/harishlalwani/testAzureDevops
基本上,在 push 事件中,Github Actions 触发 commit.sh。接下来的代码将代码推送到 Azure DevOps。
AZUREPAT=$AZUREPAT
AZUSERNAME=$AZUSERNAME
AZUSER_EMAIL=$AZUSER_EMAIL
AZORG=$AZORG
git clone https://github.com/harishlalwani/testAzureDevops
cd testAzureDevops
rm -rf .git

cd ..
GIT_CMD_REPOSITORY="https://$AZUSERNAME:$AZUREPAT@dev.azure.com/$AZORG/TestGitSync/_git/TestGitSync" 
 
git clone $GIT_CMD_REPOSITORY

cp -r testAzureDevops/* TestGitSync/

cd TestGitSync

git config --global user.email "$AZUSER_EMAIL"
git config --global user.name "$AZUSERNAME"

git add .
git commit -m "sync from git to azure"

git push

2
<b>Create a Github Actions yaml file and add this</b>

同时,将这些变量添加到你的 Github secrets 中

name: Sync From Git To Azure

on: 
  push:
    branches: [master]
    
jobs:
  sync_from_git_to_azure:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Setup environment variables
      run: |
        echo ::set-env name=AZUREPAT::${{ secrets.AZUREPAT }}
        echo ::set-env name=AZUSERNAME::${{ secrets.AZUSERNAME }}
        echo ::set-env name=AZUSER_EMAIL::${{ secrets.AZUSER_EMAIL }}
        echo ::set-env name=AZORG::${{ secrets.AZORG }}
    - name: Git clone repo
      run: |
        git clone https://link-to-your-repo
        cd reponame
        rm -rf .git
    - name: Clone to Azure DevOps Repo
      run: |
        cd ..
        GIT_CMD_REPOSITORY="https://azure-repo-url" 
        git clone $GIT_CMD_REPOSITORY
    - name: Copy files from git repro to azure repro
      run: |
        cp -r testAzureDevops/* TestGitSync/
    - name: Configure user in azure repro
      run: |
        cd TestGitSync
        git config --global user.email "$AZUSER_EMAIL"
        git config --global user.name "$AZUSERNAME"
    - name: Commit and Push to azure
      run: |
        git add .
        git commit -m "sync from git to azure"
        git push

目前您的回答写得不清楚。请[编辑]以添加更多细节,帮助其他人了解如何回答所提出的问题。您可以在帮助中心找到有关编写良好答案的更多信息。 - Community

0
如果您的GitHub和Azure DevOps存储库已经创建好了,那么,
步骤1:(用于凭据安全性)
登录GitHub -> 设置 -> 代码和自动化 -> 环境 -> 新建环境 -> 给一个有意义的名称(我建议使用“develop”) -> 点击配置环境 -> 环境密钥 -> 添加密钥 -> 1. 名称(AZORG)和值(在Azure DevOps中提供组织名称) 2. 名称(AZUREPAT)和值(在Azure DevOps中提供个人访问令牌) 3. 名称(AZUSERNAME)和值(在Azure DevOps中提供用户名) 4. 名称(AZUSER_EMAIL)和值(在Azure DevOps中提供您的组织电子邮件)
步骤2:(创建触发GitHub流水线的工作流程)
转到“Actions”选项卡 -> 新建工作流程 -> 在右上角选择“自己设置工作流程” -> 在“main.yml”中 ->
name: Push github directory to another azure devops repository
on:
  push:
    branches:
      - '*' # applies for all branches in github.
      - '!master' # This line is used to restrict this pipeline which won't triggers for master branch. remove this if you need to trigger for code push in master branch (Create PR if needed in devops repos)
jobs:
  check-bats-version:
    runs-on: ubuntu-latest
    environment: develop # mention that environment name that what we have created in step 1.
    steps:
    - uses: actions/checkout@v2
    - name: Run script file
      env:
        AZUREPAT: ${{secrets.AZUREPAT}} # calling secrets from environment variables
        AZUSERNAME: ${{secrets.AZUSERNAME}}
        AZUSER_EMAIL: ${{secrets.AZUSER_EMAIL}}
        AZORG: ${{secrets.AZORG}}
      run: |
         chmod +x ./commit.sh
         ./commit.sh # it will calls 'commit.sh' file. Thats where our logic is in.
      shell: bash
  

第三步:(主要逻辑) 在 .github 目录之外创建 'commit.sh' 文件 ->
#!/bin/bash

AZUREPAT=$AZUREPAT
AZUSERNAME=$AZUSERNAME
AZUSER_EMAIL=$AZUSER_EMAIL
AZORG=$AZORG

# Remove Git information (for fresh git start)
rm -rf Brain-Squeezes/.git

# Fetch the changes from Azure DevOps to ensure we have latest
git fetch --unshallow

# Pull changes from Azure DevOps if its exiting branch and have commits on it
git pull https://$AZUSERNAME:$AZUREPAT@dev.azure.com/$AZORG/Brain%20Squeezes/_git/Brain-Squeezes.git

#git checkout -b $github_to_azure_sync

# Set Git user identity
git config --global user.email "$AZUSER_EMAIL"
git config --global user.name "$AZUSERNAME"

# Add all changes into stage, commit, and push to Azure DevOps
git add .
git commit -m "Sync from GitHub to Azure DevOps"
git push --force https://$AZUSERNAME:$AZUREPAT@dev.azure.com/$AZORG/Brain%20Squeezes/_git/Brain-Squeezes.git

第四步:(检查结果) 在GitHub存储库中添加任何文件或修改现有文件,这些更改将反映在Azure DevOps存储库的同一分支中。如果Azure DevOps中不存在该分支,则会自动使用相同的分支名称在GitHub中创建该分支。

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