0

I have an ADO repository and a GitHub repository. Both repositories have a 'master' branch with the same commit history. When I complete a Pull Request from any branch to 'master' branch and add a new commit to 'master' branch in ADO repository, I have to add the same commit to the GitHub repository and vice-versa so that commit history will remain the same. Is that possible? Please provide an example.

UPDATE:

I have tried the suggested solution:

git clone https://msit.visualstudio.com/<org>/_git/<repo-name>
git config --global user.name "abc@xyz.com"
git config --global user.name "ABC DEF"
git checkout master
git add .
git commit -m "abc"
git push https://github.com/xyz/project.git

On trying the same, I get teh following error:

fatal: Cannot prompt because user interactivity has been disabled.
fatal: could not read Username for 'https://msit.visualstudio.com': terminal prompts disabled
Switched to a new branch 'master'
Branch 'master' set up to track remote branch 'master' from 'origin'.

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.
user989988
  • 3,006
  • 7
  • 44
  • 91
  • If one is not a mirror of the other, while it's possible, it's going to be a lot of work and it's likely going to break. You're much better off if one is always a strict mirror of the other. – bk2204 Dec 02 '20 at 01:59
  • Could you please let me know what you meant strict mirror of the other? By having same commit history, it should be a strict mirror of the other correct? – user989988 Dec 02 '20 at 02:26
  • By “strict mirror,” I mean that you modify only one repository and sync its contents across to the other one, without permitting the second to receive any modifications that the first does not already have. – bk2204 Dec 02 '20 at 02:39

2 Answers2

0

You can try the following method to synchronize azure devops repo and github repo.

First create a build pipeline with github repo as source. Then add a cmd task to the pipeline to 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/xxx.git

enter image description here

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

enter image description here

The above is about syncing github repo to azure devops repo, regarding syncing azure devops repo to github repo, you just need to create another build pipeline with azure repo as the source in the same way, for details you can refer to this answer.

Hugh Lin
  • 17,829
  • 2
  • 21
  • 25
  • Thank you! Could you please help me understand the git commands you have mentioned? Let's say this is my GitHub repo URL: https://github.com/xyz/project.git and this is my ADO repo URL: https://microsoftit.visualstudio.com/DefaultCollection/OneITVSO/_git/repo-name. I made some changes in GitHub repo master branch. 1) Clone GitHub repo 2) checkout master 3) add a new commit 4) Push changes to ADO repo master branch. Is that what it means? What does AzureDevopsPAT stand for? – user989988 Dec 02 '20 at 22:49
  • I tried your solution and I get an error. Please see my updated question. – user989988 Dec 02 '20 at 23:51
  • @user989988 You need provide your github PAT in push url, like this `git push https://{yourPAT}@github.com/xxx/yyy.git` . Here is the [document](https://learn.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page) about azure devops pat. –  Dec 03 '20 at 06:57
0

Agree with Hugh

  1. Go Project settings --> Repositories --> click Repos you want to operate -->check the service account {project name} Build Service (xxx) and ensure the permission is set to allow. You could check the pic below.

enter image description here

  1. Create yaml pipeline in the Azure DevOps, we need select GitHub as the code resource, then select GitHub repository, it will save the yaml file in the GitHub Repo instead of Azure DevOps repo, then we could configure the CI trigger.

enter image description here

  1. Add the script to Sync GitHub Repo

It will save the yml file in the GitHub repo and then Sync it to Azure DevOps repo

 trigger:
    - master
    
    pool:
      vmImage: 'ubuntu-latest'
    
    steps:
    - checkout: self
      persistCredentials: true    #Allow scripts to access the system token
      clean: true 
    
    - task: Bash@3
      name: SyncRepo
      inputs:
        targetType: 'inline'
        script: |
          git config --global user.name "{email}"
          git checkout master
          git add .
          git commit -m "Sync GitHub Repo to Azure DevOps Repo"
          git push https://{PAT}@dev.azure.com/{org name}/{project name}/_git/{repo name}

Result:

enter image description here

In addition, we could try is with AccessToken, you could refer to this answer for more details.

trigger:
  branches:
    include:
    - '*'
variables:
  system_accesstoken: $(System.AccessToken)

pool:
  vmImage: 'ubuntu-latest'

steps:
- checkout: self
  persistCredentials: true    #Allow scripts to access the system token
  clean: true 

- task: Bash@3
  name: SyncRepo
  inputs:
    targetType: 'inline'
    script: |
      git config --global user.email "xxxx@xxx.com"
      git config --global user.name "sormita"
      git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
      git remote add vsts https://xxx.visualstudio.com/xxx/_git/xxxx
      git branch -r | grep -v '\->' | while read remote; do git -c http.extraheader="AUTHORIZATION: bearer $(system_accesstoken)" push -u vsts "${remote#origin/}"; done
Vito Liu
  • 7,525
  • 1
  • 8
  • 17